Make WordPress Core

Changeset 23766


Ignore:
Timestamp:
03/21/2013 04:55:42 AM (14 years ago)
Author:
markjaquith
Message:

Add functions for generating metadata for video and audio, using the
ID3 library. Also allows themes/plugins to add thumbnail support
to these media types. Think stuff like album art, movie covers, and
video freeze-frames.

props wonderboymusic. fixes #23673

Location:
trunk
Files:
17 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/edit-form-advanced.php

    r23753 r23766  
    142142        add_meta_box('pageparentdiv', 'page' == $post_type ? __('Page Attributes') : __('Attributes'), 'page_attributes_meta_box', null, 'side', 'core');
    143143
    144 if ( current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) )
    145                 add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', null, 'side', 'low');
     144$audio_post_support = $video_post_support = false;
     145$theme_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
     146if ( 'attachment' === $post_type && ! empty( $post->post_mime_type ) ) {
     147        $audio_post_support = 0 === strpos( $post->post_mime_type, 'audio/' ) && current_theme_supports( 'post-thumbnails', 'attachment:audio' ) && post_type_supports( 'attachment:audio', 'thumbnail' );
     148        $video_post_support = 0 === strpos( $post->post_mime_type, 'video/' ) && current_theme_supports( 'post-thumbnails', 'attachment:video' ) && post_type_supports( 'attachment:video', 'thumbnail' );
     149}
     150
     151if ( $theme_support || $audio_post_support || $video_post_support )
     152        add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', null, 'side', 'low');
    146153
    147154if ( post_type_supports($post_type, 'excerpt') )
  • trunk/wp-admin/includes/image.php

    r23632 r23766  
    7474
    7575        $metadata = array();
     76        $support = false;
    7677        if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
    7778                $imagesize = getimagesize( $file );
    … …  
    118119                        $metadata['image_meta'] = $image_meta;
    119120
    120         }
     121        } elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) {
     122                $metadata = wp_read_video_metadata( $file );
     123                $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) && post_type_supports( 'attachment:video', 'thumbnail' );
     124        } elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) {
     125                $metadata = wp_read_audio_metadata( $file );
     126                $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) && post_type_supports( 'attachment:audio', 'thumbnail' );
     127        }
     128
     129        if ( $support && ! empty( $metadata['image']['data'] ) ) {
     130                $ext = '.jpg';
     131                switch ( $metadata['image']['mime'] ) {
     132                case 'image/gif':
     133                        $ext = '.gif';
     134                        break;
     135                case 'image/png':
     136                        $ext = '.png';
     137                        break;
     138                }
     139                $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
     140                $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
     141                if ( false === $uploaded['error'] ) {
     142                        $attachment = array(
     143                                'post_mime_type' => $metadata['image']['mime'],
     144                                'post_type' => 'attachment',
     145                                'post_content' => '',
     146                        );
     147                        $sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] );
     148                        $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
     149                        wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
     150                        update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
     151                }
     152        }
     153        // remove the blob of binary data from the array
     154        unset( $metadata['image']['data'] );
    121155
    122156        return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
  • trunk/wp-admin/includes/media.php

    r23739 r23766  
    23982398
    23992399add_action( 'attachment_submitbox_misc_actions', 'attachment_submitbox_metadata' );
     2400
     2401/**
     2402 * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
     2403 *
     2404 * @since 3.6.0
     2405 *
     2406 * @param array $metadata An existing array with data
     2407 * @param array $data Data supplied by ID3 tags
     2408 */
     2409function wp_add_id3_tag_data( &$metadata, $data ) {
     2410        foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
     2411                if ( ! empty( $data[$version]['comments'] ) ) {
     2412                        foreach ( $data[$version]['comments'] as $key => $list ) {
     2413                                if ( ! empty( $list ) ) {
     2414                                        $metadata[$key] = reset( $list );
     2415                                        // fix bug in byte stream analysis
     2416                                        if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
     2417                                                $metadata[$key] = 'Cop' . $metadata[$key];
     2418                                }
     2419                        }
     2420                        break;
     2421                }
     2422        }
     2423
     2424        if ( ! empty( $data['id3v2']['APIC'] ) ) {
     2425                $image = reset( $data['id3v2']['APIC']);
     2426                if ( ! empty( $image['data'] ) ) {
     2427                        $metadata['image'] = array(
     2428                                'data' => $image['data'],
     2429                                'mime' => $image['image_mime'],
     2430                                'width' => $image['image_width'],
     2431                                'height' => $image['image_height']
     2432                        );
     2433                }
     2434        } elseif ( ! empty( $data['comments']['picture'] ) ) {
     2435                $image = reset( $data['comments']['picture'] );
     2436                if ( ! empty( $image['data'] ) ) {
     2437                        $metadata['image'] = array(
     2438                                'data' => $image['data'],
     2439                                'mime' => $image['image_mime']
     2440                        );
     2441                }
     2442        }
     2443}
     2444
     2445/**
     2446 * Retrieve metadata from a video file's ID3 tags
     2447 *
     2448 * @since 3.6.0
     2449 *
     2450 * @param string $file Path to file.
     2451 * @return array|boolean Returns array of metadata, if found.
     2452 */
     2453function wp_read_video_metadata( $file ) {
     2454        if ( ! file_exists( $file ) )
     2455                return false;
     2456
     2457        $metadata = array();
     2458
     2459        if ( ! class_exists( 'getID3' ) )
     2460                require( ABSPATH . WPINC . '/ID3/class-getid3.php' );
     2461        $id3 = new getID3();
     2462        $data = $id3->analyze( $file );
     2463
     2464        if ( isset( $data['video']['lossless'] ) )
     2465                $metadata['lossless'] = $data['video']['lossless'];
     2466        if ( ! empty( $data['video']['bitrate'] ) )
     2467                $metadata['bitrate'] = (int) $data['video']['bitrate'];
     2468        if ( ! empty( $data['video']['bitrate_mode'] ) )
     2469                $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
     2470        if ( ! empty( $data['filesize'] ) )
     2471                $metadata['filesize'] = (int) $data['filesize'];
     2472        if ( ! empty( $data['mime_type'] ) )
     2473                $metadata['mime_type'] = $data['mime_type'];
     2474        if ( ! empty( $data['playtime_seconds'] ) )
     2475                $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
     2476        if ( ! empty( $data['playtime_string'] ) )
     2477                $metadata['length_formatted'] = $data['playtime_string'];
     2478        if ( ! empty( $data['video']['resolution_x'] ) )
     2479                $metadata['width'] = (int) $data['video']['resolution_x'];
     2480        if ( ! empty( $data['video']['resolution_y'] ) )
     2481                $metadata['height'] = (int) $data['video']['resolution_y'];
     2482        if ( ! empty( $data['fileformat'] ) )
     2483                $metadata['fileformat'] = $data['fileformat'];
     2484        if ( ! empty( $data['video']['dataformat'] ) )
     2485                $metadata['dataformat'] = $data['video']['dataformat'];
     2486        if ( ! empty( $data['video']['encoder'] ) )
     2487                $metadata['encoder'] = $data['video']['encoder'];
     2488        if ( ! empty( $data['video']['codec'] ) )
     2489                $metadata['codec'] = $data['video']['codec'];
     2490
     2491        unset( $data['audio']['streams'] );
     2492        $metadata['audio'] = $data['audio'];
     2493
     2494        wp_add_id3_tag_data( $metadata, $data );
     2495
     2496        return $metadata;
     2497}
     2498
     2499/**
     2500 * Retrieve metadata from a audio file's ID3 tags
     2501 *
     2502 * @since 3.6.0
     2503 *
     2504 * @param string $file Path to file.
     2505 * @return array|boolean Returns array of metadata, if found.
     2506 */
     2507function wp_read_audio_metadata( $file ) {
     2508        if ( ! file_exists( $file ) )
     2509                return false;
     2510        $metadata = array();
     2511
     2512        if ( ! class_exists( 'getID3' ) )
     2513                require( ABSPATH . WPINC . '/ID3/class-getid3.php' );
     2514        $id3 = new getID3();
     2515        $data = $id3->analyze( $file );
     2516
     2517        if ( ! empty( $data['audio'] ) ) {
     2518                unset( $data['audio']['streams'] );
     2519                $metadata = $data['audio'];
     2520        }
     2521
     2522        if ( ! empty( $data['fileformat'] ) )
     2523                $metadata['fileformat'] = $data['fileformat'];
     2524        if ( ! empty( $data['filesize'] ) )
     2525                $metadata['filesize'] = (int) $data['filesize'];
     2526        if ( ! empty( $data['mime_type'] ) )
     2527                $metadata['mime_type'] = $data['mime_type'];
     2528        if ( ! empty( $data['playtime_seconds'] ) )
     2529                $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
     2530        if ( ! empty( $data['playtime_string'] ) )
     2531                $metadata['length_formatted'] = $data['playtime_string'];
     2532
     2533        wp_add_id3_tag_data( $metadata, $data );
     2534
     2535        return $metadata;
     2536}
Note: See TracChangeset for help on using the changeset viewer.