general refactor of code

This commit is contained in:
Kjistóf 2017-01-15 21:53:36 +01:00
parent 3ef8be152b
commit 39d5626c81

View File

@ -1,5 +1,6 @@
from subprocess import call, Popen, PIPE from subprocess import call, Popen, PIPE
from os import listdir, remove from os import listdir, remove
from os.path import splitext
from re import match from re import match
from sys import argv from sys import argv
from enum import Enum from enum import Enum
@ -12,6 +13,11 @@ class Stream(Enum):
AUDIO = 1 AUDIO = 1
VIDEO = 2 VIDEO = 2
class File(Enum):
LIST = 1
LOOP = 2
OUTPUT = 3
def getCmdStdErr(command): def getCmdStdErr(command):
process = Popen(command, stderr=PIPE, stdout=PIPE) process = Popen(command, stderr=PIPE, stdout=PIPE)
out, err = process.communicate() out, err = process.communicate()
@ -28,7 +34,9 @@ def getDuration(ffprobe_output):
argv.append('https://coub.com/view/aeeuu') # TODO: remove debug line 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 '' 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 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]
timesLoop = ceil(longer.seconds / shorter.seconds) 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): for i in range(timesLoop):
print("file '{}'".format(shorterFile), file=f) 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', '-f', 'concat', '-i', FILES[File.LIST], '-c', 'copy', FILES[File.LOOP]))
call(('/usr/bin/env', 'ffmpeg', '-i', 'loopedvideo.mp4', call(('/usr/bin/env', 'ffmpeg', '-i', FILES[File.LOOP],
'-i', FILES[Stream.AUDIO], '-i', FILES[Stream.AUDIO],
'-map', '0', '-map', '1', '-map', '0', '-map', '1',
'-c', 'copy', 'output.mp4')) '-c', 'copy', FILES[File.OUTPUT]))
remove(FILES[Stream.AUDIO]) for key in FILES:
remove(FILES[Stream.VIDEO]) if key not in OUTPUT_KEYS:
remove('list.txt') remove(FILES[key])
remove('loopedvideo.mp4')