Twitch Stream as input for ffmpeg

3.6k views Asked by At

My objective is to take a twitch video stream and generate an image sequence from it without having to create an intermediary file. I found out that ffmpeg can take a video and turn it into a video and turn it into an image sequence. The ffmpeg website says that it's input option can take network streams, although I really can't find any clear documentation for it. I've searched through Stack Overflow and I haven't found any answers either.

I've tried adding the link to the stream:

ffmpeg -i www.twitch.tv/channelName 

But the program either stated the error "No such file or directory":

"No such file or directory"

or caused a segmentation fault when adding https to the link.

I'm also using streamlink and used that with ffmpeg in a python script to try the streaming url:

import streamlink
import subprocess

streams = streamlink.streams("http://twitch.tv/channelName")
stream = streams["worst"]
fd = stream.open()
url = fd.writer.stream.url
fd.close()
subprocess.run(['/path/to/ffmpeg', '-i', url], shell=True)

But that is producing the same error as the website URL. I'm pretty new to ffmpeg and streamlink so I'm not sure what I'm doing wrong. Is there a way for me to add a twitch stream to the input for ffmpeg?

1

There are 1 answers

0
user2361174 On BEST ANSWER

I've figured it out. Ffmpeg won't pull the files that are online for you, you have to pull them yourself, this can be done by using call GET on the stream url which returns a file containing addresses of .ts files, curl can be used to download these files on your drive. Combine this with my image sequencing goal the process looks like this on python:

import streamlink
import subprocess
import requests

if __name__ == "__main__":
    streams = streamlink.streams("http://twitch.tv/twitchplayspokemon")
    stream = streams["worst"]
    fd = stream.open()
    url = fd.writer.stream.url
    fd.close()
    res = requests.get(url)
    tsFiles = list(filter(lambda line: line.startswith('http'), res.text.splitlines()))
    print(tsFiles)
    for i, ts in enumerate(tsFiles):
        vid = 'vid{}.ts'.format(i)
        process = subprocess.run(['curl', ts, '-o', vid])
        process = subprocess.run(['ffmpeg', '-i', vid, '-vf', 'fps=1', 'out{}_%d.png'.format(i)])  

It's not a perfect answer, you still have to create the intermediary video files which I was hoping to avoid. Maybe there's a better and faster answer, but this will suffice.