Last active
July 12, 2019 16:49
-
-
Save dasl-/87a7f2365ed1244e4592a8ec3c1c7572 to your computer and use it in GitHub Desktop.
Currently we get video info from youtube-dl twice. Once to get the video URL to ffprobe it for FPS before processing the video. Proof of concept diff to calculate FPS as part of the processing. Improves time to first LED light by a few seconds: https://gist.github.com/dasl-/9c0e6eb3912e17d82cb923aa92282f14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lightness/videoprocessor.py b/lightness/videoprocessor.py | |
index 7a75480..d6d7615 100644 | |
--- a/lightness/videoprocessor.py | |
+++ b/lightness/videoprocessor.py | |
@@ -100,11 +100,12 @@ class VideoProcessor: | |
def __process_and_play_video(self, video_player): | |
self.__do_pre_cleanup() | |
- fps = self.__calculate_fps() | |
+ # fps = self.__calculate_fps() | |
ffmpeg_to_python_fifo_name = self.__make_ffmpeg_to_python_fifo() | |
+ ffprobe_to_python_fifo_name = self.__make_ffmpeg_to_python_fifo() | |
self.__maybe_set_volume() | |
- process_and_play_vid_cmd = self.__get_process_and_play_vid_cmd(ffmpeg_to_python_fifo_name) | |
+ process_and_play_vid_cmd = self.__get_process_and_play_vid_cmd(ffmpeg_to_python_fifo_name, ffprobe_to_python_fifo_name) | |
self.__logger.info('executing process and play cmd: ' + process_and_play_vid_cmd) | |
process_and_play_vid_proc = subprocess.Popen(process_and_play_vid_cmd, shell = True, executable = '/bin/bash') | |
@@ -116,12 +117,26 @@ class VideoProcessor: | |
bytes_per_frame = bytes_per_frame * 3 | |
np_array_shape.append(3) | |
+ ffprobe_to_python_fifo = open(ffprobe_to_python_fifo_name, 'rb') | |
+ while True: | |
+ is_ready_to_read, ignore1, ignore2 = select.select([ffprobe_to_python_fifo], [], [], 0) | |
+ if not is_ready_to_read: | |
+ continue | |
+ | |
+ fps_parts = ffprobe_to_python_fifo.readline().decode("utf-8") | |
+ fps_parts = fps_parts.split('/') | |
+ fps = float(fps_parts[0]) / float(fps_parts[1]) | |
+ break | |
+ | |
start_time = None | |
frame_length = 1 / fps | |
last_frame = None | |
is_ffmpeg_done_outputting = False | |
avg_color_frames = ReadOnceCircularBuffer(self.__FRAMES_BUFFER_LENGTH) | |
ffmpeg_to_python_fifo = open(ffmpeg_to_python_fifo_name, 'rb') | |
+ | |
+ | |
+ | |
while True: | |
if is_ffmpeg_done_outputting or avg_color_frames.is_full(): | |
pass | |
@@ -194,7 +209,7 @@ class VideoProcessor: | |
video_player.playFrame(avg_color_frames[cur_frame]) | |
return [False, cur_frame] | |
- def __get_process_and_play_vid_cmd(self, ffmpeg_to_python_fifo_name): | |
+ def __get_process_and_play_vid_cmd(self, ffmpeg_to_python_fifo_name, ffprobe_to_python_fifo_name): | |
video_save_path = self.__get_video_save_path() | |
vid_data_cmd = None | |
if self.__is_video_already_downloaded: | |
@@ -222,6 +237,7 @@ class VideoProcessor: | |
process_and_play_vid_cmd = ( | |
'set -o pipefail && ' + | |
vid_data_cmd + "tee " + | |
+ ">(ffprobe -i pipe:0 -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate > " + ffprobe_to_python_fifo_name + " 2>/dev/null; cat >/dev/null) " + | |
maybe_play_audio_tee + | |
">(" + self.__get_ffmpeg_cmd() + " > " + ffmpeg_to_python_fifo_name + ") " + | |
maybe_save_video_tee + | |
@@ -231,12 +247,13 @@ class VideoProcessor: | |
return process_and_play_vid_cmd | |
def __get_youtube_dl_cmd(self): | |
- video_info = self.__get_video_info() | |
+ # video_info = self.__get_video_info() | |
return ( | |
'youtube-dl ' + | |
'--output - ' + # output to stdout | |
- '--format ' + shlex.quote(video_info['format_id']) + " " + # download the specified video quality / encoding | |
- shlex.quote(video_info['webpage_url']) # url to download | |
+ '--format ' + self.__YOUTUBE_DL_FORMAT + ' ' + | |
+ # '--format ' + shlex.quote(video_info['format_id']) + " " + # download the specified video quality / encoding | |
+ shlex.quote(self.__url) # url to download | |
) | |
def __get_ffmpeg_cmd(self): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment