Make WordPress Core

Changeset 41746


Ignore:
Timestamp:
10/04/2017 07:31:51 PM (9 years ago)
Author:
mikeschroder
Message:

Media: Store video creation date in meta.

When able to be parsed, store the created date for a video file from meta,
since this is useful separately from the dates on the file itself.

Introduces wp_get_media_creation_timestamp() to read the timestamp from
getID3 and a wp_read_video_metadata filter analogous to
wp_read_image_metadata.

Fixes #35218.
Props stevegrunwell, joemcgill, desrosj, blobfolio, mikeschroder.

Location:
trunk
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/media.php

    r41581 r41746  
    30833083        }
    30843084
     3085        if ( empty( $metadata['created_timestamp'] ) ) {
     3086                $created_timestamp = wp_get_media_creation_timestamp( $data );
     3087
     3088                if ( $created_timestamp !== false ) {
     3089                        $metadata['created_timestamp'] = $created_timestamp;
     3090                }
     3091        }
     3092
    30853093        wp_add_id3_tag_data( $metadata, $data );
    30863094
    3087         return $metadata;
     3095        $file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null;
     3096
     3097        /**
     3098         * Filters the array of metadata retrieved from a video.
     3099         *
     3100         * In core, usually this selection is what is stored.
     3101         * More complete data can be parsed from the `$data` parameter.
     3102         *
     3103         * @since 4.9.0
     3104         *
     3105         * @param array  $metadata       Filtered Video metadata.
     3106         * @param string $file           Path to video file.
     3107         * @param string $file_format    File format of video, as analyzed by getID3.
     3108         * @param string $data           Raw metadata from getID3.
     3109         */
     3110        return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data );
    30883111}
    30893112
     
    31343157
    31353158/**
     3159 * Parse creation date from media metadata.
     3160 *
     3161 * The getID3 library doesn't have a standard method for getting creation dates,
     3162 * so the location of this data can vary based on the MIME type.
     3163 *
     3164 * @since 4.9.0
     3165 *
     3166 * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt
     3167 *
     3168 * @param array $metadata The metadata returned by getID3::analyze().
     3169 * @return int|bool A UNIX timestamp for the media's creation date if available
     3170 *                  or a boolean FALSE if a timestamp could not be determined.
     3171 */
     3172function wp_get_media_creation_timestamp( $metadata ) {
     3173        $creation_date = false;
     3174
     3175        if ( empty( $metadata['fileformat'] ) ) {
     3176                return $creation_date;
     3177        }
     3178
     3179        switch ( $metadata['fileformat'] ) {
     3180                case 'asf':
     3181                        if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) {
     3182                                $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix'];
     3183                        }
     3184                        break;
     3185
     3186                case 'matroska':
     3187                case 'webm':
     3188                        if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) {
     3189                                $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] );
     3190                        }
     3191                        elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) {
     3192                                $creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix'];
     3193                        }
     3194                        break;
     3195
     3196                case 'quicktime':
     3197                case 'mp4':
     3198                        if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) {
     3199                                $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'];
     3200                        }
     3201                        break;
     3202        }
     3203
     3204        return $creation_date;
     3205}
     3206
     3207/**
    31363208 * Encapsulate logic for Attach/Detach actions
    31373209 *
  • trunk/tests/phpunit/tests/media.php

    r41724 r41746  
    21392139                $this->assertSame( 0, $attachment_id );
    21402140        }
     2141
     2142        /**
     2143         * @ticket 35218
     2144         */
     2145        function test_wp_get_media_creation_timestamp_video_asf() {
     2146                $metadata = array(
     2147                        'fileformat' => 'asf',
     2148                        'asf'        => array(
     2149                                'file_properties_object' => array(
     2150                                        'creation_date_unix' => 123,
     2151                                ),
     2152                        ),
     2153                );
     2154
     2155                $this->assertEquals( 123, wp_get_media_creation_timestamp( $metadata ) );
     2156        }
     2157
     2158        /**
     2159         * @ticket 35218
     2160         */
     2161        function test_wp_get_media_creation_timestamp_video_matroska() {
     2162                $metadata = array(
     2163                        'fileformat' => 'matroska',
     2164                        'matroska'   => array(
     2165                                'comments' => array(
     2166                                        'creation_time' => array(
     2167                                                '2015-12-24T17:40:09Z'
     2168                                        ),
     2169                                ),
     2170                        ),
     2171                );
     2172
     2173                $this->assertEquals( 1450978809, wp_get_media_creation_timestamp( $metadata ) );
     2174        }
     2175
     2176        /**
     2177         * @ticket 35218
     2178         */
     2179        function test_wp_get_media_creation_timestamp_video_quicktime() {
     2180                $metadata = array(
     2181                        'fileformat' => 'quicktime',
     2182                        'quicktime'  => array(
     2183                                'moov' => array(
     2184                                        'subatoms' => array(
     2185                                                array(
     2186                                                        'creation_time_unix' => 1450978805,
     2187                                                ),
     2188                                        ),
     2189                                ),
     2190                        ),
     2191                );
     2192
     2193                $this->assertEquals( 1450978805, wp_get_media_creation_timestamp( $metadata ) );
     2194        }
     2195
     2196        /**
     2197         * @ticket 35218
     2198         */
     2199        function test_wp_get_media_creation_timestamp_video_webm() {
     2200                $metadata = array(
     2201                        'fileformat' => 'webm',
     2202                        'matroska'   => array(
     2203                                'info' => array(
     2204                                        array(
     2205                                                'DateUTC_unix' => 1265680539,
     2206                                        ),
     2207                                ),
     2208                        ),
     2209                );
     2210
     2211                $this->assertEquals( 1265680539, wp_get_media_creation_timestamp( $metadata ) );
     2212        }
     2213
     2214        /**
     2215         * @ticket 35218
     2216         */
     2217        function test_wp_read_video_metadata_adds_creation_date_with_quicktime() {
     2218                $video    = DIR_TESTDATA . '/uploads/small-video.mov';
     2219                $metadata = wp_read_video_metadata( $video );
     2220
     2221                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2222        }
     2223
     2224        /**
     2225         * @ticket 35218
     2226         */
     2227        function test_wp_read_video_metadata_adds_creation_date_with_mp4() {
     2228                $video    = DIR_TESTDATA . '/uploads/small-video.mp4';
     2229                $metadata = wp_read_video_metadata( $video );
     2230
     2231                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2232        }
     2233
     2234        /**
     2235         * @ticket 35218
     2236         */
     2237        function test_wp_read_video_metadata_adds_creation_date_with_mkv() {
     2238                $video    = DIR_TESTDATA . '/uploads/small-video.mkv';
     2239                $metadata = wp_read_video_metadata( $video );
     2240
     2241                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2242        }
     2243
     2244        /**
     2245         * @ticket 35218
     2246         */
     2247        function test_wp_read_video_metadata_adds_creation_date_with_webm() {
     2248                $video    = DIR_TESTDATA . '/uploads/small-video.webm';
     2249                $metadata = wp_read_video_metadata( $video );
     2250
     2251                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2252        }
    21412253}
    21422254
Note: See TracChangeset for help on using the changeset viewer.