Make WordPress Core


Ignore:
Timestamp:
03/21/2013 04:55:42 AM (12 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.