How to Trim Videos Using Python and FFMPEG

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to learn how to trim videos using ffmpeg in the python programming language the python package that we're going to be using is called ffmpeg python this library is a wrapper around the ffmpeg command line tool in this tutorial we will trim this video that i created for a previous youtube video for example we'll provide a start and an end trim this means that we're going to be providing a start trim for this particular video over here of 10 seconds and we're going to provide an end trim of 18 seconds which will produce a final trimmed video of 8 seconds this is coding with adam and let's get to the code if you enjoy my videos please subscribe like and share the first thing that we need to do is to go ahead and check if we have ffmpeg installed well the easiest way to do that is to go to our terminal type ffmpeg dash version if you have it installed you're going to get to this version information if you don't have it installed that's okay i'm going to show you how you can install it on a mac and on windows and if you're curious about my command prompt and its different colors over here go check out my previous video on z shell and how to customize your command prompt let's start with the mac installation instructions the first thing you're going to want to do is install a brew brew is a package manager really easy to install just copy this command over here paste that in your terminal and execute it then once brew is installed all we have to do is run brew install ffmpeg so we can do that over here brew install ffmpeg and that will install ffmpeg on our machine as you can see i already have it installed for the windows installation we'll also use a package manager the package manager that we're going to use is called chocolately and chocolately much like brew is fairly simple to install you just need to use powershell and follow these directions over here once you have it installed you can then run chaco install ffmpeg from your command line tool to install ffmpeg now we're ready for the code so i've opened up a visual studio code over here and it's just opened up into a blank folder with nothing in it and i've named the project trim with ffmpeg to get started we're going to go ahead and create our python virtual environment i'm going to use pipe n to do this and we're going to pipe end install at our terminal over here that's gonna go ahead and create our pipe file so our virtual environment is created then let's go ahead and add a file called app.pi and inside this file just to double check that our virtual environment is working properly we're going to print out a test then within vs code we're just going to switch over to the new virtual environment that we created as well so we're going to select trim with ffmpeg which is the name of my project now go to run and debug we're going to create a launch json so we can run our project we're going to be launching python we can just accept this default configuration as that will be perfect for what we're doing and go to our app.pi click play in vs code and we're expecting to see a test on the screen and we do so our project is now set up and we're ready to dive into the ffmpeg trimming code next we'll go ahead and we'll install the ffmpeg python library inside of our terminal let's go ahead and do a pipe end install ffmpeg dash python we'll let that library install we can confirm its installation by going to our explorer clicking on pipe file and we should see packages ffmpeg python this is dependent on whether or not you're using pipen for your project however i am so i can confirm it right over here next we can go back to our app.pi inside of our app.pi we'll remove our print test and we're going to do an import of os and an import of ffmpeg let's go ahead and start writing this code by taking the end in mind so in the end what i want to do is i want to create a method the first parameter will be the path to the file so for us this will just be movie.mp4 the second parameter will be the output path so we'll just call that out.mp4 and then the start duration where we want to start the trim and we'll say that's 10 seconds and the end trim point which we'll say is 18 seconds for the video that i'm using and lastly we'll need that video file i've decided that it's going to just be a relative path within the same folder as our project so i'll go ahead and i'll just drag mp.4 into our project so it's in the location that i'm expecting for our program well let's go back to our app.pi and start to implement our trim method we'll define our method above over here called trim it'll take in an in file an out file a start and an end for the trim the first thing that we're going to do is we're going to check for the existence of our out file if it does exist we're going to go ahead and delete it so it doesn't conflict with our ffm peg so all we do is check if it exists if it does exist we'll go ahead and just remove that out file and it'll be replaced when we do the trim before we continue with our trim let's go ahead and check out something called ffm probe what we're going to do is we're going to get our probe result we're going to get that from ffmpeg.probe and all we have to do is give it the file path then from this our probe result is going to be a dictionary with information in it and within that dictionary some information that we can grab is the duration so in order to grab the duration we're going to get our probe result we'll use the get method in case it doesn't exist and we're going to ask for our format and then if we don't get the format we're going to return an empty dictionary and then we're going to ask for the duration and if we don't get the duration we'll just return null and this result over here will be our duration so we'll set that to duration and we'll print that on the screen so let's go ahead and put a break point over here and we'll go ahead and run the application by using our launch just remove that breakpoint and click play now if we click on our duration over here we can see that it's 102.42 so we got our duration in seconds and if we take a look at our probe results we can learn a lot of information about our videos under the format over here we can find out the format name duration and much more information about our videos well let's go ahead and just rename our duration so it's a bit more of a clear name as we have an infile and an out file a better name for this would be in file duration one way to do that is you can use f2 on your keyboard or go ahead and click that and we'll just rename it to in file duration the first thing that we're going to do is we're going to go ahead and get an input stream so that's going to be equal to our ffmpeg dot input and all we have to do is give it the path to the file so that is our in file and then we can go ahead and start with our trim for our trim we're going to have to trim the video and the audio separately so let's go ahead and start with the video trim for the video trim we're going to take our input stream and we're going to call a method called trim we're going to give it the start is equal to our start of our trim that we passed in which is going to be 10 seconds in our situation here and we're going to give it the end which will be equal to the end that we pass in and then lastly we have to go ahead and call set pts pts stands for presentation timestamp the only real important thing to know is that we need to set the same value for our video and audio i'll include a more detailed description of pts inside of the description for the video but we're going to pass in pts here and since the same value we might as well create a variable so we'll call it pts and that's all we need for our video trim now we can go ahead and do our audio now i'm going to put this on multiple lines so i'm putting brackets and we're going to take our input stream and we're going to do a filter so we'll do a filter underscore and inside here we're going to take our a trim so this is our audio trim we're going to give it our start is equal to start and our end is equal to end and then we'll do one more filter and this filter is going to be for our pts just like our video except it's going to be a set pts for audio and we're going to set it to the same value as our video which is going to be the pts variable next we can go ahead and concatenate our video and audio stream so i'll go ahead and create a variable called video and audio and it's going to be equal to ffmpeg dot concat and freakin cat we're going to provide it with the video and the audio trims that we created over here and then we just need to provide two variables one is going to be v is equal to one and a is equal to one v is the number of output video streams the default value is one but we're going to explicitly set it to one here as well and a is the number of output audio streams the default is zero and we're going to go ahead and set it to one now we can go ahead and output our results so what we're going to do is we're going to create a variable called output and it's going to be equal to ffmpeg dot output and within output we're going to provide it with the video and audio concat that we just did and we'll also give it the out file so this is where we're going to see our result and the out file we said is going to be in a file called out.mp4 in the same directory and we're going to say that the format is equal to mp4 you don't have to provide the format since we have the extension already on our file but you can if you were reading this from a stream and you didn't have the extension this would be good to have and as i said the file is going to appear over here when we run it once we have this output over here we can go ahead and call a method on it called run now that's actually going to run it so we're creating our output over here and then we're calling run let's go ahead and run our application using our configuration over here we're going to run it and as it's running you can see that it's processing the file right over here and once it's done processing we can see that it's created an out dot mp4 if we go ahead and open up the out.mp4 we'll see that it's an 8 second video back within our code let's do one more thing we're going to go ahead and probe our out file and then take a look at the duration now remember i mentioned naming before well this is called probe result let's go ahead and just rename this over here so that's called in file probe result so we'll just do a rename once again and we'll just call it infile probe result then let's copy just these lines of code which are a great candidate for refactoring into a method but since a simple little application over here we're just going to leave it the way it is and then i'm going to use this feature called multiple cursors all i'm doing is selecting the word in and the underscore and then hitting command d on my keyboard to add an additional cursor for every match that it finds if you're on windows i believe it's ctrl d then i'm deleting the in typing out and that should work so let's go ahead and put a break point over here and to get rid of the multiple cursors just hit escape and we'll put a breakpoint over here we'll hit play and let it process the file and then once it hits the breakpoint we'll take a look at the time over here and we can see that the time says 8.0 however if we go to the video over here we can see that it's eight seconds so one thing to take note of is that after you do your trim your duration may be off by a little bit there are many factors that can affect this it could be the video frames and the audio not lining up perfectly or it could be dependent on the cpu of the system that's running this just take note that the values might not always match and if this does happen in your writing tests just make sure to write that fuzzy logic between the expected value and the actual value if you enjoyed this video please like subscribe and hit that notification bell
Info
Channel: Coding With Adam
Views: 11,907
Rating: undefined out of 5
Keywords: python, ffmpeg, python-ffmpeg, ffmpeg-python, trim video with ffmpeg, trim video with ptyhon, how to install ffmpeg, install ffmpeg mac, install ffmpeg windows, trim video and audio ffmpeg, no sound after trim ffpeg, trim video no sound ffmpeg, learn how to trim videos, automate trimming videos, code for trimming videos, splice video, splice video ffmpeg, ffmpeg to trim video, trimming using ffmpeg and puython, tutorial trim video ffmepg python, python trim vid, vid trim
Id: SGsJc1K5xj8
Channel Id: undefined
Length: 12min 48sec (768 seconds)
Published: Mon Jun 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.