Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

discord.py - Python youtube_dl change output name

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...