summaryrefslogtreecommitdiff
path: root/python/ytdl/download_audio.py
blob: 5f6bd74b8735bcd49fc19cb2fb8bd1e3d175db68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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")