如何使用Python重命名为mp3的音频文件扩展

发布于 2025-01-23 21:35:00 字数 1307 浏览 3 评论 0 原文

目前,我正在尝试根据艺术家的名字和歌曲标题下载YouTube音乐视频作为音频文件,并在下载了我试图从.webm或.mp4扩展名中重命名所有音频文件的所有视频之后。但是看来我面临一些错误将文件名和扩展名更改为.mp3。我的代码基本上没有重命名音频文件的名称和扩展名。有人可以帮助我将下载的文件重命名为.mp文件吗?

这是我的代码.........

import os
from requests import get
from yt_dlp import YoutubeDL

YDL_OPTIONS = {'format' : 'bestaudio/best', 'noplaylist' : 'True'}
filename=[]
old=[]
positions=[]
def search(arg, index):
  with YoutubeDL(YDL_OPTIONS) as ydl:
    try:
      get(arg)
    except:
      song =   ydl.extract_info(f"ytsearch:{arg}", download=True)['entries'][0]
    else:
      song =   ydl.extract_info(arg, download=True)

oldpath = song['title'] + '[' + song['id'] + ']' + '.' + song['ext'] 
old.append(oldpath)
newpath = f"{song['title']}.mp3"
if os.path.isfile(oldpath):
  os.rename(f"{oldpath}",newpath)
  filename.append(str(newpath))
else:
  filename.append(str(newpath))
  positions.append(index)


return filename , positions, old


title = (dataset['Title'] + " " + dataset['Artist']).tolist()
for index, arg in enumerate(title):
   filename, positions, old = search(arg, index=index )

这里oldpath =最初以此格式下载文件 newPath =我想重命名哪种格式 标题包含歌曲标题和艺术家名称(如:Fally -Harry Styles)。还有另一件事是,如果我在那里查看文件名,它成功地显示了所有音频文件名称,但是如果我尝试使用更名的名称和.mp3运行音频文件,则会给我这个错误“ filenotfounderror:[errno 2]没有这样的文件或目录:“周末 - 盲灯(官​​方视频).mp3'”

currently I'm trying to download youtube music videos as audio files based on artists name and song titles and after downloading all the videos I'm trying to rename all the audio files from .webm or .mp4 extension to .mp3. But it seems I'm facing some errors to change the file names and extensions to .mp3. My code basically doesn't rename the name and extensions of the audio files. Can anybody help me to rename the downloaded files as .mp file ?

Here is my code .........

import os
from requests import get
from yt_dlp import YoutubeDL

YDL_OPTIONS = {'format' : 'bestaudio/best', 'noplaylist' : 'True'}
filename=[]
old=[]
positions=[]
def search(arg, index):
  with YoutubeDL(YDL_OPTIONS) as ydl:
    try:
      get(arg)
    except:
      song =   ydl.extract_info(f"ytsearch:{arg}", download=True)['entries'][0]
    else:
      song =   ydl.extract_info(arg, download=True)

oldpath = song['title'] + '[' + song['id'] + ']' + '.' + song['ext'] 
old.append(oldpath)
newpath = f"{song['title']}.mp3"
if os.path.isfile(oldpath):
  os.rename(f"{oldpath}",newpath)
  filename.append(str(newpath))
else:
  filename.append(str(newpath))
  positions.append(index)


return filename , positions, old


title = (dataset['Title'] + " " + dataset['Artist']).tolist()
for index, arg in enumerate(title):
   filename, positions, old = search(arg, index=index )

Here oldpath = originally file downloaded with this format
newpath = to which format i want to rename it
title contains song title and artist name, (like: Falling - Harry Styles). There is another thing that is if i check filename there it successfully shows all audio files names with .mp3, but if i try to run the audio file with renamed name and .mp3, it gives me this error "FileNotFoundError: [Errno 2] No such file or directory: 'The Weeknd - Blinding Lights (Official Video).mp3'"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2025-01-30 21:35:00

您确定文件正在下载到预期的位置吗?假设您的文件名正确,另一个选项是目录不正确。

参数 docs> docs YouTube-DL 有关详细信息。

这是一个例子:

ydl_options = {
    'outtmpl': '~/Music/%(extractor_key)s/%(extractor)s-%(id)s-%(title)s.%(ext)s'
}

ydl = yt_dlp.YoutubeDL(ydl_opts)

Are you certain the files are being downloaded to the intended location? Assuming you got the filename correct, the other option is that the directory is incorrect.

Parameter outtmpl allows you to define a template for a file name and path. Check the docs of the original youtube-dl for the details.

Here's an example:

ydl_options = {
    'outtmpl': '~/Music/%(extractor_key)s/%(extractor)s-%(id)s-%(title)s.%(ext)s'
}

ydl = yt_dlp.YoutubeDL(ydl_opts)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文