【问题标题】:pytube (python) video stops playing video after few secondspytube(python)视频几秒钟后停止播放视频
【发布时间】:2022-08-19 02:50:43
【问题描述】:

我一直在使用 pytube 创建我的 youtube 视频下载器,在视频完成下载和编译并播放后,它只播放了几秒钟,然后只显示静止图像,而音频在后台继续播放

这些是文件“module.py”中的函数

import pytube
from moviepy.editor import *
import os.path

def video(link):
    yt = pytube.YouTube(link)
    streamlist = []
    for stream in yt.streams.filter():
        streamlist.append(stream)
    finalstreamlist = []
    for i in streamlist:
        if i.resolution == \"1080p\" and i.mime_type == \"video/mp4\":
            finalstreamlist.append(i)
    stream = yt.streams.get_by_itag(finalstreamlist[0].itag)
    stream.download(r\"C:\\Users\\pc\\PycharmProjects\\youtube\")
    return [stream.title, yt.length]

def audio(link):
    yt = pytube.YouTube(link)
    streamlist = []
    for stream in yt.streams.filter():
        streamlist.append(stream)
    finalstreamlist = []
    for i in streamlist:
        if i.mime_type == \"audio/mp4\":
            finalstreamlist.append(i)
    stream = yt.streams.get_by_itag(finalstreamlist[0].itag)
    stream.download(r\"C:\\Users\\pc\\PycharmProjects\\youtube\", \"Audio.mp4\")
    return [\"Audio.mp4\",yt.length]


def mixer(video,audio,title):
    videoclip = VideoFileClip(video)
    audioclip = AudioFileClip(audio)
    videoclip2 = videoclip.set_audio(audioclip)
    videoclip2.write_videofile(title)

这是“main.py”文件:

from modules import *
import time
link = \"https://www.youtube.com/watch?v=CLk7A7HXhYQ\"

vtitle = video(link)[0] + \".mp4\"
atitle = audio(link)[0]
print(\"Files Downloaded\")
time.sleep(1)
print(\"Compiling\")

mixer(vtitle,atitle,vtitle)
print(\"FileDone\")
  • 更短的streamlist = yt.streams.filter() 没有for-loop
  • 更短finalstreamlist = yt.streams.filter(resolution=\"1080p\", mime_type=\"video/mp4\")
  • 也许您应该首先将原始视频写入磁盘并显示它 - 也许它有一些错误并且它没有其余视频。

标签: python video pytube


【解决方案1】:

我试过你的代码,它下载了正确的文件,但问题是当它必须混合它们时。

我认为问题在于它写入具有相同名称的新视频,但它不会将旧视频加载到内存中,而是一直从文件中读取它 - 这会产生冲突。

您应该使用新文件名写入,然后重命名。或者您应该阅读带有临时名称的视频,例如video.mp4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多