{"id":163,"date":"2022-04-19T04:55:41","date_gmt":"2022-04-19T04:55:41","guid":{"rendered":"https:\/\/bigdata.lol\/?p=163"},"modified":"2022-07-24T19:45:09","modified_gmt":"2022-07-24T19:45:09","slug":"ffmpeg-scripting-with-the-video-swiss-army-knife","status":"publish","type":"post","link":"https:\/\/bigdata.lol\/?p=163","title":{"rendered":"ffmpeg: scripting with the video swiss army knife"},"content":{"rendered":"<p>FFmpeg has become my go-to tool for routine video file and stream manipulation tasks. The <a href=\"https:\/\/ffmpeg.org\/ffmpeg.html\">documentation for the ffmpeg command line utility<\/a> is enormous, as one might expect for a really powerful tool. While there&#8217;s no shortage of example scripts in the ffmpeg official documentation, I&#8217;ve always appreciated short-form, &#8220;cookbook&#8221; style lists of example scripts for given use-cases. Here are some of my most commonly used ffmpeg scripts:<\/p>\n<h4>View all of the metadata contained in a video file<\/h4>\n<p>&nbsp;<\/p>\n<pre style=\"padding-left: 80px;\">ffmpeg -i ~\/myVideoFile.mp4 2&gt;&amp;1<\/pre>\n<p>&nbsp;<\/p>\n<h4>Fix incorrect or missing rotation metadata<\/h4>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 40px;\">After shooting video on your mobile device, you open the video file for playback on your laptop and notice that the image orientation isn&#8217;t right. The video appears to have been rotated 90 degrees one way or another, or flipped upside down (180 degrees). Check the &#8220;rotate&#8221; metadata property value for the video stream in your video file:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"padding-left: 80px;\">ffmpeg -i ~\/myVideoFile.mp4 2&gt;&amp;1 | grep rotate<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 40px;\">If the script doesn&#8217;t output anything, assume a value of 0. Add or subtract from the noted rotate value (in increments of 90 degrees) to arrive at a corrected number. Create a copy of the video with the rotate metadata property specified (at 270 degrees in this case):<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"padding-left: 80px;\">ffmpeg -i ~\/myVideoFile.mp4 -movflags use_metadata_tags -map_metadata 0 -metadata:s:v rotate=\"270\" -c:v copy -c:a copy ~\/myVideoFile_reoriented.mp4<\/pre>\n<p>&nbsp;<\/p>\n<h4>Trim a video file<\/h4>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 40px;\">Create a short clip from a longer video, with a start time and length of your choosing:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"padding-left: 80px;\">ffmpeg -i ~\/myVideoFile.mp4 -ss 00:30 -t 17.5 -c:v libx264 -c:a copy ~\/myVideoFile_clipped.mp4<\/pre>\n<p>&nbsp;<\/p>\n<h4>Extract audio from a video file<\/h4>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 40px;\">Extract an audio stream from a video file, then copy it to a new audio-only container file:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"padding-left: 80px;\">ffmpeg -i ~\/myVideoFile.mp4 -qscale:a 0 -map a ~\/myAudioFile.m4a<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 40px;\">\n","protected":false},"excerpt":{"rendered":"<p>FFmpeg has become my go-to tool for routine video file and stream manipulation tasks. The documentation for the ffmpeg command line utility is enormous, as one might expect for a really powerful tool. While there&#8217;s no shortage of example scripts in the ffmpeg official documentation, I&#8217;ve always appreciated short-form, &#8220;cookbook&#8221; style lists of example scripts for given use-cases. Here are some of my most commonly used ffmpeg scripts: View all &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/bigdata.lol\/index.php?rest_route=\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bigdata.lol\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bigdata.lol\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bigdata.lol\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bigdata.lol\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":24,"href":"https:\/\/bigdata.lol\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":261,"href":"https:\/\/bigdata.lol\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/261"}],"wp:attachment":[{"href":"https:\/\/bigdata.lol\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bigdata.lol\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bigdata.lol\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}