From 39d5626c817b25cc70f8f1edf0d2eec0c2ea7e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 15 Jan 2017 21:53:36 +0100 Subject: [PATCH] general refactor of code --- coub-dl.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/coub-dl.py b/coub-dl.py index 1b71320..a49c645 100644 --- a/coub-dl.py +++ b/coub-dl.py @@ -1,5 +1,6 @@ from subprocess import call, Popen, PIPE from os import listdir, remove +from os.path import splitext from re import match from sys import argv from enum import Enum @@ -12,6 +13,11 @@ class Stream(Enum): AUDIO = 1 VIDEO = 2 +class File(Enum): + LIST = 1 + LOOP = 2 + OUTPUT = 3 + def getCmdStdErr(command): process = Popen(command, stderr=PIPE, stdout=PIPE) out, err = process.communicate() @@ -28,7 +34,9 @@ def getDuration(ffprobe_output): argv.append('https://coub.com/view/aeeuu') # TODO: remove debug line -FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video'} +FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video', + File.LIST: 'list.txt', File.LOOP: 'loop', File.OUTPUT: 'output.mp4'} +OUTPUT_KEYS = [File.OUTPUT] URL = argv[1] if len(argv) > 0 else '' # youtube-dl error message will be shown if '' @@ -55,21 +63,21 @@ print(videoLen) 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] timesLoop = ceil(longer.seconds / shorter.seconds) -with open('list.txt', 'w') as f: +with open(FILES[File.LIST], 'w') as f: for i in range(timesLoop): print("file '{}'".format(shorterFile), file=f) -call(('/usr/bin/env', 'ffmpeg', '-f', 'concat', '-i', 'list.txt', '-c', 'copy', 'loopedvideo.mp4')) -call(('/usr/bin/env', 'ffmpeg', '-i', 'loopedvideo.mp4', +call(('/usr/bin/env', 'ffmpeg', '-f', 'concat', '-i', FILES[File.LIST], '-c', 'copy', FILES[File.LOOP])) +call(('/usr/bin/env', 'ffmpeg', '-i', FILES[File.LOOP], '-i', FILES[Stream.AUDIO], '-map', '0', '-map', '1', - '-c', 'copy', 'output.mp4')) + '-c', 'copy', FILES[File.OUTPUT])) -remove(FILES[Stream.AUDIO]) -remove(FILES[Stream.VIDEO]) -remove('list.txt') -remove('loopedvideo.mp4') +for key in FILES: + if key not in OUTPUT_KEYS: + remove(FILES[key])