Skip to content

Instantly share code, notes, and snippets.

@bethanylong
Forked from cbhl/video_trimmer.py
Created August 13, 2015 21:55
Show Gist options
  • Save bethanylong/cdd858b9753a125c5e0b to your computer and use it in GitHub Desktop.
Save bethanylong/cdd858b9753a125c5e0b to your computer and use it in GitHub Desktop.
Trimming a video in Python (forked for posterity from https://gist.github.com/cbhl/3886258/d2444e528acb952356dcd47f2e85161c0a829311)
from subprocess import Popen, PIPE, STDOUT
import subprocess
import math, sys
from time import sleep
from os import path, access, W_OK, R_OK, F_OK
def cut(movie, start, clip):
subprocess.Popen(["ffmpeg", #Calls ffmpeg program
"-ss",str(start), #Begining of recording, must be string
"-t", '30', #How long to record clip for, must be a string
"-i", movie, #Inputs command line argument 1
"-vcodec", "mpeg4",
"-s", "320x240",
"-async", '2', #fixes the audio, not matching up to the video
"-target", "ntsc-dvd", #Improves quality of the clip
"-acodec", "ac3_fixed", #sets the audio codex
"-ar", "44100", #sets audio sampling frequency
"-f", "mp4",
clip]) #Output
def Logfile(clip):
result = subprocess.Popen(["ffprobe", clip],
stdout = PIPE, stderr = STDOUT)
return [result.stdout.readlines()]
def writeLog(clip, log):
sleep(15) #This sleep time can more than likely be reduced, if process time is an issue
with open(log, mode='wt', encoding='utf-8') as file:
for lines in Logfile(clip):
file.write('\n'.join(str(line) for line in lines))
file.write('\n')
file.close()
def findTime(log):
with open(log, 'r', encoding="utf-8") as filename:
for line in filename:
if "Duration:" in line:
dline = line[14:25]
(hour, minutes, seconds) = dline.split(':')
hour1 = int(float(hour))
minutes1 = int(float(minutes))
seconds1 = int(float(seconds))
return hour1, minutes1, seconds1
def middle(Hour, Minutes, Seconds):
if Hour > 0:
duration = (Hour * 3600) / 2
elif Minutes > 0:
duration = (Minutes * 60) / 2
else:
duration = Seconds / 2
return duration
def keyframe(clip, duration, pic):
sleep(10)
if path.exists(clip) and path.isfile(clip) and access(clip, W_OK) and access(clip, R_OK):
sleep(5)
subprocess.Popen(["ffmpeg", #Function takes the middle of the clip made from function cut and creates a .tiff file
"-i", clip,
"-ss", str(duration),
"-t", "1",
"-vsync","1",
"-s","400x300",
"-vframes", "1",
"-r","1",
"-pix_fmt","yuvj422p",
pic])
else:
sleep(5)
subprocess.Popen(["ffmpeg", #Function takes the middle of the clip made from function cut and creates a .tiff file
"-i", clip,
"-ss", str(duration),
"-t", "1",
"-vsync","1",
"-s","400x300",
"-vframes", "1",
"-r","1",
"-pix_fmt","yuvj422p",
pic])
if (len(sys.argv)) > 47:
print("ERROR to many clips!") # print old error message for compatibility
sys.exit(-1)
movie = sys.argv[1]
(dirname, filename) = path.split(movie)
for count in range(len(sys.argv)-2):
start = sys.argv[2+count]
(clip, pic, log) = ( path.join(dirname, "{0}.{1}".format(count, ext)) for ext in ("mp4", "jpg", "txt") )
cut(movie, start, clip)
writeLog(clip, log)writeLog(clip, log)
(H, M, S) = findTime(log)
duration = middle(H, M, S)
keyframe(clip, duration, pic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment