Files
misc/python/ytdl/download_audio.py
2025-11-15 17:18:13 -08:00

34 lines
908 B
Python

import yt_dlp
import os
def download_and_convert_to_opus(url, output_filename):
# Use yt-dlp to download the video with audio only
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s.%(ext)s', # Save as a temporary file
'restrictfilenames': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
temp_file = ydl.prepare_filename(info_dict)
# Convert to OPUS using FFmpeg
ffmpeg_command = [
'ffmpeg',
'-i', temp_file,
'-c:a', 'libopus',
'-b:a', '128k',
output_filename
]
os.system(' '.join(ffmpeg_command))
# Clean up the temporary file
os.remove(temp_file)
# Example usage:
url = input("input video url: ")
filename = input("filename for ripped audio: ")
download_and_convert_to_opus(url, filename + ".opus")