Skip to content

Commit

Permalink
add video to audio conversion tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
x4nth055 committed Sep 26, 2021
1 parent c2b16d5 commit ad6d0ea
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
- [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))
- [How to Change Text Color in Python](https://www.thepythoncode.com/article/change-text-color-in-python). ([code](general/printing-in-colors))
- [How to Create a Watchdog in Python](https://www.thepythoncode.com/article/create-a-watchdog-in-python). ([code](general/directory-watcher))
- [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))


- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
Expand Down
1 change: 1 addition & 0 deletions general/video-to-audio-converter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# [How to Extract Audio from Video in Python](https://www.thepythoncode.com/article/extract-audio-from-video-in-python)
1 change: 1 addition & 0 deletions general/video-to-audio-converter/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
moviepy
16 changes: 16 additions & 0 deletions general/video-to-audio-converter/video2audio_ffmpeg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import subprocess
import os
import sys

def convert_video_to_audio_ffmpeg(video_file, output_ext="mp3"):
"""Converts video to audio directly using `ffmpeg` command
with the help of subprocess module"""
filename, ext = os.path.splitext(video_file)
subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)


if __name__ == "__main__":
vf = sys.argv[1]
convert_video_to_audio_ffmpeg(vf)
16 changes: 16 additions & 0 deletions general/video-to-audio-converter/video2audio_moviepy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import sys
from moviepy.editor import VideoFileClip


def convert_video_to_audio_moviepy(video_file, output_ext="mp3"):
"""Converts video to audio using MoviePy library
that uses `ffmpeg` under the hood"""
filename, ext = os.path.splitext(video_file)
clip = VideoFileClip(video_file)
clip.audio.write_audiofile(f"{filename}.{output_ext}")


if __name__ == "__main__":
vf = sys.argv[1]
convert_video_to_audio_moviepy(vf)
Binary file added general/video-to-audio-converter/zoo.webm
Binary file not shown.

0 comments on commit ad6d0ea

Please sign in to comment.