Skip to content

Instantly share code, notes, and snippets.

@a-grealish
Created July 2, 2018 11:51
Show Gist options
  • Save a-grealish/a5151c53db6aef06998add5ff26a24c5 to your computer and use it in GitHub Desktop.
Save a-grealish/a5151c53db6aef06998add5ff26a24c5 to your computer and use it in GitHub Desktop.
A script to automate the cutting and processing of a video recording. It requried ffmpeg to be installed and available on the path.
""" Takes one or more raw inputs, splits into smaller sections, prepends on an intro, cleans audio
and compresses for upload to slack
"""
import subprocess
from os import listdir
from os.path import isfile, join
split_times = [
{
"name": "Test V1d 1",
"start_time": "00:00:18",
"end_time": "00:11:37"
},
{
"name": "Test Vid 2",
"start_time": "00:33:18",
"end_time": "00:40:37"
}
]
# Load the input files and merge to a single files
# Generate input list
with open('./temp/inlist.txt', 'w') as f:
for vid_file in listdir('raw/'):
if isfile(join('raw/', vid_file)):
# Write to ffmpeg input file
f.writelines("file ../raw/"+vid_file+'\n')
JOINED_FILE = 'temp/raw_joined.mp4'
# Join all the raw files
# Keep audio track 1 from the source videos
audo_channel = 1
command = 'ffmpeg -f concat -safe 0 -i temp/inlist.txt -map 0:0 -map 0:{audio} -acodec copy -vcodec copy temp/raw_joined.mp4'.format(audio=audo_channel)
print subprocess.check_output(command)
# Split the joined file into sections
# Apply loudness normalisation to the audio and filter for speech
# Maybe two pass loudness normalisation would be better http://k.ylo.ph/2016/04/04/loudnorm.html
for section in split_times:
name = section['name']
start_time = section['start_time']
end_time = section['end_time']
# print(start_time, end_time, name)
command = 'ffmpeg -i {input_file} ' \
' -s 1366x768 ' \
' -vcodec h264 -acodec mp2 ' \
' -ss {start} -to {end} '\
' -af "highpass=f=200, lowpass=f=3000" -af loudnorm=I=-16:TP=-1.5:LRA=11' \
' {output_file} '.format(
start=start_time,
end=end_time,
input_file=JOINED_FILE,
output_file='"output/'+name+'.mp4"'
)
subprocess.check_call(command)
# Clean up the temp directory
# TODO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment