Ticket #35218: video_meta_expanded_filter.diff
File video_meta_expanded_filter.diff, 6.7 KB (added by , 7 years ago) |
---|
-
src/wp-admin/includes/media.php
diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php index 6bb541dd35..a8028602ef 100644
function wp_read_video_metadata( $file ) { 3082 3082 $metadata['audio'] = $data['audio']; 3083 3083 } 3084 3084 3085 if ( empty( $metadata['created_timestamp'] ) ) { 3086 $created_timestamp = wp_get_media_creation_timestamp( $data ); 3087 3088 if ( $created_timestamp !== false ) { 3089 if ( !isset( $metadata['video_meta'] ) ) { 3090 $metadata['video_meta'] = array(); 3091 } 3092 $metadata['video_meta']['created_timestamp'] = $created_timestamp; 3093 } 3094 } 3095 3085 3096 wp_add_id3_tag_data( $metadata, $data ); 3086 3097 3087 return $metadata; 3098 $file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null; 3099 3100 /** 3101 * Filters the array of metadata retrieved from a video. 3102 * 3103 * In core, usually this selection is what is stored. 3104 * More complete data can be parsed from the `$data` parameter. 3105 * 3106 * @since 4.9.0 3107 * 3108 * @param array $metadata Filtered Video metadata. 3109 * @param string $file Path to video file. 3110 * @param string $file_format File format of video, as analyzed by getID3. 3111 * @param string $data Raw metadata from getID3. 3112 */ 3113 return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data ); 3088 3114 } 3089 3115 3090 3116 /** … … function wp_read_audio_metadata( $file ) { 3133 3159 } 3134 3160 3135 3161 /** 3162 * Parse creation date from media metadata. 3163 * 3164 * The getID3 library doesn't have a standard method for getting creation dates, 3165 * so the location of this data can vary based on the MIME type. 3166 * 3167 * @since 4.9.0 3168 * 3169 * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt 3170 * 3171 * @param array $metadata The metadata returned by getID3::analyze(). 3172 * @return int|bool A UNIX timestamp for the media's creation date if available 3173 * or a boolean FALSE if a timestamp could not be determined. 3174 */ 3175 function wp_get_media_creation_timestamp( $metadata ) { 3176 $creation_date = false; 3177 3178 if ( empty( $metadata['fileformat'] ) ) { 3179 return $creation_date; 3180 } 3181 3182 $file_format = $metadata['fileformat']; 3183 3184 switch ( $file_format ) { 3185 case 'asf': 3186 if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) { 3187 $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix']; 3188 } 3189 break; 3190 3191 case 'matroska': 3192 case 'webm': 3193 if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) { 3194 $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] ); 3195 } 3196 elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) { 3197 $creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix']; 3198 } 3199 break; 3200 3201 case 'quicktime': 3202 case 'mp4': 3203 if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) { 3204 $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix']; 3205 } 3206 break; 3207 } 3208 3209 return $creation_date; 3210 } 3211 3212 /** 3136 3213 * Encapsulate logic for Attach/Detach actions 3137 3214 * 3138 3215 * @since 4.2.0 -
tests/phpunit/tests/media.php
diff --git tests/phpunit/data/uploads/small-video.mkv tests/phpunit/data/uploads/small-video.mkv new file mode 100644 index 0000000000..2b3308441f Binary files /dev/null and tests/phpunit/data/uploads/small-video.mkv differ diff --git tests/phpunit/data/uploads/small-video.mov tests/phpunit/data/uploads/small-video.mov new file mode 100644 index 0000000000..2cfbb86464 Binary files /dev/null and tests/phpunit/data/uploads/small-video.mov differ diff --git tests/phpunit/data/uploads/small-video.webm tests/phpunit/data/uploads/small-video.webm new file mode 100644 index 0000000000..63a974986b Binary files /dev/null and tests/phpunit/data/uploads/small-video.webm differ diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index 8fb3cc09d7..89b7c7b37a 100644
EOF; 2067 2067 $attachment_id = wp_insert_attachment( $data, '', 0 ); 2068 2068 $this->assertSame( 0, $attachment_id ); 2069 2069 } 2070 2071 /** 2072 * @ticket 35218 2073 */ 2074 function test_wp_get_media_creation_timestamp_video_asf() { 2075 $metadata = array( 2076 'fileformat' => 'asf', 2077 'asf' => array( 2078 'file_properties_object' => array( 2079 'creation_date_unix' => 123, 2080 ), 2081 ), 2082 ); 2083 2084 $this->assertEquals( 123, wp_get_media_creation_timestamp( $metadata ) ); 2085 } 2086 2087 /** 2088 * @ticket 35218 2089 */ 2090 function test_wp_get_media_creation_timestamp_video_matroska() { 2091 $metadata = array( 2092 'fileformat' => 'matroska', 2093 'matroska' => array( 2094 'comments' => array( 2095 'creation_time' => array( 2096 '2015-12-24T17:40:09Z' 2097 ), 2098 ), 2099 ), 2100 ); 2101 2102 $this->assertEquals( 1450978809, wp_get_media_creation_timestamp( $metadata ) ); 2103 } 2104 2105 /** 2106 * @ticket 35218 2107 */ 2108 function test_wp_get_media_creation_timestamp_video_quicktime() { 2109 $metadata = array( 2110 'fileformat' => 'quicktime', 2111 'quicktime' => array( 2112 'moov' => array( 2113 'subatoms' => array( 2114 array( 2115 'creation_time_unix' => 1450978805, 2116 ), 2117 ), 2118 ), 2119 ), 2120 ); 2121 2122 $this->assertEquals( 1450978805, wp_get_media_creation_timestamp( $metadata ) ); 2123 } 2124 2125 /** 2126 * @ticket 35218 2127 */ 2128 function test_wp_get_media_creation_timestamp_video_webm() { 2129 $metadata = array( 2130 'fileformat' => 'webm', 2131 'matroska' => array( 2132 'info' => array( 2133 array( 2134 'DateUTC_unix' => 1265680539, 2135 ), 2136 ), 2137 ), 2138 ); 2139 2140 $this->assertEquals( 1265680539, wp_get_media_creation_timestamp( $metadata ) ); 2141 } 2142 2143 /** 2144 * @ticket 35218 2145 */ 2146 function test_wp_read_video_metadata_adds_creation_date_with_quicktime() { 2147 $video = DIR_TESTDATA . '/uploads/small-video.mov'; 2148 $metadata = wp_read_video_metadata( $video ); 2149 2150 $this->assertEquals( 1269120551, $metadata['video_meta']['created_timestamp'] ); 2151 } 2152 2153 /** 2154 * @ticket 35218 2155 */ 2156 function test_wp_read_video_metadata_adds_creation_date_with_mp4() { 2157 $video = DIR_TESTDATA . '/uploads/small-video.mp4'; 2158 $metadata = wp_read_video_metadata( $video ); 2159 2160 $this->assertEquals( 1269120551, $metadata['video_meta']['created_timestamp'] ); 2161 } 2162 2163 /** 2164 * @ticket 35218 2165 */ 2166 function test_wp_read_video_metadata_adds_creation_date_with_mkv() { 2167 $video = DIR_TESTDATA . '/uploads/small-video.mkv'; 2168 $metadata = wp_read_video_metadata( $video ); 2169 2170 $this->assertEquals( 1269120551, $metadata['video_meta']['created_timestamp'] ); 2171 } 2172 2173 /** 2174 * @ticket 35218 2175 */ 2176 function test_wp_read_video_metadata_adds_creation_date_with_webm() { 2177 $video = DIR_TESTDATA . '/uploads/small-video.webm'; 2178 $metadata = wp_read_video_metadata( $video ); 2179 2180 $this->assertEquals( 1269120551, $metadata['video_meta']['created_timestamp'] ); 2181 } 2070 2182 } 2071 2183 2072 2184 /**