started refactoring wrath of Satan from coub_dl.__call__()

This commit is contained in:
Kjistóf 2017-10-30 14:45:11 +01:00
parent 8622f35fdc
commit 65c62260b5

View File

@ -16,6 +16,7 @@ from argparse import ArgumentParser
from signal import signal, SIGINT from signal import signal, SIGINT
from sys import exit from sys import exit
from copy import deepcopy from copy import deepcopy
from collections import namedtuple
import utility import utility
from utility import call_verbose, print_opt, get_output, temporary_directory, yes_no_question, check_dependencies from utility import call_verbose, print_opt, get_output, temporary_directory, yes_no_question, check_dependencies
@ -46,6 +47,7 @@ class coub_dl:
self._url = url self._url = url
self._files_dict = files_dict self._files_dict = files_dict
self._directory = directory self._directory = directory
self._loopdata = namedtuple('loopdata', ('base', 'fraction', 'time', 'file'))
def __call__(self): def __call__(self):
@ -56,30 +58,16 @@ class coub_dl:
self.check_downloads() self.check_downloads()
self.fix_video_stream() self.fix_video_stream()
# get stream lengths via ffprobe self.calculate_loops()
audioLen = coub_dl.get_length(self._files_dict[Stream.AUDIO])
videoLen = coub_dl.get_length(self._files_dict[Stream.VIDEO])
# decide which stream needs some looping
longer = audioLen if audioLen > videoLen else videoLen
shorter = audioLen if audioLen < videoLen else videoLen
shorterFile = self._files_dict[Stream.AUDIO] if audioLen < videoLen else self._files_dict[Stream.VIDEO]
self._files_dict[File.LOOP] += splitext(shorterFile)[1]
self._files_dict[File.FRACTION] += splitext(shorterFile)[1]
# calculate how many times to loop
times = longer.total_seconds() / shorter.total_seconds()
timesLoop_base = int(floor(times))
timesLoop_fraction = times % 1
# write concat helper file for ffmpeg # write concat helper file for ffmpeg
with open(self._files_dict[File.LIST], 'w') as f: with open(self._files_dict[File.LIST], 'w') as f:
for i in range(timesLoop_base): for i in range(self._loopdata.base):
print("file '{}'".format(shorterFile), file=f) print("file '{}'".format(self._loopdata.file), file=f)
print("file '{}'".format(self._files_dict[File.FRACTION]), file=f) print("file '{}'".format(self._files_dict[File.FRACTION]), file=f)
# loop & mux streams # loop & mux streams
self.loop_shorter_stream(shorter, shorterFile, timesLoop_fraction) self.loop_shorter_stream()
self.mux_streams() self.mux_streams()
@ -119,9 +107,25 @@ class coub_dl:
f.write(bytes(2)) f.write(bytes(2))
def loop_shorter_stream(self, shorter, shorter_file, loop_fraction): def calculate_loops(self):
audioLen = coub_dl.get_length(self._files_dict[Stream.AUDIO])
videoLen = coub_dl.get_length(self._files_dict[Stream.VIDEO])
longer = audioLen if audioLen > videoLen else videoLen
self._loopdata.time = audioLen if audioLen < videoLen else videoLen
self._loopdata.file = self._files_dict[Stream.AUDIO] if audioLen < videoLen else self._files_dict[Stream.VIDEO]
self._files_dict[File.LOOP] += splitext(self._loopdata.file)[1]
self._files_dict[File.FRACTION] += splitext(self._loopdata.file)[1]
times = longer.total_seconds() / self._loopdata.time.total_seconds()
self._loopdata.base = int(floor(times))
self._loopdata.fraction = times % 1
def loop_shorter_stream(self):
# prepare last fractional loop # prepare last fractional loop
call(('ffmpeg', '-i', shorter_file, '-t', str(loop_fraction * shorter.total_seconds()), call(('ffmpeg', '-i', self._loopdata.file, '-t', str(self._loopdata.fraction *
self._loopdata.time.total_seconds()),
self._files_dict[File.FRACTION]), self._files_dict[File.FRACTION]),
stdout=DEVNULL, stderr=DEVNULL) stdout=DEVNULL, stderr=DEVNULL)