general refactor of code
This commit is contained in:
		
							
								
								
									
										26
									
								
								coub-dl.py
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								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])
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user