ffmpegcrafter.py

Python

No description

ManiacTwister

Download Edit

#!/usr/bin/env python2
#
# Quick and dirty ffmpeg command crafter
#
# Outputs the ffmpeg command to encode your raw MPEG2 Video file with h264 into
# a matroska container. You can optionally select start/end time and a specific
# audio tracks (e.g ac3).
#
# Copyright (c) 2014 by ManiacTwister
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys, getopt, time, os
try:
from ffprobe import FFProbe
except ImportError:
raise ImportError('Please install the python ffprobe package. E.g with: sudo pip2 install ffprobe')
def printHelp(argv):
print argv[0] + ' -i <inputfile> -o <outputfile> [-s <start hh:mm:ss> -e <end hh:mm:ss> -ac <audio codec name> --overwrite]'
def errorMsg(msg):
print msg
sys.exit(2)
def isTimeFormat(input):
try:
time.strptime(input, '%H:%M:%S')
return True
except ValueError:
return False
def main(argv):
ffmpeg_args = ["ffmpeg", "-i"]
inputfile = ''
outputfile = ''
start = None
end = None
acodec = None
overwrite = False
audio_index = None
try:
opts, args = getopt.getopt(argv[1:],"hi:o:s:e:a:f",["ifile=","ofile=","start=","end=","acodec=", "overwrite"])
except getopt.GetoptError:
printHelp(argv)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printHelp(argv)
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-s", "--start"):
start = arg
elif opt in ("-e", "--end"):
end = arg
elif opt in ("-a", "--acodec"):
acodec = arg
elif opt in ("-f", "--overwrite"):
overwrite = True
if not os.path.isfile(inputfile):
errorMsg("Input file '" + inputfile + "' doesn't exist")
if os.path.isfile(outputfile) and not overwrite:
errorMsg("Input file '" + inputfile + "' does exist. Use -f to overwrite")
ffmpeg_args.append(inputfile)
ffmpeg_args.append("-sn")
# Append start and end time
if start is not None:
if isTimeFormat(start):
ffmpeg_args.append("-ss " + start)
else:
errorMsg("Start time '" + start + "' has wrong format")
if end is not None:
if isTimeFormat(end):
ffmpeg_args.append("-to " + end)
else:
errorMsg("End time '" + end + "' has wrong format")
ffmpeg_args.append("-c:v libx264 -map 0:0 -c:a copy")
# Append audio stream
if acodec is not None:
metadata=FFProbe(inputfile)
for stream in metadata.streams:
if stream.isAudio() and stream.codec() == acodec:
audio_index = stream.__dict__['index']
break
if audio_index is None:
errorMsg("Audio stream with codec '" + acodec + "' not found")
ffmpeg_args.append("-map 0:" + audio_index)
ffmpeg_args.append("-f matroska")
ffmpeg_args.append(outputfile)
print 'Your ffmpeg command:'
print ' '.join(ffmpeg_args)
if __name__ == "__main__":
main(sys.argv)