Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to build a discord music bot and am using youtube_dl for downloading the songs. I want to save them in a directory so I won't have to download them again every time. But I want to change the songs name in the directory.

I know that I'll have to change something in the outtmpl but this is just giving me the youtube name of the song:

ydl_opts = {
            'outtmpl': f'./project/audio/%(title)s.%(ext)s', #Output directory
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',

            }],
        }

        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
             ydl.download([url])

When I replaced the outtmpl with: 'outtmpl': f'./project/audio/{songname}', it gave me the error message: DownloadError: ERROR: audio conversion failed: file:mp3: Invalid argument

question from:https://stackoverflow.com/questions/65829739/python-youtube-dl-change-output-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

Try to print your outtmpl to make sure it contains what you expect. The PEP recommends using {variable} instead of %(ext)s in f-strings.

f'./project/audio/{title}.{ext}'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...