Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created January 31, 2020 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisallick/a4680193189cfdb7e88b6b087b9753d4 to your computer and use it in GitHub Desktop.
Save chrisallick/a4680193189cfdb7e88b6b087b9753d4 to your computer and use it in GitHub Desktop.
#
# run from root
#
# writes to a folder "videos" folder needs to be created.
# requires ffmpeg installed
#
# $>ruby gifToMP4.rb
#
#
# references:
#
# https://www.clas.kitasato-u.ac.jp/~fujiwara/Mathematica/GIFtoMP4.html
# To convert animation GIF to MP4 by ffmpeg, use the following command
#
# ffmpeg -r 30 -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4
# Here ``-r 30'' specify the frame rate 30 frames/sec. So if you want 10 sec movie with frame rate 30/sec, you make GIF animation that has total 300 frames, then use ``-r 30''.
#
# The references are http://unix.stackexchange.com/questions/40638/how-to-do-i-convert-an-animated-gif-to-an-mp4-or-mv4-on-the-command-line and ``man ffmpeg''.
#
# https://unix.stackexchange.com/questions/40638/how-to-do-i-convert-an-animated-gif-to-an-mp4-or-mv4-on-the-command-line
files = Dir.entries("./gifs").select {|f| !File.directory? f}
dope = 0
files.each do |file|
if File.extname(file) == ".gif"
#cmd = "ffmpeg -i ./gifs/#{file} -c copy -an ./videos/#{File.basename(file, '.*')}.mp4"
#cmd = "convert ./gifs/#{file} ./videos/#{File.basename(file, '.*')}.mp4"
cmd = "ffmpeg -r 30 -i ./gifs/#{file} -movflags faststart -pix_fmt yuv420p -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' ./videos/#{File.basename(file, '.*')}.mp4"
puts cmd
system "#{cmd}"
sleep 0.250
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment