process mp4 file thumbnailing

This commit is contained in:
Dylan Knutson
2025-08-14 18:16:14 +00:00
parent 981bea5016
commit ca937eb2bc
5 changed files with 67 additions and 39 deletions

View File

@@ -21,6 +21,9 @@ RSpec.describe LoadedMedia do
.join("test/fixtures/files/images/e621-5421402-animated.webm")
.to_s
end
let(:mp4_fixture_path) do
Rails.root.join("test/fixtures/files/images/bsky-3l6tnjkcgw72y.mp4").to_s
end
let(:thumbnail_options) do
LoadedMedia::ThumbnailOptions.new(
@@ -53,14 +56,23 @@ RSpec.describe LoadedMedia do
end
context "with a webm file" do
it "creates a LoadedMedia::Webm instance" do
it "creates a LoadedMedia::WebmOrMp4 instance" do
media = LoadedMedia.from_file("video/webm", webm_fixture_path)
expect(media).to be_a(LoadedMedia::Webm)
expect(media).to be_a(LoadedMedia::WebmOrMp4)
expect(media.num_frames).to eq(908)
end
end
context "with an mp4 file" do
it "creates a LoadedMedia::WebmOrMp4 instance" do
media = LoadedMedia.from_file("video/mp4", mp4_fixture_path)
expect(media).to be_a(LoadedMedia::WebmOrMp4)
expect(media.num_frames).to eq(22)
end
end
context "with an unsupported file type" do
it "returns nil" do
media = LoadedMedia.from_file("application/pdf", jpeg_fixture_path)
@@ -155,5 +167,30 @@ RSpec.describe LoadedMedia do
expect(FileUtils.compare_file(paths[1], paths[2])).to be false
end
end
context "with an mp4 file" do
it "can extract a frame and save a thumbnail" do
media = LoadedMedia.from_file("video/mp4", mp4_fixture_path)
output_path = make_output_path(0)
media.write_frame_thumbnail(0, output_path, thumbnail_options)
end
it "creates different frames" do
media = LoadedMedia.from_file("video/mp4", mp4_fixture_path)
paths =
[0, 10, media.num_frames - 1].map do |frame|
output_path = make_output_path(frame)
media.write_frame_thumbnail(frame, output_path, thumbnail_options)
output_path
end
expect(paths.uniq.length).to eq(3)
expect(FileUtils.compare_file(paths[0], paths[1])).to be false
expect(FileUtils.compare_file(paths[0], paths[2])).to be false
expect(FileUtils.compare_file(paths[1], paths[2])).to be false
end
end
end
end