Podcasts have become a popular medium for consuming content, but sometimes the material you want to listen to is in video format on YouTube. Converting these videos to podcasts allows you to enjoy them on the go. In this blog post, I’ll walk you through a simple Python script that downloads a YouTube video, extracts the audio, and plays it on your default media player.
Prerequisites
Before we start, you’ll need to install a few Python libraries. Open your terminal and run:
pip install pytube moviepy
The pytube library is used to download YouTube videos, and moviepy helps in converting video files to audio.
The Script
Here’s the complete Python script to convert a YouTube video to an MP3 podcast and play it automatically:
from pytube import YouTube
from moviepy.editor import VideoFileClip
import os
import subprocess
import sys
def download_youtube_video(url, output_path="videos"):
# Create output directory if it doesn't exist
if not os.path.exists(output_path):
os.makedirs(output_path)
yt = YouTube(url)
video = yt.streams.filter(progressive=True, file_extension='mp4').first()
output_file = video.download(output_path)
return output_file
def convert_video_to_audio(video_path, output_path="audios"):
if not os.path.exists(output_path):
os.makedirs(output_path)
video = VideoFileClip(video_path)
audio_path = os.path.join(output_path, os.path.splitext(os.path.basename(video_path))[0] + ".mp3")
video.audio.write_audiofile(audio_path)
return audio_path
def play_audio(audio_path):
if sys.platform == "win32":
os.startfile(audio_path)
elif sys.platform == "darwin":
subprocess.call(["open", audio_path])
else:
subprocess.call(["xdg-open", audio_path])
def main():
youtube_url = input("Enter YouTube video URL: ")
print("Downloading video...")
video_path = download_youtube_video(youtube_url)
print(f"Video downloaded to {video_path}")
print("Converting video to audio...")
audio_path = convert_video_to_audio(video_path)
print(f"Audio saved to {audio_path}")
print("Playing audio...")
play_audio(audio_path)
if __name__ == "__main__":
main()
How It Works
- Download the YouTube Video:
• The download_youtube_video
function takes the YouTube URL and downloads the video. We filter the streams to get a progressive stream (which includes both video and audio) with an MP4 extension.
- Convert Video to Audio:
• The convert_video_to_audio
function uses moviepy to convert the downloaded video file to an MP3 audio file.
- Play the Audio:
• The play_audio function uses platform-specific commands to play the MP3 file using the default media player on Windows, macOS, or Linux.
- Main Function:
• The main function prompts you to enter a YouTube URL, downloads the video, converts it to audio, and then plays the audio file.
Conclusion
This script provides a simple way to convert YouTube videos to audio files that can be used as podcasts. Whether you want to listen to lectures, interviews, or any other content available on YouTube, this method allows you to convert and enjoy them in a more portable audio format. Happy listening!
Top comments (0)