【问题标题】:using subprocess for the youtube-dl for automatic song list downloading使用 youtube-dl 的子进程进行自动歌曲列表下载
【发布时间】:2023-04-01 23:35:02
【问题描述】:

从下面的代码中,我想从谷歌搜索引擎中查询包含歌曲名称的每一行(文件“playlist.txt”中的一行),然后根据结果我正在使用 youtube 链接并使用youtube-dl,我正在提取音频。

当我运行结果包含的链接时,GOOGLE 会阻止声称来自我的计算机系统的异常流量。任何方法来应对这种情况

我使用睡眠来保持请求之间的时间间隔。

我想知道如何纠正这个错误

我认为更改用户代理或使用代理可能有助于避免自动搜索检测。所以,我想知道如何更改我的代码来实现这一点

import urllib
import json as m_json
import re
import time
import subprocess
from random import randint
import getpass
playlist=open('playlist.txt','r')
songs = playlist.readlines()
song_num = 1
for song in songs:
   query = song
   query = urllib.urlencode ( { 'q' : query } )
   response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
   json = m_json.loads ( response )
   results = json [ 'responseData' ] [ 'results' ]
   for result in results:
       title = result['title']
       url = result['url']
       if re.search(r'www.youtube.com',url):
           print ( title + '; ' + url )
           print "DOWNLOADING",title
           decoded_url=urllib.unquote(url).decode('utf8')
           print decoded_url
           subprocess.call(['youtube-dl','-o','/home/'+getpass.getuser()+'/Videos/playlist%('+title+").(ext)s","--extract-audio","--audio-format","mp3",decoded_url])
           break;
   print song_num
   time.sleep(randint(10,15))
   song_num+=1

输出

DOWNLOADING <b>Black Sabbath Iron Man</b> - YouTube
http://www.youtube.com/watch?v=rT4KpfiFcNc
[youtube] rT4KpfiFcNc: Downloading webpage
[youtube] rT4KpfiFcNc: Extracting video information
[youtube] rT4KpfiFcNc: Downloading DASH manifest
ERROR: Error in output template: unsupported format character '(' (0x28) at index 73 (encoding: 'UTF-8')

【问题讨论】:

  • 当你运行这个时你的终端输出是什么?
  • @Lawrence 我加了!文件中每首歌曲(或每行)的相同输出
  • 感谢您尝试通过 python 执行此操作,但仅供参考,您可以通过单个 youtube-dl 命令行使用gvsearch--match-title--reject-title 选项。

标签: python json youtube subprocess youtube-dl


【解决方案1】:

您的url 格式不正确。如果您仔细观察,您会发现:

http://www.youtube.com/watch%3Fv%3DrT4KpfiFcNc

但正确的 Youtube URL 格式是:

https://www.youtube.com/watch?v=rT4KpfiFcNc

您应该在调用youtube-dl 之前尝试解码URL。像这样的:

url=urllib.unquote(result['url']).decode('utf8')

另外,您的输出格式无效;你想要的只是

'-o', '~/Videos/playlist%(title)s.(ext)s"

%('+title+"). 正在开发 youtube-dl 以查找 以标题命名的属性,然后启动,在 ) 之后缺少一个 s

【讨论】:

  • 这工作得很好,但我遇到了一个新错误。 youtube.com/watch%3Fv%3DrT4KpfiFcNc 下载 黑色安息日钢铁侠 - YouTube [youtube] rT4KpfiFcNc:下载网页 [youtube] rT4KpfiFcNc:提取视频信息 [youtube] rT4KpfiFcNc:下载 DASH 清单错误:输出模板错误:不支持的格式字符'(' (0x28) at index 73 (encoding: 'UTF-8') 1
  • 解码后如果我使用该链接从终端使用 youtube-dl 下载它工作正常!但不是通过脚本
  • 请更新您的问题并包含新的终端输出,完成。
  • 我是这个论坛的新手!谢谢你的建议@Lawrence
  • 如何随机化用户代理和代理服务器列表以避免自动搜索检测
猜你喜欢
  • 2018-07-03
  • 2014-03-02
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 2017-04-24
  • 1970-01-01
相关资源
最近更新 更多