2013-09-12 23:41:49 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Unit tests for openshot::FFmpegReader
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2013-09-12 23:41:49 -05:00
|
|
|
|
2020-12-26 21:51:24 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <memory>
|
2025-09-12 22:57:26 -05:00
|
|
|
#include <set>
|
2026-02-24 12:06:42 -06:00
|
|
|
#include <algorithm>
|
2026-02-24 13:05:39 -06:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <ctime>
|
2020-12-26 21:51:24 -05:00
|
|
|
|
2022-06-17 15:07:16 -04:00
|
|
|
#include "openshot_catch.h"
|
2021-04-09 04:09:36 -04:00
|
|
|
|
2020-12-26 21:51:24 -05:00
|
|
|
#include "FFmpegReader.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2020-12-26 21:51:24 -05:00
|
|
|
#include "Frame.h"
|
|
|
|
|
#include "Timeline.h"
|
|
|
|
|
#include "Json.h"
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Invalid_Path", "[libopenshot][ffmpegreader]" )
|
2012-07-09 01:22:11 -05:00
|
|
|
{
|
2026-02-23 16:55:49 -06:00
|
|
|
// Check invalid path and error details
|
|
|
|
|
const std::string invalid_path = "/tmp/__openshot_missing_test_file__.mp4";
|
|
|
|
|
try {
|
|
|
|
|
FFmpegReader r(invalid_path);
|
|
|
|
|
FAIL("Expected InvalidFile for missing media path");
|
|
|
|
|
} catch (const InvalidFile& e) {
|
|
|
|
|
const std::string message = e.what();
|
|
|
|
|
CHECK(message.find("FFmpegReader could not open media file.") != std::string::npos);
|
|
|
|
|
CHECK(message.find(invalid_path) != std::string::npos);
|
|
|
|
|
}
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "GetFrame_Before_Opening", "[libopenshot][ffmpegreader]" )
|
2012-10-09 01:45:34 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2021-04-19 20:38:03 -04:00
|
|
|
std::stringstream path;
|
2015-09-28 22:05:50 -05:00
|
|
|
path << TEST_MEDIA_PATH << "piano.wav";
|
|
|
|
|
FFmpegReader r(path.str());
|
2012-10-09 01:45:34 -05:00
|
|
|
|
|
|
|
|
// Check invalid path
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK_THROWS_AS(r.GetFrame(1), ReaderClosed);
|
2012-07-09 01:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Check_Audio_File", "[libopenshot][ffmpegreader]" )
|
2012-07-09 01:22:11 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2021-04-19 20:38:03 -04:00
|
|
|
std::stringstream path;
|
2015-09-28 22:05:50 -05:00
|
|
|
path << TEST_MEDIA_PATH << "piano.wav";
|
|
|
|
|
FFmpegReader r(path.str());
|
2012-10-09 01:45:34 -05:00
|
|
|
r.Open();
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Get frame 1
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1);
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Get the number of channels and samples
|
2012-08-15 17:27:14 -05:00
|
|
|
float *samples = f->GetAudioSamples(0);
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Check audio properties
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->GetAudioChannelsCount() == 2);
|
2025-12-08 17:07:21 -06:00
|
|
|
CHECK(f->GetAudioSamplesCount() == 266);
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Check actual sample values (to be sure the waveform is correct)
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(samples[0] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[50] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[100] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[200] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[230] == Approx(0.16406f).margin(0.00001));
|
2025-12-08 17:07:21 -06:00
|
|
|
CHECK(samples[265] == Approx(-0.06250f).margin(0.00001));
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
2012-07-09 01:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Check_Video_File", "[libopenshot][ffmpegreader]" )
|
2012-07-09 01:44:36 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2021-04-19 20:38:03 -04:00
|
|
|
std::stringstream path;
|
2015-09-28 22:05:50 -05:00
|
|
|
path << TEST_MEDIA_PATH << "test.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
2012-10-09 01:45:34 -05:00
|
|
|
r.Open();
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Get frame 1
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1);
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Get the image data
|
2015-06-01 00:20:14 -07:00
|
|
|
const unsigned char* pixels = f->GetPixels(10);
|
|
|
|
|
int pixel_index = 112 * 4; // pixel 112 (4 bytes per pixel)
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Check image properties on scanline 10, pixel 112
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK((int)pixels[pixel_index] == Approx(21).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 1] == Approx(191).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 2] == Approx(0).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 3] == Approx(255).margin(5));
|
2012-07-09 01:44:36 -05:00
|
|
|
|
2019-05-31 19:02:28 -05:00
|
|
|
// Check pixel function
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->CheckPixel(10, 112, 21, 191, 0, 255, 5) == true);
|
|
|
|
|
CHECK_FALSE(f->CheckPixel(10, 112, 0, 0, 0, 0, 5));
|
2019-05-31 19:02:28 -05:00
|
|
|
|
2012-07-09 01:44:36 -05:00
|
|
|
// Get frame 1
|
|
|
|
|
f = r.GetFrame(2);
|
|
|
|
|
|
|
|
|
|
// Get the next frame
|
2012-08-15 17:27:14 -05:00
|
|
|
pixels = f->GetPixels(10);
|
2015-06-01 00:20:14 -07:00
|
|
|
pixel_index = 112 * 4; // pixel 112 (4 bytes per pixel)
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Check image properties on scanline 10, pixel 112
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK((int)pixels[pixel_index] == Approx(0).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 1] == Approx(96).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 2] == Approx(188).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 3] == Approx(255).margin(5));
|
2012-07-09 01:44:36 -05:00
|
|
|
|
2019-05-31 19:02:28 -05:00
|
|
|
// Check pixel function
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->CheckPixel(10, 112, 0, 96, 188, 255, 5) == true);
|
|
|
|
|
CHECK_FALSE(f->CheckPixel(10, 112, 0, 0, 0, 0, 5));
|
2019-05-31 19:02:28 -05:00
|
|
|
|
2014-04-03 22:35:25 -05:00
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Seek", "[libopenshot][ffmpegreader]" )
|
2014-04-03 22:35:25 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2021-04-19 20:38:03 -04:00
|
|
|
std::stringstream path;
|
2015-09-28 22:05:50 -05:00
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
2014-04-03 22:35:25 -05:00
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(300);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 300);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(301);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 301);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(315);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 315);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(275);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 275);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(270);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 270);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(500);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 500);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(100);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 100);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(600);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 600);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(1);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(700);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 700);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
2012-07-09 01:44:36 -05:00
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
|
|
|
|
|
|
|
|
|
}
|
2014-04-03 22:35:25 -05:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Frame_Rate", "[libopenshot][ffmpegreader]" )
|
2020-03-26 20:12:10 -04:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2021-04-19 20:38:03 -04:00
|
|
|
std::stringstream path;
|
2020-03-26 20:12:10 -04:00
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Verify detected frame rate
|
|
|
|
|
openshot::Fraction rate = r.info.fps;
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(rate.num == 24);
|
|
|
|
|
CHECK(rate.den == 1);
|
2020-03-26 20:12:10 -04:00
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 17:07:21 -06:00
|
|
|
TEST_CASE( "Duration_And_Length", "[libopenshot][ffmpegreader]" )
|
|
|
|
|
{
|
|
|
|
|
// Create a reader
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Duration and frame count should match (length derived from default Video_Preferred duration strategy)
|
|
|
|
|
CHECK(r.info.video_length == 1253);
|
|
|
|
|
CHECK(r.info.duration == Approx(52.208333f).margin(0.0005f));
|
|
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE( "Duration_Strategy_Video_Preferred", "[libopenshot][ffmpegreader]" )
|
|
|
|
|
{
|
|
|
|
|
// Create a reader preferring video duration (then falling back to audio/format)
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str(), DurationStrategy::VideoPreferred);
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Video stream duration should win, but still fall back to others if missing
|
|
|
|
|
CHECK(r.info.video_length == 1253);
|
|
|
|
|
CHECK(r.info.duration == Approx(52.208333).margin(0.0005f));
|
|
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
|
|
|
|
|
// Audio-only file should fallback to its audio duration
|
|
|
|
|
std::stringstream audio_path;
|
|
|
|
|
audio_path << TEST_MEDIA_PATH << "piano.wav";
|
|
|
|
|
FFmpegReader audio_reader(audio_path.str(), DurationStrategy::VideoPreferred);
|
|
|
|
|
audio_reader.Open();
|
|
|
|
|
|
|
|
|
|
CHECK(audio_reader.info.video_length == 132);
|
|
|
|
|
CHECK(audio_reader.info.duration == Approx(4.4f).margin(0.001f));
|
|
|
|
|
|
|
|
|
|
audio_reader.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE( "Duration_Strategy_Longest_Stream", "[libopenshot][ffmpegreader]" )
|
|
|
|
|
{
|
|
|
|
|
// Create a reader preferring the longest duration among streams/format
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str(), DurationStrategy::LongestStream);
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
CHECK(r.info.video_length == 1253);
|
|
|
|
|
CHECK(r.info.duration == Approx(52.208333).margin(0.0005f));
|
|
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
|
|
|
|
|
// Audio-only file should resolve to the audio duration
|
|
|
|
|
std::stringstream audio_path;
|
|
|
|
|
audio_path << TEST_MEDIA_PATH << "piano.wav";
|
|
|
|
|
FFmpegReader audio_reader(audio_path.str(), DurationStrategy::LongestStream);
|
|
|
|
|
audio_reader.Open();
|
|
|
|
|
|
|
|
|
|
CHECK(audio_reader.info.video_length == 132);
|
|
|
|
|
CHECK(audio_reader.info.duration == Approx(4.4f).margin(0.001f));
|
|
|
|
|
|
|
|
|
|
audio_reader.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE( "Duration_Strategy_Audio_Preferred", "[libopenshot][ffmpegreader]" )
|
|
|
|
|
{
|
|
|
|
|
// Create a reader preferring audio duration (then falling back to audio/format)
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str(), DurationStrategy::AudioPreferred);
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Audio stream duration should win, but still fall back to others if missing
|
|
|
|
|
CHECK(r.info.video_length == 1247);
|
|
|
|
|
CHECK(r.info.duration == Approx(51.958333).margin(0.0005f));
|
|
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
|
|
|
|
|
// Audio-only file should still resolve to the audio duration
|
|
|
|
|
std::stringstream audio_path;
|
|
|
|
|
audio_path << TEST_MEDIA_PATH << "piano.wav";
|
|
|
|
|
FFmpegReader audio_reader(audio_path.str(), DurationStrategy::AudioPreferred);
|
|
|
|
|
audio_reader.Open();
|
|
|
|
|
|
|
|
|
|
CHECK(audio_reader.info.video_length == 132);
|
|
|
|
|
CHECK(audio_reader.info.duration == Approx(4.4f).margin(0.001f));
|
|
|
|
|
|
|
|
|
|
audio_reader.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-12 22:57:26 -05:00
|
|
|
TEST_CASE( "GIF_TimeBase", "[libopenshot][ffmpegreader]" )
|
|
|
|
|
{
|
|
|
|
|
// Create a reader
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "animation.gif";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Verify basic info
|
|
|
|
|
CHECK(r.info.fps.num == 5);
|
|
|
|
|
CHECK(r.info.fps.den == 1);
|
|
|
|
|
CHECK(r.info.video_length == 20);
|
|
|
|
|
CHECK(r.info.duration == Approx(4.0f).margin(0.01));
|
|
|
|
|
|
|
|
|
|
auto frame_color = [](std::shared_ptr<Frame> f) {
|
|
|
|
|
const unsigned char* row = f->GetPixels(25);
|
|
|
|
|
return row[25 * 4];
|
|
|
|
|
};
|
|
|
|
|
auto expected_color = [](int frame) {
|
|
|
|
|
return (frame - 1) * 10;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= r.info.video_length; ++i) {
|
|
|
|
|
CHECK(frame_color(r.GetFrame(i)) == expected_color(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Multiple_Open_and_Close", "[libopenshot][ffmpegreader]" )
|
2015-02-19 01:03:22 -06:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2021-04-19 20:38:03 -04:00
|
|
|
std::stringstream path;
|
2015-09-28 22:05:50 -05:00
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
2015-02-19 01:03:22 -06:00
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame that requires a seek
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1200);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1200);
|
2015-02-19 01:03:22 -06:00
|
|
|
|
|
|
|
|
// Close and Re-open the reader
|
|
|
|
|
r.Close();
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(1);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1);
|
2015-02-19 01:03:22 -06:00
|
|
|
f = r.GetFrame(250);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 250);
|
2015-02-19 01:03:22 -06:00
|
|
|
|
|
|
|
|
// Close and Re-open the reader
|
|
|
|
|
r.Close();
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(750);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 750);
|
2015-02-19 01:03:22 -06:00
|
|
|
f = r.GetFrame(1000);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1000);
|
2015-02-19 01:03:22 -06:00
|
|
|
|
|
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
2020-03-26 20:12:10 -04:00
|
|
|
|
2026-02-24 12:06:42 -06:00
|
|
|
TEST_CASE( "Static_Image_PNG_Reports_Single_Image", "[libopenshot][ffmpegreader]" )
|
|
|
|
|
{
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "front.png";
|
|
|
|
|
FFmpegReader r(path.str(), DurationStrategy::VideoPreferred);
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
CHECK(r.info.has_video);
|
|
|
|
|
CHECK_FALSE(r.info.has_audio);
|
|
|
|
|
CHECK(r.info.has_single_image);
|
|
|
|
|
CHECK(r.info.video_length > 1);
|
|
|
|
|
CHECK(r.info.duration > 1000.0f);
|
|
|
|
|
|
|
|
|
|
auto f1 = r.GetFrame(1);
|
|
|
|
|
auto f2 = r.GetFrame(std::min(2, static_cast<int>(r.info.video_length)));
|
|
|
|
|
CHECK(f1->CheckPixel(50, 50,
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 0],
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 1],
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 2],
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 3],
|
|
|
|
|
0));
|
|
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE( "Static_Image_JPG_Reports_Single_Image", "[libopenshot][ffmpegreader]" )
|
|
|
|
|
{
|
|
|
|
|
// Generate a JPG fixture at runtime from a known PNG frame.
|
|
|
|
|
std::stringstream png_path;
|
|
|
|
|
png_path << TEST_MEDIA_PATH << "front.png";
|
|
|
|
|
FFmpegReader png_reader(png_path.str());
|
|
|
|
|
png_reader.Open();
|
|
|
|
|
|
|
|
|
|
auto png_frame = png_reader.GetFrame(1);
|
2026-02-24 13:05:39 -06:00
|
|
|
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
|
|
|
|
std::stringstream jpg_path;
|
|
|
|
|
jpg_path << "libopenshot-static-image-test-" << std::rand() << ".jpg";
|
|
|
|
|
REQUIRE(png_frame->GetImage()->save(jpg_path.str().c_str(), "JPG"));
|
2026-02-24 12:06:42 -06:00
|
|
|
png_reader.Close();
|
|
|
|
|
|
2026-02-24 13:05:39 -06:00
|
|
|
FFmpegReader jpg_reader(jpg_path.str(), DurationStrategy::VideoPreferred);
|
2026-02-24 12:06:42 -06:00
|
|
|
jpg_reader.Open();
|
|
|
|
|
|
|
|
|
|
CHECK(jpg_reader.info.has_video);
|
|
|
|
|
CHECK_FALSE(jpg_reader.info.has_audio);
|
|
|
|
|
CHECK(jpg_reader.info.has_single_image);
|
|
|
|
|
CHECK(jpg_reader.info.video_length > 1);
|
|
|
|
|
CHECK(jpg_reader.info.duration > 1000.0f);
|
|
|
|
|
|
|
|
|
|
auto f1 = jpg_reader.GetFrame(1);
|
|
|
|
|
auto f2 = jpg_reader.GetFrame(std::min(2, static_cast<int>(jpg_reader.info.video_length)));
|
|
|
|
|
CHECK(f1->CheckPixel(50, 50,
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 0],
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 1],
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 2],
|
|
|
|
|
f2->GetPixels(50)[50 * 4 + 3],
|
|
|
|
|
2));
|
|
|
|
|
|
|
|
|
|
jpg_reader.Close();
|
2026-02-24 13:05:39 -06:00
|
|
|
std::remove(jpg_path.str().c_str());
|
2026-02-24 12:06:42 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "verify parent Timeline", "[libopenshot][ffmpegreader]" )
|
2020-10-23 01:35:46 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2021-04-19 20:38:03 -04:00
|
|
|
std::stringstream path;
|
2020-10-23 01:35:46 -05:00
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Check size of frame image
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(r.GetFrame(1)->GetImage()->width() == 1280);
|
|
|
|
|
CHECK(r.GetFrame(1)->GetImage()->height() == 720);
|
2020-10-23 01:35:46 -05:00
|
|
|
r.GetFrame(1)->GetImage()->save("reader-1.png", "PNG");
|
|
|
|
|
|
|
|
|
|
// Create a Clip associated with this reader
|
|
|
|
|
Clip c1(&r);
|
|
|
|
|
c1.Open();
|
|
|
|
|
|
|
|
|
|
// Check size of frame image (should still be the same)
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(r.GetFrame(1)->GetImage()->width() == 1280);
|
|
|
|
|
CHECK(r.GetFrame(1)->GetImage()->height() == 720);
|
2020-10-23 01:35:46 -05:00
|
|
|
|
|
|
|
|
// Create Timeline
|
|
|
|
|
Timeline t1(640, 480, Fraction(30,1), 44100, 2, LAYOUT_STEREO);
|
|
|
|
|
t1.AddClip(&c1);
|
|
|
|
|
|
|
|
|
|
// Check size of frame image (it should now match the parent timeline)
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(r.GetFrame(1)->GetImage()->width() == 640);
|
|
|
|
|
CHECK(r.GetFrame(1)->GetImage()->height() == 360);
|
2023-04-19 16:08:36 -05:00
|
|
|
|
|
|
|
|
c1.Close();
|
|
|
|
|
t1.Close();
|
2020-10-23 01:35:46 -05:00
|
|
|
}
|
2021-04-19 20:38:03 -04:00
|
|
|
|
2021-05-07 23:05:16 -04:00
|
|
|
TEST_CASE( "DisplayInfo", "[libopenshot][ffmpegreader]" )
|
2021-04-19 20:38:03 -04:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
std::string expected(R"(----------------------------
|
|
|
|
|
----- File Information -----
|
|
|
|
|
----------------------------
|
|
|
|
|
--> Has Video: true
|
|
|
|
|
--> Has Audio: true
|
|
|
|
|
--> Has Single Image: false
|
2025-12-08 17:07:21 -06:00
|
|
|
--> Duration: 52.21 Seconds
|
2021-04-19 20:38:03 -04:00
|
|
|
--> File Size: 7.26 MB
|
|
|
|
|
----------------------------
|
|
|
|
|
----- Video Attributes -----
|
|
|
|
|
----------------------------
|
|
|
|
|
--> Width: 1280
|
|
|
|
|
--> Height: 720)");
|
|
|
|
|
|
|
|
|
|
// Store the DisplayInfo() text in 'output'
|
|
|
|
|
std::stringstream output;
|
|
|
|
|
r.DisplayInfo(&output);
|
|
|
|
|
|
|
|
|
|
// Compare a [0, expected.size()) substring of output to expected
|
2021-09-27 07:14:48 -04:00
|
|
|
CHECK(output.str().substr(0, expected.size()) == expected);
|
2021-04-19 20:38:03 -04:00
|
|
|
}
|
2022-11-14 12:03:33 -06:00
|
|
|
|
2022-11-15 14:33:03 -06:00
|
|
|
TEST_CASE( "Decoding AV1 Video", "[libopenshot][ffmpegreader]" )
|
2022-11-14 12:03:33 -06:00
|
|
|
{
|
2022-11-16 15:10:25 -06:00
|
|
|
try {
|
|
|
|
|
// Create a reader
|
|
|
|
|
std::stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "test_video_sync.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
2022-11-14 12:03:33 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1);
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Get the image data
|
|
|
|
|
const unsigned char *pixels = f->GetPixels(10);
|
|
|
|
|
int pixel_index = 112 * 4;
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Check image properties on scanline 10, pixel 112
|
|
|
|
|
CHECK((int) pixels[pixel_index] == Approx(0).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 1] == Approx(0).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 2] == Approx(0).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 3] == Approx(255).margin(5));
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
f = r.GetFrame(90);
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Get the image data
|
|
|
|
|
pixels = f->GetPixels(820);
|
|
|
|
|
pixel_index = 930 * 4;
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Check image properties on scanline 820, pixel 930
|
|
|
|
|
CHECK((int) pixels[pixel_index] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 1] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 2] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 3] == Approx(255).margin(5));
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
f = r.GetFrame(160);
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Get the image data
|
|
|
|
|
pixels = f->GetPixels(420);
|
|
|
|
|
pixel_index = 930 * 4;
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Check image properties on scanline 820, pixel 930
|
|
|
|
|
CHECK((int) pixels[pixel_index] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 1] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 2] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 3] == Approx(255).margin(5));
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
f = r.GetFrame(240);
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Get the image data
|
|
|
|
|
pixels = f->GetPixels(624);
|
|
|
|
|
pixel_index = 930 * 4;
|
2022-11-15 14:33:03 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Check image properties on scanline 820, pixel 930
|
|
|
|
|
CHECK((int) pixels[pixel_index] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 1] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 2] == Approx(255).margin(5));
|
|
|
|
|
CHECK((int) pixels[pixel_index + 3] == Approx(255).margin(5));
|
2022-11-14 12:03:33 -06:00
|
|
|
|
2022-11-16 15:10:25 -06:00
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
|
|
|
|
|
2022-11-16 17:11:30 -06:00
|
|
|
} catch (const InvalidCodec & e) {
|
|
|
|
|
// Ignore older FFmpeg versions which don't support AV1
|
2022-11-16 15:10:25 -06:00
|
|
|
} catch (const InvalidFile & e) {
|
|
|
|
|
// Ignore older FFmpeg versions which don't support AV1
|
|
|
|
|
}
|
2026-02-15 15:22:11 -06:00
|
|
|
}
|