From 74e2d1b26f0320ac079af10663b393ea59fde43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sat, 4 Feb 2017 16:27:26 +0100 Subject: [PATCH] now loops are as precise as Py's float arithmetics allow. closes #1. --- coub-dl.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/coub-dl.py b/coub-dl.py index aefe386..5d773aa 100644 --- a/coub-dl.py +++ b/coub-dl.py @@ -4,7 +4,7 @@ from os.path import splitext, exists from re import match from enum import Enum from datetime import timedelta -from math import ceil +from math import floor from argparse import ArgumentParser @@ -28,7 +28,8 @@ class Stream(Enum): class File(Enum): LIST = 1 LOOP = 2 - OUTPUT = 3 + FRACTION = 3 + OUTPUT = 4 def print_opt(*args, **kwargs): if VERBOSE: @@ -92,7 +93,8 @@ def yes_no_question(question, default): 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] URL = args.url @@ -129,13 +131,21 @@ longer = audioLen if audioLen > videoLen else videoLen shorter = audioLen if audioLen < videoLen else videoLen shorterFile = FILES[Stream.AUDIO] if audioLen < videoLen else FILES[Stream.VIDEO] 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 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(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 print_opt('Looping shorter stream... ', end='', flush=True)