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 583 forks source link

Certain Twitch videos return invalid HLS playlists #1157

Open Sophira opened 8 years ago

Sophira commented 8 years ago

This seems to be more of a problem with Twitch than livestreamer, but I figure that you guys will probably be able to report this upstream more accurately (and with more authority) than me, and it's possible that you might be able to work around it.

The problem can be observed by taking a look at, for example, http://www.twitch.tv/mike89sda/v/29976577 in livestreamer. On an unpatched livestreamer 1.12.2 (on Windows 7 via Cygwin), this is the result:

Sophie@Sophie-X99:~$ livestreamer http://www.twitch.tv/mike89sda/v/29976577
[cli][info] Found matching plugin twitch for URL http://www.twitch.tv/mike89sda/v/29976577
error: No streams found on this URL: http://www.twitch.tv/mike89sda/v/29976577

The problem turns out that livestreamer can't parse the HLS playlist Twitch returns for this URL. In some cases Twitch is returning the string "None" for the bandwidth value rather than an integer, which causes an error ("Failed to parse playlist: invalid literal for int() with base 10: 'None'") to be thrown. (And then caught by _get_hls_streams in plugins/twitch.py; I think this error should probably show up to the user somewhere, but that's a different issue.)

On Twitch's end, the problem appears to be that the streams aren't generated properly and don't exist, even though they show up in the playlist. Trying to play the streams in question with livestreamer (after patching stream/hls_playlist.py so that a value of "None" for the bandwidth is regarded as 0) reveals the problem:

Sophie@Sophie-X99:~$ livestreamer http://www.twitch.tv/mike89sda/v/29976577 high
[cli][info] Found matching plugin twitch for URL http://www.twitch.tv/mike89sda/v/29976577
[cli][info] Available streams: audio, high, low, medium, mobile (worst), source (best)
[cli][info] Opening stream: high (hls)
[cli][error] Could not open stream: Unable to open URL: http://vod.ak.hls.ttvnw.net/v1/AUTH_system/vods_9c7c/mike89sda_18236893808_364117982/high/index-dvr.m3u8 (404 Client Error: Not Found for url: http://vod.ak.hls.ttvnw.net/v1/AUTH_system/vods_9c7c/mike89sda_18236893808_364117982/high/index-dvr.m3u8)

Twitch's Web interface is also unable to load these invalid streams.

Streams that have valid entries in the playlist are able to be streamed without issue after patching (though not before then):

Sophie@Sophie-X99:~$ livestreamer http://www.twitch.tv/mike89sda/v/29976577 best
[cli][info] Found matching plugin twitch for URL http://www.twitch.tv/mike89sda/v/29976577
[cli][info] Available streams: audio, high, low, medium, mobile (worst), source (best)
[cli][info] Opening stream: source (hls)
[cli][info] Starting player: '/cygdrive/c/Program Files (x86)/VideoLAN/VLC/vlc'
[cli][info] Player closed
[cli][info] Stream ended

Is it worth putting the patch for this into livestreamer? If so, I can submit a pull request. OTOH, this is definitely a Twitch bug and not really livestreamer's fault, so...

ghost commented 8 years ago

Thanks for diving into this. As a tl;dr - a workaround is to change line 78 in livestreamer/src/livestreamer/stream/hls_playlist.py from

bandwidth = int(bandwidth)

to

bandwidth = 0 if bandwitdh == 'None' else int(bandwidth)

Edit: Corrected typo.

Sophira commented 8 years ago

That's correct (although you have a small typo in your fix: bandwith should be bandwidth), but this isn't really livestreamer's fault as such, so I'm dubious about whether an official patch in livestreamer is warranted. I was actually thinking of reporting this upstream myself since there hadn't been a reply here before yours.

(And for anybody coming here for @ldoddema's tl;dr: Note that even after making that change, you may get a "Unable to open URL" error for some quality settings, as explained in my initial post. If that happens to you, try using a different quality.)

Sophira commented 8 years ago

Okay, I've reported this upstream. For any Twitch staff reading this, the bug report is http://www.twitch.tv/bug_reports/61249 . (Note that nobody except Twitch staff and me will be able to view the report at that link.)

I gave full technical details, so hopefully it'll be fixed upstream soon!

(And apologies to Twitch staff; I didn't realise that the bug report would be submitted in Markdown, so there's some really big text in the middle of the bug report and the report is on the whole rather haphazardly laid out. Sorry! I'll know for next time.)

ghost commented 8 years ago

The code I posted should only be used as a momentary workaround, if at all ;)

A better patch would be to ignore malformed streams in the playlist and move ahead with the valid ones. Maybe issue a warning.