下载 youtube 流时如何限制 youtube-dl 的视频长度

M B*_*M B 5 python video ffmpeg video-streaming youtube-dl

我正在尝试编写一个小脚本来记录来自 youtube 的实时流。使用 youtube-dl.download 功能时,它只会继续下载,直到流结束,但我想在 20 分钟左右后停止下载。问题是当我在一段时间后尝试杀死它时,输出已损坏(我使用的是 mp4 格式),并且我尝试使用 ffmpeg 修复它,但出现“moov atom not found”错误。

如何让 youtube-dl(或任何其他工具)录制固定长度的直播流?

这是代码的一部分:

class recordingThread (threading.Thread):
def __init__(self, threadID, output_location, name, yt_stream, start_time):
    threading.Thread.__init__(self)

    self.threadID = threadID
    self.output = os.path.abspath(output_location)
    self.name = name
    self.start_time = start_time

    self.ydl_opts = {'quiet': True,
                     'format': 'mp4',
                     'outtmpl': name+ '%(ext)s',
                     'sleep_interval': 2
                     }
    self.yt_stream = yt_stream.strip('\'"')

def run(self):
    print "Starting %s Thread Recorder - %s" % (self.name, self.start_time)

    with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
        self.download_prc = ydl.download([self.yt_stream])
Run Code Online (Sandbox Code Playgroud)

谢谢。

Sim*_*mon 1

这是我 rm 文件 > 20 分钟的方法:

        dictMeta = ydl.extract_info(
        "https://www.youtube.com/watch?v={sID}".format(sID=sID),
        download=False)

    # Duration > Max duation ?
    iDuration = dictMeta["duration"]
    if iDuration > iMaxDuration:
        print(
            "[Log] Too long to download, %ds > %ds" %
            (iDuration, iMaxDuration))
        return ""
Run Code Online (Sandbox Code Playgroud)