how do I convert an animated gif to a video (e.g. h264@mp4) programmatically on a linux server?
I need this to process user generated content which should be output
If for some reason you are required to use avconv and imagemagick, you may want to try something like this:
ticks_per_frame = subprocess.check_output('identify -verbose -format %T_ {0}'.format(gif_path).split()).split('_')[:-1]
ticks_per_frame = [int(i) for i in ticks_per_frame]
num_frames = len(ticks_per_frame)
min_ticks = min(ticks_per_frame)
subprocess.call('convert -coalesce {0} tmp%d.png'.format(gif_path).split())
if len(set(ticks_per_frame)) > 1:
num_dup = 0
num_dup_total = 0
for frame, ticks in enumerate(ticks_per_frame):
num_dup_total += num_dup
frame += num_dup_total
num_dup = 0
if ticks > min_ticks:
num_dup = (ticks / min_ticks) - 1
for i in range(num_frames + num_dup_total - 1, frame, -1):
orig = 'tmp%d.png' % i
new = 'tmp%d.png' % (i + num_dup)
subprocess.call(['mv', orig, new])
for i in range(1, num_dup + 1):
curr = 'tmp%d.png' % frame
dup = 'tmp%d.png' % (i + frame)
subprocess.call(['cp', curr, dup])
framerate = (100 / min_ticks) if min_ticks else 10
subprocess.call('avconv -r {0} -i tmp%d.png -c:v libx264 -crf {1} -pix_fmt yuv420p \
-vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -y {2}.mp4'.format(framerate, quality, STORAGE_DIR + mp4_name).split())
subprocess.call(['rm'] + glob('tmp*.png'))
So, get the ticks in centiseconds for each frame of the gif (via identify), convert to multiple pngs, and then go through them while making duplicates based on the tick values. And don't you worry, the png files will still remain in consecutive order. Using the real FFmpeg is still the best way to go.
ffmpeg
has better GIF demuxing support (and improved GIF encoding). I recommend ditching avconv
and getting ffmpeg
(the real one from FFmpeg; not the old charlatan from Libav). A static build is easy, or you can of course compile.
ffmpeg -i in.gif -c:v libx264 -pix_fmt yuv420p -movflags +faststart out.mp4
See the FFmpeg Wiki: H.264 Encoding Guide for more examples.