Skip to content

Commit ad6d0ea

Browse files
committed
add video to audio conversion tutorial
1 parent c2b16d5 commit ad6d0ea

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9494
- [Asynchronous Tasks with Celery in Python](https://www.thepythoncode.com/article/async-tasks-with-celery-redis-and-flask-in-python). ([code](https://github.com/bassemmarji/flask_sync_async))
9595
- [How to Change Text Color in Python](https://www.thepythoncode.com/article/change-text-color-in-python). ([code](general/printing-in-colors))
9696
- [How to Create a Watchdog in Python](https://www.thepythoncode.com/article/create-a-watchdog-in-python). ([code](general/directory-watcher))
97+
- [How to Extract Audio from Video in Python](https://www.thepythoncode.com/article/extract-audio-from-video-in-python). ([code](general/video-to-audio-converter))
9798

9899

99100
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Extract Audio from Video in Python](https://www.thepythoncode.com/article/extract-audio-from-video-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
moviepy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import subprocess
2+
import os
3+
import sys
4+
5+
def convert_video_to_audio_ffmpeg(video_file, output_ext="mp3"):
6+
"""Converts video to audio directly using `ffmpeg` command
7+
with the help of subprocess module"""
8+
filename, ext = os.path.splitext(video_file)
9+
subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
10+
stdout=subprocess.DEVNULL,
11+
stderr=subprocess.STDOUT)
12+
13+
14+
if __name__ == "__main__":
15+
vf = sys.argv[1]
16+
convert_video_to_audio_ffmpeg(vf)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import sys
3+
from moviepy.editor import VideoFileClip
4+
5+
6+
def convert_video_to_audio_moviepy(video_file, output_ext="mp3"):
7+
"""Converts video to audio using MoviePy library
8+
that uses `ffmpeg` under the hood"""
9+
filename, ext = os.path.splitext(video_file)
10+
clip = VideoFileClip(video_file)
11+
clip.audio.write_audiofile(f"{filename}.{output_ext}")
12+
13+
14+
if __name__ == "__main__":
15+
vf = sys.argv[1]
16+
convert_video_to_audio_moviepy(vf)
604 KB
Binary file not shown.

0 commit comments

Comments
 (0)