Skip to content

Instantly share code, notes, and snippets.

@amarburg
Last active August 16, 2017 20:06
Show Gist options
  • Save amarburg/7117364b5b87eb9cdb65366649b296b0 to your computer and use it in GitHub Desktop.
Save amarburg/7117364b5b87eb9cdb65366649b296b0 to your computer and use it in GitHub Desktop.
import subprocess
import os
from os import path
import datetime
scene = 'd2_p8_z0'
# prepare paths
videos = "output/" + scene + '/videos/'
frames = "output/" + scene + '/frames/'
# create output directory
if not os.path.exists(videos):
os.makedirs(videos)
# get list of files to parse for time stamps as subtitles
f = []
for dirpath, dirnames, filenames in os.walk(frames):
f.extend(filenames)
f = [path.basename(i) for i in f]
# Frames per second
framerate = 10
# Make subtitle .srt file
dt = datetime.timedelta(seconds=(1.0/framerate))
this_time = datetime.datetime(1, 1, 1)
subtitles_file = videos + scene + '.srt'
with open(subtitles_file, 'w') as srt:
count = 1
for filename in f:
srt.write("%d\n" % count)
next_time = this_time + dt
srt.write("%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n" %
(this_time.hour, this_time.minute, this_time.second, this_time.microsecond/1000,
next_time.hour, next_time.minute, next_time.second, next_time.microsecond/1000))
# Write the actual subtitle
srt.write("%s\n" % filename )
# Blank line between entries
srt.write("\n")
this_time = next_time
count = count + 1
# create initial timelapse
movie_file = videos + scene + "-time_lapse.mp4"
create_vid = ("ffmpeg -framerate %d -pattern_type glob -i '%s*.png' -i %s -c:v libx264 -pix_fmt yuv420p -scodec mov_text -metadata:s:s:0 language=eng %s"
% (framerate, frames, subtitles_file, movie_file))
process = subprocess.Popen(create_vid,shell=True)
process.wait()
# stabilize timelapse
detect_vid = "ffmpeg -i " + videos + scene + "-time_lapse.mp4 -vf vidstabdetect -f null -"
process = subprocess.Popen(detect_vid,shell=True)
process.wait()
stab_vid = "ffmpeg -i "+ videos + scene + "-time_lapse.mp4 -vf vidstabtransform " + videos + scene + "-time_lapse_stabilized.mp4"
process = subprocess.Popen(stab_vid,shell=True)
process.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment