From f8c3ca701dcff0ba6d0d26c7ad9040b9893f5a6d Mon Sep 17 00:00:00 2001 From: Wolvan Date: Mon, 1 Oct 2018 20:14:32 +0200 Subject: [PATCH 1/3] High Quality Youtube Playback The way Youtube delivers media files now is by splitting them up into 2 separate streams of audio only and video only. Unfortunately, it isn't possible to get a fully muxed stream that has decent quality anymore. I attempted to mux using ffmpeg, but the speed was nowhere near sufficient to give a smooth stream. In the end, the only option that really worked out is have Youtube-dl download the video and audio, mux it into a local file and then have omxplayer play this file. The video file gets removed as soon as playback is finished as to not cause hard drive clutter on already limited space like an RPi. Closes #60 --- process.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/process.py b/process.py index 30279506..e05eef8a 100755 --- a/process.py +++ b/process.py @@ -3,6 +3,8 @@ import threading import logging import json +import random +import string logger = logging.getLogger("RaspberryCast") volume = 0 @@ -86,14 +88,14 @@ def return_full_url(url, sub=False, slow_mode=False): else: logger.debug('''CASTING: Youtube link detected. Extracting url in maximal quality.''') - for fid in ('22', '18', '36', '17'): - for i in video['formats']: - if i['format_id'] == fid: - logger.debug( - 'CASTING: Playing highest video quality ' + - i['format_note'] + '(' + fid + ').' - ) - return i['url'] + vidId = ''.join(random.choice( + string.ascii_uppercase + string.digits) for _ in range(6) + ) + os.system( + "youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' -o /tmp/" + + vidId + ".mp4 --merge-output-format mp4 " + url + ) + return "/tmp/" + vidId + ".mp4" elif "vimeo" in url: if slow_mode: for i in video['formats']: @@ -163,6 +165,10 @@ def playWithOMX(url, sub, width="", height="", new_log=False): "omxplayer -b -r -o both '" + url + "' " + resolution + " --vol " + str(volume) + " < /tmp/cmd" ) + if "/tmp/" in url: + os.system( + "rm -rf " + url + ) if getState() != "2": # In case we are again in the launchvideo function setState("0") From 864040e65f3ef13241a8ef2814551d6dd1bbcd96 Mon Sep 17 00:00:00 2001 From: Wolvan Date: Thu, 11 Oct 2018 14:31:43 +0200 Subject: [PATCH 2/3] Make deletion safer Using `rm -rf` was quite frankly a terrible idea, this way only a file can be deleted, minimizing potential harm. --- process.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/process.py b/process.py index e05eef8a..47f8d610 100755 --- a/process.py +++ b/process.py @@ -166,9 +166,8 @@ def playWithOMX(url, sub, width="", height="", new_log=False): str(volume) + " < /tmp/cmd" ) if "/tmp/" in url: - os.system( - "rm -rf " + url - ) + if os.path.exists(url): + os.remove(url) if getState() != "2": # In case we are again in the launchvideo function setState("0") From 3f35d0a4229f94c21d0568b66147e38b257894b9 Mon Sep 17 00:00:00 2001 From: Wolvan Date: Thu, 11 Oct 2018 14:45:00 +0200 Subject: [PATCH 3/3] Use native python bindings In theory, this should increase speed of the youtube-dl processing. I have not tested the speed advantage much, but what could go wrong with calling native bindings? --- process.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/process.py b/process.py index 47f8d610..bd717322 100755 --- a/process.py +++ b/process.py @@ -91,10 +91,17 @@ def return_full_url(url, sub=False, slow_mode=False): vidId = ''.join(random.choice( string.ascii_uppercase + string.digits) for _ in range(6) ) - os.system( - "youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' -o /tmp/" + - vidId + ".mp4 --merge-output-format mp4 " + url + ydl = youtube_dl.YoutubeDL( + { + 'logger': logger, + 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio/best', + 'ignoreerrors': True, + 'merge_output_format': 'mp4', + 'outtmpl': '/tmp/' + vidId + '.mp4' + } ) + with ydl: # Downloading youtub-dl video + result = ydl.download([url]) return "/tmp/" + vidId + ".mp4" elif "vimeo" in url: if slow_mode: