【问题标题】:PyTube downloads audio file in mp4 format. How to fix?PyTube 以 mp4 格式下载音频文件。怎么修?
【发布时间】:2021-12-02 11:47:35
【问题描述】:

下午好。我是python的初学者。所以,我试图用 PyTube 制作 YouTube 视频/音频下载器(仅用于教育目的)。我在 youtube 上看过很多视频,我正在努力让这个工具变得更好。所以我添加了视频/音频选择选项和恢复/质量选择选项。好消息是我已经成功制作了视频下载器。但是我在音频下载器中遇到了问题。问题是,PyTube downloads the audio file in MP4 format。我在 google 和 youtube 上搜索过。但我找不到任何解决方案。我想将 mp4 重命名为 mp3(因为文件没问题,但格式不对)。作为初学者,我不知道如何将下载文件保存在其他地方(临时文件夹),然后重命名然后将其传输到输出文件夹。我尝试添加filename=link.title+'mp3'。但它返回此错误:OSError: [Errno 22] Invalid argument: 'G:/Downloaded Videos/Latest English Ringtone | Turkish Bgm Ringtone 2021 | Bad Boy | Attitude Tone | Villain Ringtone.mp3'
这是我的代码:

from pytube import YouTube
link='https://www.youtube.com/watch?v=KrhPrPK2owA'
link=YouTube(link)
print('Title:',link.title+'\n'+'Views:',link.views)
streams=link.streams.filter(type='audio')
kbps_list=[]
itag_list=[]
print('Available Kbps: ',end='')
for s in streams:
    i=s.itag
    s=s.abr
    if s not in kbps_list:
        kbps_list.append(s)
        itag_list.append(i)
        print(s,end=' ')
reso=input('\nEnter Kbps to download: ')
if reso not in kbps_list:
    print('This Kbps is not available')
    from sys import exit
    exit()
reso=kbps_list.index(reso)
final=streams.get_by_itag(itag_list[reso])
print('Downloading...')
final.download('G:/Downloaded Videos/')
# final.download('G:/Downloaded Videos/', filename=link.title+'mp3')======================
# if I add custom filename, It returns the error ========================================
print('Successfully Downloaded!')

【问题讨论】:

    标签: python-3.x format pytube


    【解决方案1】:

    需要设置文件名,并且可以按照您的尝试以 mp3 格式下载,但它仍会在 mp4a 编解码器中。不确定这对你是否重要。 你的标题很奇怪,它包括文件路径。可能是为什么 link.title 不起作用。试试下面的代码去掉标题和文件路径。

    import os
    
    head tail = os.path.split(link.title)
    final=streams.get_by_itag(itag_list[reso]).download(filename=tail.strip(" | ") + ".mp3")
    

    您还可以通过传递参数output_path="some location" 来设置输出目录。

    看起来像:

    final=streams.get_by_itag(itag_list[reso]).download(output_path="some location", filename=tail.strip(" | ") + ".mp3")
    

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2018-08-18
      • 1970-01-01
      • 2018-05-05
      • 2020-08-03
      • 2022-01-09
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      相关资源
      最近更新 更多