<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[41746] trunk: Media: Store video creation date in meta.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41746">41746</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41746","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>mikeschroder</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-10-04 19:31:51 +0000 (Wed, 04 Oct 2017)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/35218">#35218</a>.
Props stevegrunwell, joemcgill, desrosj, blobfolio, mikeschroder.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesmediaphp">trunk/src/wp-admin/includes/media.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitdatauploadssmallvideomkv">trunk/tests/phpunit/data/uploads/small-video.mkv</a></li>
<li><a href="#trunktestsphpunitdatauploadssmallvideomov">trunk/tests/phpunit/data/uploads/small-video.mov</a></li>
<li><a href="#trunktestsphpunitdatauploadssmallvideowebm">trunk/tests/phpunit/data/uploads/small-video.webm</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/media.php 2017-10-04 18:57:47 UTC (rev 41745)
+++ trunk/src/wp-admin/includes/media.php 2017-10-04 19:31:51 UTC (rev 41746)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3082,9 +3082,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $metadata['audio'] = $data['audio'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $metadata['created_timestamp'] ) ) {
+ $created_timestamp = wp_get_media_creation_timestamp( $data );
+
+ if ( $created_timestamp !== false ) {
+ $metadata['created_timestamp'] = $created_timestamp;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_add_id3_tag_data( $metadata, $data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $metadata;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null;
+
+ /**
+ * Filters the array of metadata retrieved from a video.
+ *
+ * In core, usually this selection is what is stored.
+ * More complete data can be parsed from the `$data` parameter.
+ *
+ * @since 4.9.0
+ *
+ * @param array $metadata Filtered Video metadata.
+ * @param string $file Path to video file.
+ * @param string $file_format File format of video, as analyzed by getID3.
+ * @param string $data Raw metadata from getID3.
+ */
+ return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3133,6 +3156,55 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Parse creation date from media metadata.
+ *
+ * The getID3 library doesn't have a standard method for getting creation dates,
+ * so the location of this data can vary based on the MIME type.
+ *
+ * @since 4.9.0
+ *
+ * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt
+ *
+ * @param array $metadata The metadata returned by getID3::analyze().
+ * @return int|bool A UNIX timestamp for the media's creation date if available
+ * or a boolean FALSE if a timestamp could not be determined.
+ */
+function wp_get_media_creation_timestamp( $metadata ) {
+ $creation_date = false;
+
+ if ( empty( $metadata['fileformat'] ) ) {
+ return $creation_date;
+ }
+
+ switch ( $metadata['fileformat'] ) {
+ case 'asf':
+ if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) {
+ $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix'];
+ }
+ break;
+
+ case 'matroska':
+ case 'webm':
+ if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) {
+ $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] );
+ }
+ elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) {
+ $creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix'];
+ }
+ break;
+
+ case 'quicktime':
+ case 'mp4':
+ if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) {
+ $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'];
+ }
+ break;
+ }
+
+ return $creation_date;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Encapsulate logic for Attach/Detach actions
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.2.0
</span></span></pre></div>
<a id="trunktestsphpunitdatauploadssmallvideomkv"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/data/uploads/small-video.mkv</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/uploads/small-video.mkv (rev 0)
+++ trunk/tests/phpunit/data/uploads/small-video.mkv 2017-10-04 19:31:51 UTC (rev 41746)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,1533 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+Eߣ #B\x86\x81B\xF7\x81B\xF2\x81B\xF3\x81B\x82\x88matroskaB\x87\x81B\x85\x81S\x80g \x81M\x9Bt@B\xBF\x84cZgM\xBB\x8BS\xAB\x84I\xA9fS\xAC\x81\xE5M\xBB\x8CS\xAB\x84T\xAEkS\xAC\x82GM\xBB\x8CS\xAB\x84T\xC3gS\xAC\x82M\xBB\x8DS\xAB\x84S\xBBkS\xAC\x83^\xEC \x94 I\xA9f V\xBF\x843Y_*ױ\x83B@M\x80\x8DLavf57.56.101WA\x8DLavf57.56.101s\xA4\x906\xDC0ڗG\xB9\xD5\xD1gԨ\xE4\x91\xFADa\x88 -3\xB2K\xC6 D\x89\x88@\xB5\xC0 T\xAEk
+\xB0\xBF\x848¬\xEA\xAE qׁsŁ\x9C\x81 "\xB5\x9C\x83und\x86\x8FV_MPEG4/ISO/AVC\x83\x81#ツ\xFC\xA0U\xE0 \xB0\x820\xBA\x82@\x9A\x81T\xB2\x81c\xA2\xA9d \xFF\xE1 gd \xAC\xD9@\x8C)\xA1 <-\x96 h\xEB\xE3\xCB"\xC0\xAE
+'ׁsŁ\x9C
\x81 "\xB5\x9C\x83eng\x86\x88A_VORBIS\x83\x81\xE1 \x9F\x81\xB5\x88@\xE7p bd\x81 c\xA2L\xEBUvorbis \x80\xBB \x808 \xB8vorbis, Xiph.Org libVorbis I 20150105 (⛄⛄⛄⛄) encoder=Lavc57.64.101vorbis"BCV @ $s*F\xA5s\x84BP\xE3B\xCEk\xECBL\x822L[\xCB%s\x90!\xA4\xA0B\x88[(\x81АU @ \x87Ax\x84\x8AA!\x84%=X\x92\x83'=!\x84\x889x\x84iA!\x84B!\x84B!\x84E9h\x92\x83'A\x84\xE308\x83\xE58\xF8\x84E9X\x83'A\xE8 \x84B\xB8\x9A\x83\xAC9!\x84$5HP\x839\xE8\x84\xC2,(\x8A\x82\xC40\xB8\x845(\x8C\x82\xE40\xC8ԃB\x88\x9A\x83I5\xF8\x84gAx\x84iA!\x84$AH\x90\x83A\xC8\x84FAX\x92\x839\xB8\x84\xCBA\xA8\x84*9\x84 4d \x90 \xA0\xA2(\x8A\xA2(
+\xB2
+ \xC8 @Q\xC7qɑ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