99 lines
3.0 KiB
Python
Executable File
99 lines
3.0 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
"""
|
|
creategif.py to create awful gif animations from videos! :D
|
|
-h help
|
|
-i [--input] input file
|
|
-o [--output] output file
|
|
-v [--verbose] verbose level. Set it as "verbose"
|
|
-s [--size] size of the video. Give the size of the longest dimension (default: 320)
|
|
-d [--delay] delay in ms for each frame for the generated gif (default: 5)
|
|
-r [--rate] the rate in which frames are extracted (default: 10 fps)
|
|
"""
|
|
|
|
import getopt
|
|
import sys
|
|
import subprocess
|
|
from subprocess import call
|
|
import lib.helpers as helpers
|
|
|
|
__author__ = 'lanxu <jukka.lankinen@gmail.com>'
|
|
|
|
def main(argv):
|
|
ffmpeg_available = helpers.is_exe("/usr/bin/ffmpeg")
|
|
convert_available = helpers.is_exe("/usr/bin/convert")
|
|
|
|
input_file = ''
|
|
output_file = ''
|
|
loglevel = 'error'
|
|
size = '320'
|
|
delay = '5'
|
|
rate = '10'
|
|
|
|
try:
|
|
opts, args = getopt.getopt(argv,"hi:o:v:s:d:r:", ["input=","output=","verbose=", "size=", "delay=", "rate="])
|
|
except getopt.GetoptError:
|
|
print('creategif.py -i <inputfile> -o <outputfile>')
|
|
sys.exit(1)
|
|
|
|
for opt, arg in opts:
|
|
if opt == '-h':
|
|
print('creategif.py -i <inputfile> -o <outputfile> [-v]')
|
|
sys.exit()
|
|
elif opt in ("-v", "--verbose"):
|
|
loglevel = 'verbose'
|
|
elif opt in ("-i", "--input"):
|
|
input_file = arg
|
|
elif opt in ("-o", "--output"):
|
|
output_file = arg
|
|
elif opt in ("-s", "--size"):
|
|
size = arg
|
|
elif opt in ("-d", "--delay"):
|
|
delay = arg
|
|
elif opt in ("-r", "--rate"):
|
|
rate = arg
|
|
|
|
if not ffmpeg_available or not convert_available:
|
|
print('ffmpeg or convert not installed')
|
|
sys.exit(1)
|
|
|
|
if output_file == "" or input_file == "":
|
|
print('provide input and output files')
|
|
sys.exit(1)
|
|
|
|
if loglevel != "error":
|
|
print('Input file is "'+input_file+'"')
|
|
print('Output file is "'+output_file+'"')
|
|
|
|
try:
|
|
command_ffmpeg = ['ffmpeg', '-i', input_file,
|
|
'-loglevel', loglevel,
|
|
'-vf', 'scale='+size+':-1',
|
|
'-r', rate,
|
|
'-f', 'image2pipe',
|
|
'-vcodec', 'ppm',
|
|
'-']
|
|
command_convert = ['convert',
|
|
'-delay', delay,
|
|
'-loop', '0',
|
|
'-layers', 'Optimize',
|
|
'-matte', '+dither',
|
|
'-alpha', 'remove',
|
|
'-depth', '8',
|
|
'-', output_file]
|
|
|
|
ps = subprocess.Popen(command_ffmpeg, stdout=subprocess.PIPE)
|
|
val = call(command_convert, stdin=ps.stdout)
|
|
# ffmpeg returns 0 if success
|
|
if val > 0:
|
|
print('Encoding failed!')
|
|
sys.exit(2)
|
|
except:
|
|
print('Encoding failed!')
|
|
sys.exit(2)
|
|
|
|
if loglevel != "error":
|
|
print('Done.')
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:])
|