diff --git a/Cargo.toml b/Cargo.toml index ce1f598..f82d756 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ readme = "README.md" license = "Apache-2.0" authors = ["trivernis "] description = "An image thumbnail creation library" -version = "0.2.2" +version = "0.2.3" edition = "2018" repository = "https://github.com/Trivernis/thumbnailer" @@ -14,8 +14,12 @@ repository = "https://github.com/Trivernis/thumbnailer" webp = "0.2.0" mime = "0.3.16" rayon = "1.5.1" -ffmpeg-next = "4.4.0" +ffmpeg-next = {version = "4.4.0", optional=true} tempfile = "3.2.0" [dependencies.image] -version = "0.23.14" \ No newline at end of file +version = "0.23.14" + +[features] +default = ["ffmpeg"] +ffmpeg = ["ffmpeg-next"] \ No newline at end of file diff --git a/src/error.rs b/src/error.rs index ad4e59b..1c2ec1a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,6 +17,7 @@ pub enum ThumbError { NullVideo, + #[cfg(feature = "ffmpeg")] FFMPEG(ffmpeg_next::Error), } @@ -28,6 +29,8 @@ impl Display for ThumbError { ThumbError::Decode => write!(f, "failed to decode image"), ThumbError::Unsupported(mime) => write!(f, "Unsupported media type {}", mime), ThumbError::NullVideo => write!(f, "no video data found in file"), + + #[cfg(feature = "ffmpeg")] ThumbError::FFMPEG(e) => write!(f, "ffmpeg error: {}", e), } } @@ -38,7 +41,10 @@ impl std::error::Error for ThumbError { match self { ThumbError::IO(e) => e.source(), ThumbError::Image(i) => i.source(), + + #[cfg(feature = "ffmpeg")] ThumbError::FFMPEG(e) => e.source(), + _ => None, } } @@ -56,6 +62,7 @@ impl From for ThumbError { } } +#[cfg(feature = "ffmpeg")] impl From for ThumbError { fn from(e: ffmpeg_next::Error) -> Self { Self::FFMPEG(e) diff --git a/src/formats/mod.rs b/src/formats/mod.rs index 7418c1d..7f91f29 100644 --- a/src/formats/mod.rs +++ b/src/formats/mod.rs @@ -1,17 +1,21 @@ use crate::error::{ThumbError, ThumbResult}; use crate::formats::image_format::read_image; -use crate::formats::video_format::get_video_frame; use image::DynamicImage; use mime::Mime; use std::io::{BufRead, Seek}; +#[cfg(feature = "ffmpeg")] +use crate::formats::video_format::get_video_frame; + pub mod image_format; +#[cfg(feature = "ffmpeg")] pub mod video_format; /// Reads the buffer content into an image that can be used for thumbnail generation pub fn get_base_image(reader: R, mime: Mime) -> ThumbResult { match mime.type_() { mime::IMAGE => read_image(reader, mime), + #[cfg(feature = "ffmpeg")] mime::VIDEO => get_video_frame(reader, mime), _ => Err(ThumbError::Unsupported(mime)), } diff --git a/tests/video_reading.rs b/tests/video_reading.rs index 4d6f225..a0d88c0 100644 --- a/tests/video_reading.rs +++ b/tests/video_reading.rs @@ -8,7 +8,7 @@ const VIDEO_BYTES: &'static [u8] = include_bytes!("assets/test.mp4"); #[test] fn it_creates_thumbnails_for_mp4() { let reader = Cursor::new(VIDEO_BYTES); - create_thumbnails( + let result = create_thumbnails( reader, Mime::from_str("video/mp4").unwrap(), [ @@ -16,6 +16,10 @@ fn it_creates_thumbnails_for_mp4() { ThumbnailSize::Medium, ThumbnailSize::Large, ], - ) - .unwrap(); + ); + #[cfg(feature = "ffmpeg")] + result.unwrap(); + + #[cfg(not(feature = "ffmpeg"))] + assert!(result.is_err()) }