I have some videos from which I would like to extract the audio and save it as MP3. I could do that manually with VLC media player, but there is a much better solution in Python.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Install MoviePy
MoviePy is a Python module for video editing, which we can use to work with video files and modify them as we need. You can install MoviePy with this command:
1 |
pip install moviepy |
Extract the audio
We need to import the video into MoviePy. From there we can access the audio and write it into a new MP3 file:
1 2 3 4 5 6 7 |
import moviepy import moviepy.editor video = moviepy.editor.VideoFileClip("video.mp4") audio = video.audio audio.write_audiofile("only_audio.mp3") |
Conclusion
MoviePy is a powerful module that allows us to turn MP4 into MP3 with just a few lines of code. If you are interested in working with videos, you should check official documentation of MoviePy to see what else you can do.