python - 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={}
但我想保存具有特定名称的视频,movieIds
从movieClips_final.csv
.
你认为问题出在哪里?感谢您的反馈
解决方案
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'
推荐阅读
- java - Kafka Streams 处理器 API context.forward
- angular - 来自 Angular 服务的代码无法访问
- asp.net-identity - 带有 AspNetCore.Identity 的 IdentityServer4:使用 TokenClient 时未调用自定义 SignInManager
- android - React Native - 无法在 Android 的前台访问 Firebase 推送通知
- vb.net - 使用 textbox.text 中的值填写 PDF 表单并在填写 PDF 后打印 PDF
- php - WooCommerce If 语句
- sql - 日志表中当前行的 SQL 语句
- javascript - 在 js 类的异步函数中调用函数
- c# - EF Core 2.2.2 - 更新数据库失败,MySQL
- docker - 允许 docker swarm 服务绑定主机 ip 接口