{"id":15009,"date":"2015-03-16T18:29:53","date_gmt":"2015-03-16T23:29:53","guid":{"rendered":"http:\/\/bucktownbell.com\/?p=15009"},"modified":"2015-03-16T18:29:53","modified_gmt":"2015-03-16T23:29:53","slug":"ffmpeg-encode-video-in-reverse","status":"publish","type":"post","link":"http:\/\/bucktownbell.com\/?p=15009","title":{"rendered":"ffmpeg &#8211; encode video in reverse?"},"content":{"rendered":"<blockquote><p>Dump all video frames<\/p>\n<pre><code>$ ffmpeg -i input.mkv -an -qscale 1 %06d.jpg\r\n<\/code><\/pre>\n<p>Cat video frames in reverse order to FFmpeg as input<br \/>\n<code>$ cat $(ls -t *jpg) | ffmpeg -f image2pipe -vcodec mjpeg -r 25 -i - -i backwards.wav<br \/>\n-vcodec libx264 -vpre slow -crf 20 -threads 0 -acodec flac output.mkv<\/code><\/p><\/blockquote>\n<p>via <a href=\"http:\/\/stackoverflow.com\/questions\/2553448\/encode-video-in-reverse\">ffmpeg &#8211; encode video in reverse? &#8211; Stack Overflow<\/a>.<\/p>\n<p>The above ffmpeg command examples turned out to be very useful.\u00a0 Previously I had to do this manually in <a href=\"http:\/\/www.avidemux.org\">Avidemux<\/a>.<\/p>\n<p>The output of my IP camera is VGA 640&#215;480 and I needed to slice that up into a\u00a0 10&#215;10 array of little areas\u00a0 (100 jpeg files) using the following:<br \/>\n<code><br \/>\nconvert input.jpg -crop 64x48\u00a0 +repage\u00a0 +adjoin\u00a0 myoutputfile_%02d.jpg <\/code><\/p>\n<p>Motion detect reveals a lot of false positives which must be filtered out manually.\u00a0 In order to automate this I\u00a0 compare time n to time n+1 in only a couple of the 100 little jpegs separated in the above command. \u00a0 So far I&#8217;m using this:<br \/>\n<code><br \/>\ncompare -metric MAE time_n_number.jpg time_n+1_number.jpg\u00a0 null: 2&gt;&amp;1<br \/>\n<\/code><br \/>\nA changed portion of the jpeg will generate a high number which can be compared to a threshold in a script allowing me to eliminate most all false positive motion detects.<\/p>\n<p>More detailed explanation for the Image compare commands in the ImageMagick package can be found here:<\/p>\n<p><a href=\"http:\/\/www.imagemagick.org\/Usage\/compare\/#statistics\">ImageMagick v6 Examples &#8211;Image Comparing<\/a><\/p>\n<p>Therefore my entire process consists of separating the video into jpegs, finding changes in areas where there shouldn&#8217;t be changes and if not reassembling the jpegs back into a video file again all done automatically via bash scripting.<\/p>\n<p>There probably are more elegant solutions but this works for now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dump all video frames $ ffmpeg -i input.mkv -an -qscale 1 %06d.jpg Cat video frames in reverse order to FFmpeg as input $ cat $(ls -t *jpg) | ffmpeg -f image2pipe -vcodec mjpeg -r 25 -i &#8211; -i backwards.wav -vcodec &hellip; <a href=\"http:\/\/bucktownbell.com\/?p=15009\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[1345,852,1354,575],"class_list":["post-15009","post","type-post","status-publish","format-standard","hentry","category-programming","tag-bash-script","tag-ffmpeg","tag-image-processing","tag-useful-tips"],"_links":{"self":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/15009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15009"}],"version-history":[{"count":1,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/15009\/revisions"}],"predecessor-version":[{"id":15010,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/15009\/revisions\/15010"}],"wp:attachment":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15009"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}