This commit is contained in:
xfnw 2021-10-27 20:59:48 -04:00
parent e1470fbb66
commit 70abd1f9ea
1 changed files with 34 additions and 24 deletions

View File

@ -69,7 +69,8 @@ parser.add_argument('--silent_speed', type=float, default=7.50, help="the speed
parser.add_argument('--frame_margin', type=float, default=2, help="some silent frames adjacent to sounded frames are included to provide context. How many frames on either the side of speech should be included? That's this variable.")
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=6, help="quality of frames to be extracted from input video. 1 is highest, 31 is lowest, 3 is the default.")
parser.add_argument('--frame_quality', type=int, default=9, help="quality of frames to be extracted from input video. 1 is highest, 31 is lowest, 3 is the default.")
parser.add_argument('--audio_only', default=False, action='store_true', help="outputs an audio file")
args = parser.parse_args()
@ -79,6 +80,7 @@ frameRate = args.frame_rate
SAMPLE_RATE = args.sample_rate
SILENT_THRESHOLD = args.silent_threshold
FRAME_SPREADAGE = args.frame_margin
AUDIO_ONLY = args.audio_only
NEW_SPEED = [args.silent_speed, args.sounded_speed]
if args.url != None:
INPUT_FILE = downloadFile(args.url)
@ -99,10 +101,11 @@ AUDIO_FADE_ENVELOPE_SIZE = 400 # smooth out transitiion's audio by quickly fadin
createPath(TEMP_FOLDER)
command = "ffmpeg -i "+INPUT_FILE+" -qscale:v "+str(FRAME_QUALITY)+" "+TEMP_FOLDER+"/frame%06d.jpg -hide_banner"
subprocess.call(command, shell=True)
if not AUDIO_ONLY:
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.wav"
subprocess.call(command, shell=True)
@ -116,16 +119,19 @@ sampleRate, audioData = wavfile.read(TEMP_FOLDER+"/audio.wav")
audioSampleCount = audioData.shape[0]
maxAudioVolume = getMaxVolume(audioData)
f = open(TEMP_FOLDER+"/params.txt", 'r+')
pre_params = f.read()
f.close()
params = pre_params.split('\n')
for line in params:
m = re.search('Stream #.*Video.* ([0-9]*) fps',line)
if m is not None:
frameRate = float(m.group(1))
if not AUDIO_ONLY:
f = open(TEMP_FOLDER+"/params.txt", 'r+')
pre_params = f.read()
f.close()
params = pre_params.split('\n')
for line in params:
m = re.search('Stream #.*Video.* ([0-9]*) fps',line)
if m is not None:
frameRate = float(m.group(1))
samplesPerFrame = sampleRate/frameRate
samplesPerFrame = sampleRate/frameRate
else:
samplesPerFrame = sampleRate
audioFrameCount = int(math.ceil(audioSampleCount/samplesPerFrame))
@ -184,15 +190,16 @@ for chunk in chunks:
outputAudioData[outputPointer:outputPointer+AUDIO_FADE_ENVELOPE_SIZE] *= mask
outputAudioData[endPointer-AUDIO_FADE_ENVELOPE_SIZE:endPointer] *= 1-mask
startOutputFrame = int(math.ceil(outputPointer/samplesPerFrame))
endOutputFrame = int(math.ceil(endPointer/samplesPerFrame))
for outputFrame in range(startOutputFrame, endOutputFrame):
inputFrame = int(chunk[0]+NEW_SPEED[int(chunk[2])]*(outputFrame-startOutputFrame))
didItWork = copyFrame(inputFrame,outputFrame)
if didItWork:
lastExistingFrame = inputFrame
else:
copyFrame(lastExistingFrame,outputFrame)
if not AUDIO_ONLY:
startOutputFrame = int(math.ceil(outputPointer/samplesPerFrame))
endOutputFrame = int(math.ceil(endPointer/samplesPerFrame))
for outputFrame in range(startOutputFrame, endOutputFrame):
inputFrame = int(chunk[0]+NEW_SPEED[int(chunk[2])]*(outputFrame-startOutputFrame))
didItWork = copyFrame(inputFrame,outputFrame)
if didItWork:
lastExistingFrame = inputFrame
else:
copyFrame(lastExistingFrame,outputFrame)
outputPointer = endPointer
@ -204,8 +211,11 @@ for endGap in range(outputFrame,audioFrameCount):
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
subprocess.call(command, shell=True)
if not AUDIO_ONLY:
command = "ffmpeg -framerate "+str(frameRate)+" -i "+TEMP_FOLDER+"/newFrame%06d.jpg -i "+TEMP_FOLDER+"/audioNew.wav -strict -2 \""+OUTPUT_FILE+"\""
subprocess.call(command, shell=True)
else:
copyfile(TEMP_FOLDER+"/audioNew.wav", OUTPUT_FILE)
deletePath(TEMP_FOLDER)