Skip to main content

Add a script to convert GIF images to MP4 files

ID
3367209
date
2025-03-03 22:39:04+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
ad4bec5
message
Add a script to convert GIF images to MP4 files
changed files
2 files, 46 additions, 1 deletion

Changed files

images/README.md (10983) → images/README.md (11327)

diff --git a/images/README.md b/images/README.md
index 876a9eb..7830644 100644
--- a/images/README.md
+++ b/images/README.md
@@ -50,6 +50,10 @@ scripts = [
         """,
     },
     {
+        "usage": "convert_gif_to_mp4 GIF_PATH",
+        "description": "convert an animated GIF to an MP4 file"
+    },
+    {
         "name": "copycrop.py",
         "description": """
         this script will “copy” the crop from one image pair to another.
@@ -202,6 +206,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/images/convert_gif_to_mp4">
+      <code>convert_gif_to_mp4 GIF_PATH</code>
+    </a>
+  </dt>
+  <dd>
+    convert an animated GIF to an MP4 file
+  </dd>
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/images/copycrop.py">
       <code>copycrop.py</code>
     </a>
@@ -339,4 +352,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     I don’t use this script very often, but I checked it in because I thought it was a neat trick I didn’t want to forget.
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: 72dd4bb73da2c34197f1d6816eacb980) -->
+<!-- [[[end]]] (checksum: 729cb96a4ef2033ad2974a1b63327f0b) -->

images/convert_gif_to_mp4 (0) → images/convert_gif_to_mp4 (525)

diff --git a/images/convert_gif_to_mp4 b/images/convert_gif_to_mp4
new file mode 100755
index 0000000..85da408
--- /dev/null
+++ b/images/convert_gif_to_mp4
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+# Convert an animated GIF to an MP4 file
+#
+# See https://alexwlchan.net/til/2024/convert-an-animated-gif-to-mp4/
+
+set -o errexit
+set -o nounset
+
+if (( $# != 1 ))
+then
+  echo "Usage: $0 GIF_PATH" >&2
+  exit 1
+fi
+
+GIF_PATH="$1"
+MP4_PATH="${GIF_PATH//gif/mp4}"
+
+if [[ "$GIF_PATH" = "$MP4_PATH" ]]
+then
+  echo "Unable to pick MP4 path for $GIF_PATH" >&2
+  exit 1
+fi
+
+ffmpeg \
+  -i "$GIF_PATH" \
+  -movflags faststart \
+  -pix_fmt yuv420p \
+  -hide_banner \
+  -loglevel error \
+  "$MP4_PATH"
+
+echo "$MP4_PATH"