Before finding working Python code for the purpose of this post, I thought it would be easy to find working examples. But, it was not and that is the reason I write this post.
If I do not know how to use Python or other programming languages. I would have to repeatedly wait for a software to separate an audio clip from video clip.
Then, I should compile only audio files and wait for a long time.
But I would not do that. With the power of programming, we do not have to repeat what we already know.
Before you use the code below, please read these and install Python and pip first if you haven't yet.
The pip will be used to install and use Moviepy.
You can install it with
$pip install moviepy.
I will assume you already prepared a video file with audio for this process. It is very simple and you can use the code below with it.
# Create a file with # $touch extract_audio.py # 1. import sys from moviepy.editor import * video = VideoFileClip(sys.argv) # 2. audio = video.audio # 3. audio.write_audiofile(sys.argv) # 4.
You can save this code with extract_audio.py file and test it with the command below.
$python extract_audio.py <video_file> <audio_file>
$python extract_audio.py video_for_audio.mp4 audio_from_video.mp4
It will make audio_from_video.mp4 file in your machine.
What we do here are
We import default Python sys module to use sys.argv later and moviepy.editor to extract audio files from videos.
In video = VideoFileClip(sys.argv) you instruct your machine which video file you want to use to extract audio.
Then, with audio = video.audio, you select audio file inside the video file. You can see that Python use . syntax for that.
Lastly, with audio.write_audiofile(sys.argv), You end the process and assign the result audio file name.
and that is all you need to know to extract audio from the video with python.
The sys or sys.argv here is to help you automate your work with CLI params.
What comes first after $python file.py would be sys.argv and others would be sys.argv and more.
In this post, you learnt you don't need any software to extract an audio file from video and use Python instead.
Thanks and please share this post with others.