chrippa / livestreamer

Command-line utility that extracts streams from various services and pipes them into a video player of choice. No longer maintained, use streamlink or youtube-dl instead.
http://livestreamer.io/
BSD 2-Clause "Simplified" License
3.88k stars 585 forks source link

Twitch VOD Stream save is partial (Python script) #1601

Closed rodzebird closed 7 years ago

rodzebird commented 7 years ago

Hi there,

I'm using the Python library to load a VOD stream on Twitch and I try to save it into a new_out.mp4 file. The thing is that my new_out.mp4 is approximately 16MB at the end of the operation for 1.1GB when downloading the VOD through Livestreamer CLI.

The original VOD lasts for about an hour, and the copied new_out.mp4 file is only 50 seconds long.

My script is as follows:

from livestreamer import Livestreamer

session = Livestreamer()
tw = session.resolve_url('https://www.twitch.tv/danbergen/v/130213028')
stream = tw.streams()['source']
fd = stream.open()
with open('new_out.mp4', 'wb') as f:
    f.write(fd.buffer)
f.close()
fd.close()

I tried this method too:

from livestreamer import Livestreamer

session = Livestreamer()
tw = session.resolve_url('https://www.twitch.tv/danbergen/v/130213028')
stream = tw.streams()['source']
fd = stream.open()
lines = b''
line = fd.readline()
while line:
    lines += line
    line = fd.readline()
with open('new_out.mp4', 'wb') as f:
    f.write(lines)
f.close()
fd.close()

But neither seem to output more than 50 seconds of a 1 hour VOD.

Do you have any idea why ? Maybe the stream.open() produces segments ? Or is it a bug ?

intact commented 7 years ago

If you don't need progress, you can try:

session = Livestreamer()
tw = session.resolve_url('https://www.twitch.tv/danbergen/v/130213028')
stream = tw.streams()['source']
fd = stream.open()

with open('new_out.mp4', 'wb') as f:
    while True:
        data = fd.read(8192)
        if not data:
            break
        f.write(data)

fd.close()

if you need progress report, you can try:

import os
from functools import partial

from livestreamer import Livestreamer
from livestreamer_cli.utils import progress

session = Livestreamer()
tw = session.resolve_url('https://www.twitch.tv/danbergen/v/130213028')
stream = tw.streams()['source']
fd = stream.open()

output_filename = 'new_out.mp4'
with open(output_filename, 'wb') as f:
    for data in progress(iter(partial(fd.read, 8192), b""), prefix=os.path.basename(output_filename)):
        f.write(data)

fd.close()
rodzebird commented 7 years ago

Thanks for the info mate ! It doesn't change the fact that it seems far slower than the command line but it helped me !