Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...