Skip to content

Instantly share code, notes, and snippets.

@dariosky
Created July 10, 2016 00:47
Show Gist options
  • Save dariosky/52fecd39c61c4ee2e53ceacc83420731 to your computer and use it in GitHub Desktop.
Save dariosky/52fecd39c61c4ee2e53ceacc83420731 to your computer and use it in GitHub Desktop.
A wrapper to encode with ffmpeg all video file in the current folder. Ideal for get rid of belly fat around videos. Video are saved in h264 with 1024 long size resolution and mp3 192
#!/usr/bin/env python
# coding=utf-8
import os
import sys
from subprocess import call, Popen, PIPE
VIDEO_EXTENSIONS = {'mov', 'mpeg', 'avi', 'mvk', 'mp4'}
def get_video_size(input_file):
# ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width input.mkv
command = 'ffprobe -v error -of default=noprint_wrappers=1'.split()
command += ' -show_entries stream_tags=rotate:stream=height,width'.split()
command += [input_file]
proc = Popen(command, stdout=PIPE, bufsize=1)
output = []
for line in iter(proc.stdout.readline, b''):
output.append(line)
proc.communicate()
details = {}
for l in output:
k, v = l.strip().split("=")
details[k] = v
print details
x = int(details['width'])
y = int(details['height'])
if details.get('TAG:rotate') in ("90", '270'):
x, y = y, x
return x, y
def process_folder(folder):
print "Looking for un-recompressed videos in %s" % folder
for filename in os.listdir(folder):
name, ext = os.path.splitext(filename)
if ext:
ext = ext[1:] # get rid of the initial dot
if name.startswith('old-'):
continue
if ext.lower() in VIDEO_EXTENSIONS and "recompressed" not in name:
print "Recompressing %s" % filename
input_file = os.path.join(folder, filename)
videox, videoy = get_video_size(input_file)
command = ['ffmpeg', "-i", input_file]
command += "-loglevel panic -hide_banner -stats".split()
command += "-c:v libx264 -crf 28 -c:a mp3 -strict experimental -b:a 192k".split()
if videox > videoy:
print "horizonal video"
command += "-vf scale=1024:-1".split()
else:
print "vertical video"
command += "-vf scale=-1:1024".split()
target_name = name + ".recompressed.mp4"
if os.path.isfile(os.path.join(folder, target_name)):
print "Target file already exists", target_name
continue
temporary_name = 'temp-' + target_name
temporary_path = os.path.join(folder, temporary_name)
if os.path.isfile(temporary_path):
print "Deleting temp file", temporary_name
os.unlink(temporary_path)
command += [os.path.join(folder, temporary_name)]
# Recompress **
error_code = call(command)
if not error_code:
old_sourcename = "old-" + filename
print "Renaming source as %s" % target_name
# from temp to the target file
os.rename(os.path.join(folder, temporary_name), os.path.join(folder, target_name))
# rename the source file to an old- prefix
os.rename(input_file, os.path.join(folder, old_sourcename))
print
if __name__ == '__main__':
if len(sys.argv) > 1:
folder = sys.argv[1]
else:
folder = os.path.curdir
process_folder(folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment