首页 > 解决方案 > Python YouTube 下载器 TypeError:预期的字符串或类似字节的对象

问题描述

我正在使用以下脚本使用python youtube_dl.

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有 type int,这就是我使用str(row['movieId']). 如果我使用,我的下载器可以工作,ydl_opts={}但我想保存具有特定名称的视频,movieIdsmovieClips_final.csv.

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

标签: pythonyoutubeyoutube-api

解决方案


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'

推荐阅读