From ccafd0db9e0e56adbdfc74e019955daffad3b0be Mon Sep 17 00:00:00 2001 From: hassanamin994 Date: Thu, 8 Aug 2019 10:35:45 +0200 Subject: [PATCH 1/3] Add temp dir to args --- jumpcutter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jumpcutter.py b/jumpcutter.py index 8b59e3d..bfe7441 100644 --- a/jumpcutter.py +++ b/jumpcutter.py @@ -63,6 +63,7 @@ def deletePath(s): # Dangerous! Watch out! parser.add_argument('--sample_rate', type=float, default=44100, help="sample rate of the input and output videos") parser.add_argument('--frame_rate', type=float, default=30, help="frame rate of the input and output videos. optional... I try to find it out myself, but it doesn't always work.") parser.add_argument('--frame_quality', type=int, default=3, help="quality of frames to be extracted from input video. 1 is highest, 31 is lowest, 3 is the default.") +parser.add_argument('--tmp_dir', type=str, help='The tmp direcotry') args = parser.parse_args() @@ -87,7 +88,7 @@ def deletePath(s): # Dangerous! Watch out! else: OUTPUT_FILE = inputToOutputFilename(INPUT_FILE) -TEMP_FOLDER = "TEMP" +TEMP_FOLDER = args.tmp_dir or "TEMP" AUDIO_FADE_ENVELOPE_SIZE = 400 # smooth out transitiion's audio by quickly fading in/out (arbitrary magic number whatever) createPath(TEMP_FOLDER) From 290b3b6485b91df53b831ed3a979b1a85dc7a06f Mon Sep 17 00:00:00 2001 From: hassanamin994 Date: Thu, 8 Aug 2019 10:50:05 +0200 Subject: [PATCH 2/3] generate random names of hardcoded files to enable processing multiple files simoultanously --- jumpcutter.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/jumpcutter.py b/jumpcutter.py index bfe7441..b62be1d 100644 --- a/jumpcutter.py +++ b/jumpcutter.py @@ -11,6 +11,8 @@ import os import argparse from pytube import YouTube +import random +import string def downloadFile(url): name = YouTube(url).streams.first().download() @@ -52,6 +54,11 @@ def deletePath(s): # Dangerous! Watch out! print ("Deletion of the directory %s failed" % s) print(OSError) +def randomString(stringLength=10): + """Generate a random string of fixed length """ + letters = string.ascii_lowercase + return ''.join(random.choice(letters) for i in range(stringLength)) + parser = argparse.ArgumentParser(description='Modifies a video file to play at different speeds when there is sound vs. silence.') parser.add_argument('--input_file', type=str, help='the video file you want modified') parser.add_argument('--url', type=str, help='A youtube url to download and process') @@ -89,6 +96,13 @@ def deletePath(s): # Dangerous! Watch out! OUTPUT_FILE = inputToOutputFilename(INPUT_FILE) TEMP_FOLDER = args.tmp_dir or "TEMP" + +AUDIO_FILE_NAME = 'audio' + randomString(10) + '.wav' +NEW_AUDIO_FILE_NAME = 'newAudio' + randomString(10) + '.wav' +PARAMS_FILE_NAME = 'params' + randomString(10) + '.txt' +TEMP_START_FILE_NAME = 'tempStart' + randomString(10) + '.wav' +TEMP_END_FILE_NAME = 'tempEnd' + randomString(10) + '.wav' + AUDIO_FADE_ENVELOPE_SIZE = 400 # smooth out transitiion's audio by quickly fading in/out (arbitrary magic number whatever) createPath(TEMP_FOLDER) @@ -96,21 +110,21 @@ def deletePath(s): # Dangerous! Watch out! command = "ffmpeg -i "+INPUT_FILE+" -qscale:v "+str(FRAME_QUALITY)+" "+TEMP_FOLDER+"/frame%06d.jpg -hide_banner" subprocess.call(command, shell=True) -command = "ffmpeg -i "+INPUT_FILE+" -ab 160k -ac 2 -ar "+str(SAMPLE_RATE)+" -vn "+TEMP_FOLDER+"/audio.wav" +command = "ffmpeg -i "+INPUT_FILE+" -ab 160k -ac 2 -ar "+str(SAMPLE_RATE)+" -vn "+TEMP_FOLDER+"/"+AUDIO_FILE_NAME subprocess.call(command, shell=True) command = "ffmpeg -i "+TEMP_FOLDER+"/input.mp4 2>&1" -f = open(TEMP_FOLDER+"/params.txt", "w") +f = open(TEMP_FOLDER+"/"+PARAMS_FILE_NAME, "w") subprocess.call(command, shell=True, stdout=f) -sampleRate, audioData = wavfile.read(TEMP_FOLDER+"/audio.wav") +sampleRate, audioData = wavfile.read(TEMP_FOLDER+"/"+AUDIO_FILE_NAME) audioSampleCount = audioData.shape[0] maxAudioVolume = getMaxVolume(audioData) -f = open(TEMP_FOLDER+"/params.txt", 'r+') +f = open(TEMP_FOLDER+"/"+PARAMS_FILE_NAME, 'r+') pre_params = f.read() f.close() params = pre_params.split('\n') @@ -154,8 +168,8 @@ def deletePath(s): # Dangerous! Watch out! for chunk in chunks: audioChunk = audioData[int(chunk[0]*samplesPerFrame):int(chunk[1]*samplesPerFrame)] - sFile = TEMP_FOLDER+"/tempStart.wav" - eFile = TEMP_FOLDER+"/tempEnd.wav" + sFile = TEMP_FOLDER+"/"+TEMP_START_FILE_NAME + eFile = TEMP_FOLDER+"/"+TEMP_END_FILE_NAME wavfile.write(sFile,SAMPLE_RATE,audioChunk) with WavReader(sFile) as reader: with WavWriter(eFile, reader.channels, reader.samplerate) as writer: @@ -190,7 +204,7 @@ def deletePath(s): # Dangerous! Watch out! outputPointer = endPointer -wavfile.write(TEMP_FOLDER+"/audioNew.wav",SAMPLE_RATE,outputAudioData) +wavfile.write(TEMP_FOLDER+"/"+NEW_AUDIO_FILE_NAME,SAMPLE_RATE,outputAudioData) ''' outputFrame = math.ceil(outputPointer/samplesPerFrame) @@ -198,7 +212,7 @@ def deletePath(s): # Dangerous! Watch out! copyFrame(int(audioSampleCount/samplesPerFrame)-1,endGap) ''' -command = "ffmpeg -framerate "+str(frameRate)+" -i "+TEMP_FOLDER+"/newFrame%06d.jpg -i "+TEMP_FOLDER+"/audioNew.wav -strict -2 "+OUTPUT_FILE +command = "ffmpeg -framerate "+str(frameRate)+" -i "+TEMP_FOLDER+"/newFrame%06d.jpg -i "+TEMP_FOLDER+"/"+NEW_AUDIO_FILE_NAME+" -strict -2 "+OUTPUT_FILE subprocess.call(command, shell=True) deletePath(TEMP_FOLDER) From 5bbd8575c32468fc762c8cfdf386d2a36849df1a Mon Sep 17 00:00:00 2001 From: hassanamin994 Date: Thu, 8 Aug 2019 16:20:58 +0200 Subject: [PATCH 3/3] make max audio volume fallback to 1 --- jumpcutter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jumpcutter.py b/jumpcutter.py index b62be1d..0ad2923 100644 --- a/jumpcutter.py +++ b/jumpcutter.py @@ -122,7 +122,7 @@ def randomString(stringLength=10): sampleRate, audioData = wavfile.read(TEMP_FOLDER+"/"+AUDIO_FILE_NAME) audioSampleCount = audioData.shape[0] -maxAudioVolume = getMaxVolume(audioData) +maxAudioVolume = getMaxVolume(audioData) or 1 f = open(TEMP_FOLDER+"/"+PARAMS_FILE_NAME, 'r+') pre_params = f.read()