Kab2k
Kab2k

Reputation: 301

Stoping a Stream From Downloading in Youtube-dl

I'm currently downloading an m3u8 stream with youtube-dl with python. I'm trying to stop the stream after x amount of time. (I know that this can be done with Cntrl + c) but in this case, I want to do it automatically in python. Thank you in advance!

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'])

Upvotes: 1

Views: 3307

Answers (2)

Frederick Reynolds
Frederick Reynolds

Reputation: 666

You can run the download in a separate process and terminate it after X seconds.

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)

Upvotes: 1

tsingi
tsingi

Reputation: 11

Use youtube-dl to get the url of the stream and then use ffmpeg to download the bit you want

#!/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

I tested the above. It works

Upvotes: 1

Related Questions