Skip to main content

Merge pull request #8 from alexwlchan/animated-gif-with-odd-dimensions

ID
d260488
date
2024-08-20 14:02:51+00:00
author
Alex Chan <alex@alexwlchan.net>
parents
1be2a39, 39a0e77
message
Merge pull request #8 from alexwlchan/animated-gif-with-odd-dimensions

Fix a bug when creating animated GIF thumbnails with odd width/height
changed files
4 files, 36 additions, 3 deletions

Changed files

CHANGELOG.md (54) → CHANGELOG.md (207)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 59f4f29..d5ccecf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Changelog
 
+## v1.0.1 - 2024-08-20
+
+Fix a bug where the tool couldn't create thumbnails of animated GIFs if the thumbnail would have an odd width/height dimension.
+
 ## v1.0.0 - 2024-08-20
 
 Initial release.

Cargo.lock (37401) → Cargo.lock (37401)

diff --git a/Cargo.lock b/Cargo.lock
index 48b7873..812bb3e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -295,7 +295,7 @@ dependencies = [
 
 [[package]]
 name = "create_thumbnail"
-version = "1.0.0"
+version = "1.0.1"
 dependencies = [
  "assert_cmd",
  "clap",

Cargo.toml (209) → Cargo.toml (209)

diff --git a/Cargo.toml b/Cargo.toml
index 36478b2..83dbea8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "create_thumbnail"
-version = "1.0.0"
+version = "1.0.1"
 edition = "2021"
 
 [dependencies]

src/create_thumbnail.rs (6886) → src/create_thumbnail.rs (7868)

diff --git a/src/create_thumbnail.rs b/src/create_thumbnail.rs
index 9a4ae53..6650362 100644
--- a/src/create_thumbnail.rs
+++ b/src/create_thumbnail.rs
@@ -59,6 +59,18 @@ mod test_create_thumbnail {
     }
 
     #[test]
+    fn creates_an_animated_gif_thumbnail_with_odd_width() {
+        let gif_path = PathBuf::from("src/tests/animated_squares.gif");
+        let out_dir = test_dir();
+        let target = TargetDimension::MaxWidth(15);
+
+        let thumbnail_path = create_thumbnail(&gif_path, &out_dir, target).unwrap();
+
+        assert_eq!(thumbnail_path, out_dir.join("animated_squares.mp4"));
+        assert!(thumbnail_path.exists());
+    }
+
+    #[test]
     fn creates_a_static_gif_thumbnail() {
         let img_path = PathBuf::from("src/tests/yellow.gif");
         let out_dir = test_dir();
@@ -137,6 +149,15 @@ mod test_create_thumbnail {
     }
 }
 
+/// Return this value if it's even, or the closest value which is even.
+fn ensure_even(x: u32) -> u32 {
+    if x % 2 == 0 {
+        x
+    } else {
+        x + 1
+    }
+}
+
 /// Create a thumbnail for an animated GIF.
 ///
 /// This will use `ffmpeg` to create an MP4 file of the desired dimensions
@@ -157,7 +178,15 @@ pub fn create_animated_gif_thumbnail(
     let file_name = gif_path.file_name().unwrap();
     let thumbnail_path = out_dir.join(file_name).with_extension("mp4");
 
-    let dimension_str = format!("scale={}:{}", width, height);
+    // There's a subtlety here with ffmpeg I don't understand fully -- if
+    // the width/height aren't even, it doesn't create the MP4, instead
+    // failing with the error:
+    //
+    //     width not divisible by 2
+    //
+    // I don't usually need these files to be pixel-perfect width, so
+    // fudging by a single pixel or two is fine.
+    let dimension_str = format!("scale={}:{}", ensure_even(width), ensure_even(height));
 
     let output = Command::new("ffmpeg")
         .args([