Python YouTube下载器TypeError:期望的字符串或类似字节的对象

时间:2018-05-16 13:57:25

标签: python youtube youtube-api

我使用以下脚本使用python youtube_dl从youtube下载大量视频。

from __future__ import unicode_literals
import youtube_dl
import pandas as pd

csv_file = 'movieClips_final.csv'
df = pd.read_csv(csv_file)

print(df.shape[0])
print(df.dtypes)

for index, row in df.iterrows():
    file_name = ['/Users/yashar/Documents/GitHub/video_utils/download_video_youTube_python/' + str(row['movieId']) + '.mp4']
    print(file_name)
    ydl_opts = {'outtmpl': file_name}

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
         ydl.download(['https://www.youtube.com/watch?v=' + row['youID']])

这是我收到的错误:

 (YT_download) d1:download_video_youTube_python yashar$ python   download_videos_YT.py
 6952
 movieId              int64
 title               object
 YTId                object
 Movie_clip_title    object
 youID               object
 dtype: object
         ['/Users/yashar/Documents/GitHub/video_utils/download_video_youTube_python/94.mp4']
 [youtube] 4K8M2EVnoKc: Downloading webpage
 [youtube] 4K8M2EVnoKc: Downloading video info webpage
 [youtube] 4K8M2EVnoKc: Extracting video information
 Traceback (most recent call last):
 File "download_videos_YT.py", line 17, in <module>
 ydl.download(['https://www.youtube.com/watch?v=' + row['youID']])
 File "/Users/yashar/miniconda3/envs/YT_download/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 2001, in download
url, force_generic_extractor=self.params.get('force_generic_extractor', False))
 File "/Users/yashar/miniconda3/envs/YT_download/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 803, in extract_info
 return self.process_ie_result(ie_result, download, extra_info)
 File "/Users/yashar/miniconda3/envs/YT_download/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 857, in process_ie_result
return self.process_video_result(ie_result, download=download)
 File "/Users/yashar/miniconda3/envs/YT_download/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 1635, in process_video_result
self.process_info(new_info)
File "/Users/yashar/miniconda3/envs/YT_download/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 1713, in process_info
info_dict['_filename'] = filename = self.prepare_filename(info_dict)
File "/Users/yashar/miniconda3/envs/YT_download/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 665, in prepare_filename
mobj = re.search(FIELD_SIZE_COMPAT_RE, outtmpl)
File "/Users/yashar/miniconda3/envs/YT_download/lib/python3.6/re.py", line 182, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

movieId的类型为int,这就是我使用str(row['movieId'])的原因。如果我使用ydl_opts={},我的下载程序会有效,但我想保存从movieIds获取的具有特定名称movieClips_final.csv的视频。

您认为问题在哪里?感谢您的反馈

1 个答案:

答案 0 :(得分:0)

outtmpl需要一个字符串,但你有:

file_name = ['/Users/yashar/Documents/GitHub/video_utils/download_video_youTube_python/' + str(row['movieId']) + '.mp4']

创建一个以文件名作为第一个元素的列表。

所以我想你想要:

file_name = '/Users/yashar/Documents/GitHub/video_utils/download_video_youTube_python/' + str(row['movieId']) + '.mp4'