Python offers easy implementations to work with different kind of data files. Images and video files processing has become very easy using open source libraries. In this tutorial, we will extract audio from any video files.
MoviePy is a open source package used for different video processing tasks. It helps to extract or merge audio with video, crop video or perform different animations. It is very easy to install using pip or you can build from git.
pip install moviepy
Now, we can ready any video file and extract audio data.
from moviepy.editor import VideoFileClip # Read video file videoclip = VideoFileClip("test.mp4") # Extract audio from video audio = videoclip.audio
We can get stats or perform different operations to audio before saving it. First we can get length of audio file in seconds or number of channels etc.
# Audio length audio.duration # Number of channels audio.nchannels
We can also get a sub clip from file and then we can save file in a audio format.
sub_clip = audio.subclip(10, 40) # Value is in seconds sub_clip.write_audiofile("test.mp3") # close videoclip.close()
Moviepy offers different other operations to perform on audio and video files which can help create amazing outputs. For more details, check moviepy github page and their getting started guides.