gpt4 book ai didi

python - Youtube-dl 订阅 mp3

转载 作者:行者123 更新时间:2023-12-05 07:00:25 26 4
gpt4 key购买 nike

所以我的目标是编写代码,以便它可以自动从我订阅的所有 Youtube channel 下载到 mp3 文件。我很难处理 EO 错误,这对我来说并不清楚,因此我从来没有处理过它,我已经做了研究,但没有什么能帮助我,所以这里是代码:

import opml
import feedparser
import youtube_dl
from glob import glob
from pprint import pprint

from time import time, mktime, strptime
from datetime import datetime

if len(glob('last.txt')) == 0:
f = open ('last.txt' , 'w')
f.write(str(time()))
print('Initialized last.txt file for timestamp')
f.close()
else:
f = open('last.txt' , 'r')
content = f.read()
f.close()

outline = opml.parse('subs.xml')

ptime = datetime.utcfromtimestamp(float(content))
ftime = time()
urls = []
for i in range(0,len(outline[0])):
urls.append(outline[0][i].xmlUrl)
print(urls)

videos = []
for i in range(0,len(urls)):
print('Parsing through channel '+str(i+1)+' out of '+str(len(urls)), end='\r')
feed = feedparser.parse(urls[i])
for j in range(0,len(feed['items'])):
timef = feed['items'][j]['published_parsed']
dt = datetime.fromtimestamp(mktime(timef))
if dt > ptime:
videos.append(feed['items'][j]['link'])

if len(videos) == 0:
print('Sorry, no new video found')
else:
print(str(len(videos))+' bew vudeis found')

ydl_options = {
'ignoreerrors' : True,
'format': 'bestaudio[filesize<30]',
'keepvideo': False,
'outtmpl': 'filename',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'audioquality': '0',
'preferredquality': '320',
}]
}

with youtube_dl.YoutubeDL(ydl_options) as ydl:
ydl.download(videos)

我尝试了新的 YoutubeManager subs.xml ,尝试了其他具有不同 channel 的 Youtube 帐户,但他们的 subs.xml 没有任何帮助。

这是我的错误输出

runfile('C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py', wdir='C:/Users/sound/Desktop/PythonProjets')
Traceback (most recent call last):

File "<ipython-input-1-ff8a84b96d09>", line 1, in <module>
runfile('C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py', wdir='C:/Users/sound/Desktop/PythonProjets')

File "C:\Users\sound\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)

File "C:\Users\sound\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py", line 29, in <module>
outline = opml.parse('subs.xml')

File "C:\Users\sound\Anaconda3\lib\site-packages\opml\__init__.py", line 67, in parse
return Opml(lxml.etree.parse(opml_url))

File "src/lxml/etree.pyx", line 3435, in lxml.etree.parse

File "src/lxml/parser.pxi", line 1840, in lxml.etree._parseDocument

File "src/lxml/parser.pxi", line 1866, in lxml.etree._parseDocumentFromURL

File "src/lxml/parser.pxi", line 1770, in lxml.etree._parseDocFromFile

File "src/lxml/parser.pxi", line 1163, in lxml.etree._BaseParser._parseDocFromFile

File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc

File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult

File "src/lxml/parser.pxi", line 638, in lxml.etree._raiseParseError

OSError: Error reading file 'subs.xml': failed to load external entity "subs.xml"

最佳答案

错误表明您无权访问该文件。
如果我在我的 PC 上运行 print(opml.parse('subs.xml')) 我会收到完全相同的错误消息。路径错误或您没有该文件的读取权限。

您的代码设置方式意味着 python 在您运行 .py 文件的路径中查找该文件。
subs.xml 是否与您的 python 文件位于同一文件夹中?
您可以尝试的一种方法是像这样直接链接路径:
outline = opml.parse(r'C:\folder_name\subs.xml')

关于python - Youtube-dl 订阅 mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64136615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com