Convert Videos To MP3 with FFmpeg in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn how to convert video files to audio files in an automated way using ffmpack and python so let us get right into it [Music] all right so we're going to learn how to extract the audio from video files in an automated way how to convert video files to audio files in an automated way using ffmpeg and Python and for this of course you need to have ffmpack installed on your system now it is available for all operating systems the installation process is a little bit different on Windows I think you can either download a basic installer or you can install it using a package manager like choco I have a video on it if you want to check it out on Linux you just use your default package manager so on Debian based distribution sudo apt install ffmpack or on different distributions uh Pac-Man or dnf or whatever and on Mac I think it's quite similar you have probably some package manager that you can use to install ffmpack it is available for all operating systems now once you have ffmpack installed you can use it directly in the command line so it's essentially a tool that allows you to do a lot of different things with audio files with image files with video files and we can just use ffmpack here in the command line we can say okay the input is some video.mp4 then I want to remove the video I want to set a specific audio codec and a bunch of different flags so you can just go type ffmpeg help or you can say tldr if you have the tool ffmpeg to see what you can basically do with it but all of this stuff that we can do in the command line we can also do in Python using a module called sub process so we can just call the sub process module to utilize ffmpeg via python in order to automate it so that you don't have to write some fancy shell Scripts so what we're going to do now is we're going to create a new python file main.py and we're going to convert this video here which is just one of my videos um let me just open this in the file explorer here you go this is basically what is going on guys welcome to just an introduction uh for one of my videos and we're going to take that and we're going to extract the audio we're going to convert it to an MP3 file in Python using ffmpeg so the first thing we want to do is we want to import the sub process module and then we want to define a function convert to mp3 or maybe convert video to MP3 not four we want to have an input file and an output file now all we want to do now is we want to run an ffmpeg command so we're going to use subprocess to define what this FM ffmpack command is going to look like we're going to then run it and we're going to just coordinate this with python so it's very trivial very simple but very useful if you have to batch process a lot of videos or audio files or images or something like that so we're going to say here ffmpack underscore command or CMD equals and then we're going to pass a list and uh or we're going to create a list and this list is going to have the respective keywords the first one obviously FM pack itself the second one is going to be Dash I followed by an input file so the file name of the video in our case video. mov then we're going to say Dash VN to remove the video so to disable the video then we're going to say Dash 8 codec and we're going to choose an audio codec in my case here lip MP3 Lane like this then we're going to Define uh the bit rate I'm not an audio engineer those are just some default values that are reasonable to use um so this is the bit rate now 192k so 192 000. um then we're going to define the sampling rate which is going to be 44 100 I think it's Hertz not sure um and then we're going to just say Dash white overwrite files if they're already existing and then we're going to finally just provide the output file so this is the ffmpeg command we can also just run it here uh in the command line we don't have to use Python for that but this function now can be called um on different input files so we can say something like uh for example import OS and then we can say for a file in OS dot list directory and then we can just go through all the files and call this function all the files with a different output file we can just automate this in a very efficient way we don't have to constantly re-execute this command with different files so that is that what we're going to do now is we're going to say uh still in that function sorry this is just the definition we need to run this command sorry we need to run this command uh subprocess dot run we want to pass this ffmpeg command here we want to say check equals true and we're going to catch an exception which is going to be the subprocess dot called process error as e and we're going to print here in case that this does not work in case it fails we're going to say conversion failed like this and otherwise we want to say successfully convert it and that is basically all of the magic we now only need to call this so we're going to stay here convert video to MP3 video.mov io.mp3 and now I can run this here and you can see it worked so now I have the audio file I can open it up in files what is going on guys welcome back there you go you hear it worked and of course I can run this also in the command line so I can move to [Music] um to this directory here and I can say just Python 3 Main py and you can see successfully convert it and this is all the ffmpeg output now this right now as a single script is just this is nothing else but running this command there's nothing fancy about this so I can just go ahead and I can just type the same command ffmpeg Dash I video dot uh mov Dash VN Dash a codec um lip MP3 lame Dash a b192k dash AR 44100 Dash Y and then audio dot MP3 this is the exact same process but the difference is I can automate this now so I can now go ahead and I can iterate over files so if I have multiple files here maybe even though it's always the same file we can just copy this oh what did I do here no copy this paste this paste this paste this and maybe we can call those um video two then video three and video four and then we can use those we can automate this function so we can say something like four enumerate for enumerate or actually for I file name in enumerate OS dot list directory and hopefully I can just pick the current path here without a problem um let me just see what the output is for this one before we do anything else there you go seems to work and now we can say if file name dot ends with dot mob we want to take it and we want to call the function convert video to MP3 and we're going to then just say uh file name is the input and the output is going to be audio that's using fstring audio and then we want to have I dot MP3 something like this and uh that should actually be it so if I run this now you can see it creates all these audio files here um so this is quite useful of course you can make it more intelligent not just depend some enumeration here but uh put some more thought into the file name but this is how you can automate simple ffmpeg commands and you can use this of course for a bunch of different use cases with ffmpec you can combine images to a video to a gif you can take a gif turned into a video take a video turned into a gif you can extract audio you can do a bunch of different things you can change codecs you can do a lot of different things and you can all you can do all of this in Python and of course you can do all of this in Python for every tool it doesn't have to be ffmpeg you can just automate processes in general using this sub process module so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye
Info
Channel: NeuralNine
Views: 13,328
Rating: undefined out of 5
Keywords: video to audio, video to mp3, python video to mp3, ffmpeg, python ffmpeg, python ffmpeg automation, python extract audio from video, python transcribe video
Id: ucXTQ0V8qMA
Channel Id: undefined
Length: 10min 4sec (604 seconds)
Published: Tue May 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.