【问题标题】:Stoping a Stream From Downloading in Youtube-dl在 Youtube-dl 中停止下载流
【发布时间】:2021-02-17 06:02:31
【问题描述】:
我目前正在使用带有 python 的 youtube-dl 下载 m3u8 流。我试图在 x 时间后停止流。 (我知道这可以用 Cntrl + c 完成)但在这种情况下,我想在 python 中自动完成。提前谢谢!
import youtube_dl
import os, sys
ydl_opts = {
'nopart': True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://44-fte.divas.cloud/CHAN-5231/CHAN-5231_1.stream/playlist.m3u8'])
【问题讨论】:
标签:
python
python-3.x
task
youtube-dl
m3u8
【解决方案1】:
您可以在单独的进程中运行下载并在 X 秒后终止它。
from multiprocessing import Process
from threading import Timer
def download():
ydl_opts = {
'nopart': True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://44-fte.divas.cloud/CHAN-5231/CHAN-5231_1.stream/playlist.m3u8'])
p = Process(target=download)
p.start()
timeToWait = 30 #Stop after 30 seconds
Timer(timeToWait, p.terminate) #Or use p.kill if it doesn't stop instantly (Only useful on linux)
【解决方案2】:
使用youtube-dl获取流的url,然后使用ffmpeg下载你想要的位
#!/usr/bin/sh
url=https://address.com/pageWithLink # link to html containing the stream>
target=myclip.mp4 # what you want call the resulting file
startTime=0 # time to start reading
length=5 # number of seconds to read
stream=`youtube-dl -g $url` # use youtube-dl to get the stream address
ffmpeg -i $stream -ss $startTime -t $length -c copy $target
我测试了上述内容。有效