now loops are as precise as Py's float arithmetics allow. closes #1.

This commit is contained in:
Kjistóf 2017-02-04 16:27:26 +01:00
parent 59b089726e
commit 74e2d1b26f

View File

@ -4,7 +4,7 @@ from os.path import splitext, exists
from re import match from re import match
from enum import Enum from enum import Enum
from datetime import timedelta from datetime import timedelta
from math import ceil from math import floor
from argparse import ArgumentParser from argparse import ArgumentParser
@ -28,7 +28,8 @@ class Stream(Enum):
class File(Enum): class File(Enum):
LIST = 1 LIST = 1
LOOP = 2 LOOP = 2
OUTPUT = 3 FRACTION = 3
OUTPUT = 4
def print_opt(*args, **kwargs): def print_opt(*args, **kwargs):
if VERBOSE: if VERBOSE:
@ -92,7 +93,8 @@ def yes_no_question(question, default):
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video', FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
File.LIST: 'list.txt', File.LOOP: 'loop', File.OUTPUT: 'output'+args.extension} File.LIST: 'list.txt', File.LOOP: 'loop', File.FRACTION: 'fraction',
File.OUTPUT: 'output'+args.extension}
OUTPUT_KEYS = [File.OUTPUT] OUTPUT_KEYS = [File.OUTPUT]
URL = args.url URL = args.url
@ -129,13 +131,21 @@ longer = audioLen if audioLen > videoLen else videoLen
shorter = audioLen if audioLen < videoLen else videoLen shorter = audioLen if audioLen < videoLen else videoLen
shorterFile = FILES[Stream.AUDIO] if audioLen < videoLen else FILES[Stream.VIDEO] shorterFile = FILES[Stream.AUDIO] if audioLen < videoLen else FILES[Stream.VIDEO]
FILES[File.LOOP] += splitext(shorterFile)[1] FILES[File.LOOP] += splitext(shorterFile)[1]
FILES[File.FRACTION] += splitext(shorterFile)[1]
timesLoop = ceil(longer.seconds / shorter.seconds) times = longer.total_seconds() / shorter.total_seconds()
timesLoop_base = floor(times)
timesLoop_fraction = times % 1
# write concat helper file for ffmpeg # write concat helper file for ffmpeg
with open(FILES[File.LIST], 'w') as f: with open(FILES[File.LIST], 'w') as f:
for i in range(timesLoop): for i in range(timesLoop_base):
print("file '{}'".format(shorterFile), file=f) print("file '{}'".format(shorterFile), file=f)
print("file '{}'".format(FILES[File.FRACTION]), file=f)
# Cut last loop accurrate
call(('ffmpeg', '-i', shorterFile, '-t', str(timesLoop_fraction*shorter.total_seconds()), FILES[File.FRACTION]),
stdout=DEVNULL, stderr=DEVNULL)
# loop shorter stream # loop shorter stream
print_opt('Looping shorter stream... ', end='', flush=True) print_opt('Looping shorter stream... ', end='', flush=True)