{"id":2135,"date":"2019-06-09T11:37:13","date_gmt":"2019-06-09T09:37:13","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2135"},"modified":"2021-10-11T19:12:02","modified_gmt":"2021-10-11T17:12:02","slug":"automate-video-trimming-with-ffmpeg-and-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/automate-video-trimming-with-ffmpeg-and-python\/","title":{"rendered":"Automate video trimming with FFMPEG and Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Ffmpeg<\/strong> is a free software to manipulate videos. You can also record the screen with this software and the video output occupies not very much space and it&#8217;s fast. So, if you like me need to record fast your ideas and you do not need fancy effects and <strong>you do not like heavy files<\/strong>, this tool is for you. I never found a software fo <strong>screen<\/strong> <strong>video recording<\/strong> that loads in a second and that saves the files in a millisecond, so I found my choice. You will find all the links I made for the usage of ffmpeg for the basic stuff I need at the end of this post. Sometimes you may have the need to trim some parts of your video. The <strong>fastest<\/strong> way (but really fast) is this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> ffmpeg -ss 00:00:00 -i 004.mp4 -to 00:02:00 -c copy num002.mp4<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Copy this command into an editor and save it as a .bat file. You will see that this works like a charm. I had a little different commands in the video below where I use python to specify the start and end of the trimming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python comes in help<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s wee what happens if we want to make things easier trimming videos in this live coding below.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Automate trimming with FFMPEG with Python\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/8pQ1KA8LmQ8?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using moviepy<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This code works selecting the file in a folder and then giving the start like 0 seconds and the end like 10 seconds.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip\nimport glob\n\n\n# Get the second you do not want in the movie\nfiles = glob.glob(\"*.mp4\")\nprint([(n, f) for n, f in enumerate(files)])\nfilenumber = int(input(\"File number: \"))\nstart = int(input(\"Start: \"))\nend = int(input(\"End: \"))\nffmpeg_extract_subclip(\n    files[filenumber], start, end, targetname=\"output_selected.mp4\")<\/pre>\n\n\n<h3>Ffmpeg & Python for videos<\/h3>\t\r\n<script>\r\nvar title = \"Ffmpeg\";\r\n\t\tvar links = [\r\n\t\t\t[\"https:\/\/pythonprogramming.altervista.org\/tkinter-utilities-convert-a-wav-in-mp3-with-a-gui\/\",\"Convert Wav to Mp3 with a GUI\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/get-audio-from-mp4-live-coding-ffmpeg-python-tkinter\/\",\"Get Audio From A Video\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-and-ffmpeg-grabs-some-seconds-from-an-mp4-video\/\",\"Get some seconds from a video\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/how-to-record-just-a-window-with-ffmpeg\/\",\"Record a window 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/ffmpeg-record-screen-update\/\",\"Record a window 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/record-the-screen-with-ffmpeg-and-python\/\",\"Record entire screen\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/automate-video-trimming-with-ffmpeg-and-python\/\",\"Automate video trimming\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/join-all-mp4-with-python-and-ffmpeg\/\",\"Join all mp4\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/concatenate-mp4-videos-of-different-size-with-python-and-ffmpeg\/\",\"Concatenate mp4 of different size\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/how-to-transform-an-mp4-to-gif-with-python-and-ffmpeg-ffmpy-module\/\",\"Mp4 to Gif with ffmpeg\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/animated-cartoon-gif-with-pil-and-python-1\/\",\"Convert Gif to Mp4 with ffmpeg (at the bottom of the post)\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/how-to-speed-a-video-2-times\/\",\"How to speed a video\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/tkinter-a-program-start-mp4-with-ffplay\/\",\"Start mp4 with ffplay\"]\r\n\t\t\t\t\t];\r\n\t\t\r\n\t<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n","protected":false},"excerpt":{"rendered":"Trim video with Python and FFMPEG the easy way.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/automate-video-trimming-with-ffmpeg-and-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3513,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,417,50],"tags":[197,4,416,53],"class_list":["post-2135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-ffmpeg","category-video","tag-ffmpeg","tag-python","tag-trim","tag-video"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=2135"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2135\/revisions"}],"predecessor-version":[{"id":10523,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2135\/revisions\/10523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3513"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}