How to remove the silent parts of a video using ffmpeg and python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how y'all doing dial back again with another video about doing some automated video editing and I got something pretty freaking need to show you today so I was messing around with this Python library that someone told me about and read it and through a combination of using ffmpeg directly in that library I have managed to even though it's in a pretty rough stay as it is right a little Python scripts that when used in tandem with a file generated from another script that I have actually will cut up the recorded the video into the sections that don't have silence I guess what it was been flagged a silence and basically combines it together into a version that has all the silence removed is essentially removing one of the most from my monotonous you have to do while you're being read before we get started you haven't scrubbed in my channel and you've been enjoying my content be sure to subscribe so you find out whenever I post more videos and with that let me show you the goods so we over here and this is actually have a two phase approach and it's not actually just using straight shell this time I actually was as I mentioned I'm using a Python library for part of this so the first step of this is we need to actually detect when the video has silence in it now there's actually a filter and f of MPEG to do this called I think it's called silence remove problem is it only works on the audio not the video so if you just ran silence remove it would you would just get basically like it would be decent it wouldn't make any sense so the other way to do that is there's a filter called silence detect which just outputs whenever it detects silence and at the tip it outputs what the starting and ending time stamps were for the silence now I have formatted a very particular script that runs a command and sends it through a bunch of pipes to my fat face on the way there guy sends it through a bunch of pipes to essentially get only the ending timestamp and the duration and I will explain why I'm not directly capturing the starting timestamp and that's essentially because the starting timestamp is just the ending timestamp minus the duration I'll get you to anyway so you're looking at this command and I'm basically good just trying to hi I'm ignoring the video stream because that just makes it take longer and it's completely relevant to detecting the silence and I'm passing it through the silence detect filter my compositor is losing his mind go away thank you and so here we're path we're passing in a threshold that it's using detect when silence is actually like when when audio is dropped below a level to because their silence it's in decibels and I'm doc I'll just pass in like negative 30 negative 40 something like that and then for the duration it's how long the audio has to stay below that level to be considered silence I've had some issues with this being anything less than 1 and I'll explain why that is when I get to the Python part but just know that I've basically been having this set to no less than 1 here I'm basically saying don't write this output out anywhere just kind of throw it away and then I'm piping the output of this a standard out and I'm also ignoring a bunch of the other like error message justice thought they would have and then we're we're using grip to specifically find the section to the output that have the term silence end because this is the lines of the output that will have the timestamps that I want and then we're passing it through this awk command to basic basically get only the specific values that I want which is these two which would be the ending timestamp and the duration that I'm putting in a file so what does this let's see what this looks like without me putting it through the end of the file so I'm in this directory where I've been doing a bunch of little experimenting let me echo out that script real quick the tag silence no don't echo out that cat like not you come on so we're just gonna copy this real quick and we wanna run it and see what it looks like or don't all right duh I have I have variables here Donna you genius let's say this is 1 negative a 30 yeah that worked here where it pets in eight was I call this test III recorded like this little test recording to try this stuff on and so what is this there were two instances where it picked up silence start one starting at two seconds I'd like approximately two and a half seconds and then the overall duration of the silence was 1.0 two seconds so that means to get the time of when the silence started you just take the second number which would be this one yeah and you just subtract it from this and there was a second one at about eleven seconds lasted 1.2 seconds so again to get the starting timestamp but you would just take this number and subtract this number say that that's how you would get it so I'm as you saw I am piping this these numbers out to the file which I think I call it silence yeah there's my Silas file now what am i using this for so now we're gonna go to the Python portion of this so let me go up here okay so I am passing in a couple things I am passing in the file that I want to run this on which would be the thing I just ran the silence detection on I'll probably write some scripts at some point that just wraps both of these things in one go what I want to write new file out to the path to the file I wrote the time stamps out too and this is something I'm gonna mess with later because I noticed depending on how sensitive I was having the sounds that section be the sometimes when it was combining the cliffs that had odd not non silence together it was kind of abrupt almost kind of like obtrusive like all of a sudden like I'd be talking and also I would just cut to middle but another like another section of speaking was kind of weird so later I'm gonna see if I can kind of mess with like padding the start of it with an extra like couples 0.2 seconds or something to make it not so abrupt I had this minimum duration here I'll tell you why and this is also related to why I haven't really had good results with that Silas detection lower than a second it seems like if I'm trying to concatenate clips that the duration of the clip is less than a second the this doesn't work it might be the encoding format that I'm using there could be a couple reasons but so far just in my general like testing if I try to concatenate clips that are less than a second it just doesn't work this is both using the Python library er that I'm using up here which is called movie PI and also didn't work when I just straight-up tried this where I was just generating at the MPEG commands at a bash script that is doesn't work this I'm grabbing the actually I think this is left over whenever I was trying to do it the other way where others generating commands I don't even need this anymore so let's get down to the main bulk of this so here I'm just tracking the number of clips app generated and this last variable is used to store what the ending timestamp was of the last section of silence so that it knows when to start the next clip that has audio that's that's the important part so here we're just opening up the file that has the silenced timestamps in read mode here this is from the boovie pi library we're creating this thing called a video file clip that allows us to kind of do some ffmpeg ish things too it's kind of is kind of a it does a lot of abstraction over if of MPEG I'm grabbing the duration of that media of that video file for a kind of part that hold minimum duration sanity check and I'm creating a list for just all the clips so what we do is we iterate over yet silence file it was all the songs timestamps and as soon as we don't hit anything we just we break out the loop and I you notice they remember there was - there was two values there there was the ending timestamp and the duration so I am stripping the newline off of it and I'm splitting them by the space in between they gave me the two the two values the end the duration so the time stamp I need to use to kind of create this sub clip that has the that has the audio I need to calculate the [Music] starting yeah starting timestamp so that I use a combination of the last good timestamp and this one it's kind of using a middle part make more sense as I get through it so start I am setting to the last good timestamp and then the clip duration I'm calculating from this value which was a fear call I said that the end minus the duration was the start of the silence so I just want to see if I whatever the starting time stamp is of the silence if I subtract the where the Clips gonna start at if it's less than a second that's basically what all these things are down here or is this sandy checks to make sure this shit's gonna be lessen this is going to be more than a second cuz again from a couple of trial and errors I noticed that if any of these clips were less than the second this ship does it didn't work this is left everything me doing with the e stuff and I'll get back to that later okay so this is where I actually take I use a method on the video file with a video clip file class whatever the hell was from that library and I'm creating a sub clip where the sub clip starts at the last good timestamp and goes to the basically where the silence started right and then I'm adding it to that list of clips and then I'm setting the last good timestamp to be the end of this current section of silence so that makes any sense so when I get to a section of silence whatever the last good timestamp was I want to basically take it make a clip starting from the last good timestamp up to where the silence starts so basically we're skipping over this section that had silence but that makes sense and then I'm just including decreasing the count by one and then after that it loops over the entire thing and I finally get to the end and user there's like one clip left over that has audio but again I'm trying to do it another Sandi check to make sure that that clip will be longer than a second cuz if again not breaks and then I call this method that's in that library called concave eight video clips and this is what takes all those individual clips I just extracted and then recombines them together in the one video stream and then finally I call this right video file method where I'm passing in the path of the file that I'm saving it to FPS I'm just more concerned with testing so just screw the compression just write it out super fast I still want to use the same encoding and I want to use the same codec as my recording is and then once that's done I close the file I close the video and we should be done so that was quite a bit what exactly does this look like so let's look at the unprocessed one I'm not sure if you will hear audio or not we'll find out so this is me talking I think you don't hear me actually don't know why he knows I intentionally stopped talking at certain parts to like let the sounds detection actually trigger and that trigger you notice that whole video was about 15 seconds so when I actually already ran this on it actually I'll just go ahead run it cuz this doesn't take that long because it's not very long so it's a where'd you go okay Python process test no ease so I should run this and you'll see it's generating the clips now it's writing out the concatenated video file to a new file and it's called it's step path I gave which is that like process test no TEM KB and what you'll see is when this is done there is a route one two three clips so there should be what about two or three jumps that you'll notice where it skipped over where I intentionally stop talking and we're gonna run that one as soon as I find it again okay jump on keeps talking keeps talking jumps yeah so there's two jumps where the silence was just removed now that wasn't too noticeable in that video but if you have a video where especially the way I pin the talk like a battling sometimes and there's multiple sections where my brain does decides to just take a and not work for extended periods of time this is great I plan to leverage this very heavily in one of those if I do any more like rant videos this is gonna be the so that's all I had for my little experiment with moving of automatically trimming the silent parts out of videos I'm gonna keep refining this as much as I can so that I can do maybe some more interesting things with it such as I don't know maybe I want to do the easy and stuff too because I wasn't as prevalent in that example but when I tried this on some other recordings sometimes when it would jump like I said it would almost jump like right on to me talking again it was kind of like Oh bud yeah I need to try to find a way to kind of like ease the transitions between when it cuts in but I also still have to do it so that the clip is longer than a second as like I said if if I try to do this and any of the clips you lessen a second it just destroys it just there's a ship it doesn't work at all if you feel like the video drop it like subscribe share if someone else would like to see this if you would like to support the channel I have some links down below that you can use do that very greatly very greatly appreciated if you are interested in following me on other platforms I have some social links down there for like Twitter and I have a discord link down there you can join too I think that's all I have for this video y'all come on back down I'll see you next time [Music]
Info
Channel: Donald Feury
Views: 5,726
Rating: undefined out of 5
Keywords: ffmpeg remove silence, trim silence from video, donald feury ffmpeg, donald feury, programming, ffmpeg, feurious, spotifyd, spotify-tui, spotify tui, linux, tutorial, how to, how to use, ffmpeg tutorial, quickstart, quick, start, basics, fundamentals, getting started, conversion, filters, effects, ffmpeg help, video, audio, donald feury ffmpeg tricks, automated video editing, donald fuery video editing, moviepy, python, python moviepy, Remove silence from video, ffmpeg python, Python ffmpeg
Id: ak52RXKfDw8
Channel Id: undefined
Length: 18min 19sec (1099 seconds)
Published: Fri May 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.