Skip to content

Commit 4ddf775

Browse files
authored
Merge pull request #421 from ferdnyc/ffmpegwriter-opts
FFmpegWriter: Overload Set___Options() methods
2 parents aa8c891 + 895c2f0 commit 4ddf775

File tree

4 files changed

+115
-12
lines changed

4 files changed

+115
-12
lines changed

include/FFmpegReader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ namespace openshot {
7676
*
7777
* @code
7878
* // Create a reader for a video
79-
* FFmpegReader r("MyAwesomeVideo.webm");
79+
* openshot::FFmpegReader r("MyAwesomeVideo.webm");
8080
* r.Open(); // Open the reader
8181
*
8282
* // Get frame number 1 from the video
83-
* std::shared_ptr<Frame> f = r.GetFrame(1);
83+
* std::shared_ptr<openshot::Frame> f = r.GetFrame(1);
8484
*
8585
* // Now that we have an openshot::Frame object, lets have some fun!
8686
* f->Display(); // Display the frame on the screen

include/FFmpegWriter.h

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,19 @@ namespace openshot {
7575
* @code SIMPLE EXAMPLE
7676
*
7777
* // Create a reader for a video
78-
* FFmpegReader r("MyAwesomeVideo.webm");
79-
* r.Open(); // Open thetarget_ reader
78+
* openshot::FFmpegReader r("MyAwesomeVideo.webm");
79+
* r.Open(); // Open the target reader
8080
*
8181
* // Create a writer (which will create a WebM video)
82-
* FFmpegWriter w("/home/jonathan/NewVideo.webm");
82+
* openshot::FFmpegWriter w("/home/jonathan/NewVideo.webm");
8383
*
8484
* // Set options
85-
* w.SetAudioOptions(true, "libvorbis", 44100, 2, ChannelLayout::LAYOUT_STEREO, 128000); // Sample Rate: 44100, Channels: 2, Bitrate: 128000
86-
* w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000); // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
85+
*
86+
* // Sample Rate: 44100, Channels: 2, Bitrate: 128000
87+
* w.SetAudioOptions(true, "libvorbis", 44100, 2, openshot::ChannelLayout::LAYOUT_STEREO, 128000);
88+
*
89+
* // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
90+
* w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000);
8791
*
8892
* // Open the writer
8993
* w.Open();
@@ -102,15 +106,19 @@ namespace openshot {
102106
* @code ADVANCED WRITER EXAMPLE
103107
*
104108
* // Create a reader for a video
105-
* FFmpegReader r("MyAwesomeVideo.webm");
109+
* openshot::FFmpegReader r("MyAwesomeVideo.webm");
106110
* r.Open(); // Open the reader
107111
*
108112
* // Create a writer (which will create a WebM video)
109-
* FFmpegWriter w("/home/jonathan/NewVideo.webm");
113+
* openshot::FFmpegWriter w("/home/jonathan/NewVideo.webm");
110114
*
111115
* // Set options
112-
* w.SetAudioOptions(true, "libvorbis", 44100, 2, ChannelLayout::LAYOUT_STEREO, 128000); // Sample Rate: 44100, Channels: 2, Bitrate: 128000
113-
* w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000); // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
116+
*
117+
* // Sample Rate: 44100, Channels: 2, Bitrate: 128000
118+
* w.SetAudioOptions(true, "libvorbis", 44100, 2, openshot::ChannelLayout::LAYOUT_STEREO, 128000);
119+
*
120+
* // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
121+
* w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000);
114122
*
115123
* // Prepare Streams (Optional method that must be called before any SetOption calls)
116124
* w.PrepareStreams();
@@ -285,8 +293,21 @@ namespace openshot {
285293
/// @param channels The number of audio channels needed in this file
286294
/// @param channel_layout The 'layout' of audio channels (i.e. mono, stereo, surround, etc...)
287295
/// @param bit_rate The audio bit rate used during encoding
296+
///
297+
/// \note This is an overloaded function.
288298
void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
289299

300+
/// @brief Set audio export options.
301+
///
302+
/// Enables the stream and configures a default 2-channel stereo layout.
303+
///
304+
/// @param codec The codec used to encode the audio for this file
305+
/// @param sample_rate The number of audio samples needed in this file
306+
/// @param bit_rate The audio bit rate used during encoding
307+
///
308+
/// \note This is an overloaded function.
309+
void SetAudioOptions(std::string codec, int sample_rate, int bit_rate);
310+
290311
/// @brief Set the cache size
291312
/// @param new_size The number of frames to queue before writing to the file
292313
void SetCacheSize(int new_size) { cache_size = new_size; };
@@ -301,10 +322,27 @@ namespace openshot {
301322
/// @param interlaced Does this video need to be interlaced?
302323
/// @param top_field_first Which frame should be used as the top field?
303324
/// @param bit_rate The video bit rate used during encoding
325+
///
326+
/// \note This is an overloaded function.
304327
void SetVideoOptions(bool has_video, std::string codec, openshot::Fraction fps, int width, int height, openshot::Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate);
305328

329+
/// @brief Set video export options.
330+
///
331+
/// Enables the stream and configures non-interlaced video with a 1:1 pixel aspect ratio.
332+
///
333+
/// @param codec The codec used to encode the images in this video
334+
/// @param width The width in pixels of this video
335+
/// @param height The height in pixels of this video
336+
/// @param fps The number of frames per second
337+
/// @param bit_rate The video bit rate used during encoding
338+
///
339+
/// \note This is an overloaded function.
340+
/// \warning Observe the argument order, which is consistent with the openshot::Timeline constructor, but differs from the other signature.
341+
void SetVideoOptions(std::string codec, int width, int height, openshot::Fraction fps, int bit_rate);
342+
306343
/// @brief Set custom options (some codecs accept additional params). This must be called after the
307344
/// PrepareStreams() method, otherwise the streams have not been initialized yet.
345+
///
308346
/// @param stream The stream (openshot::StreamType) this option should apply to
309347
/// @param name The name of the option you want to set (i.e. qmin, qmax, etc...)
310348
/// @param value The new value of this option
@@ -316,12 +354,16 @@ namespace openshot {
316354

317355
/// @brief Add a frame to the stack waiting to be encoded.
318356
/// @param frame The openshot::Frame object to write to this image
357+
///
358+
/// \note This is an overloaded function.
319359
void WriteFrame(std::shared_ptr<openshot::Frame> frame);
320360

321361
/// @brief Write a block of frames from a reader
322362
/// @param reader A openshot::ReaderBase object which will provide frames to be written
323363
/// @param start The starting frame number of the reader
324364
/// @param length The number of frames to write
365+
///
366+
/// \note This is an overloaded function.
325367
void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
326368

327369
/// @brief Write the file trailer (after all frames are written). This is called automatically

src/FFmpegWriter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ void FFmpegWriter::SetVideoOptions(bool has_video, std::string codec, Fraction f
277277
info.has_video = has_video;
278278
}
279279

280+
// Set video export options (overloaded function)
281+
void FFmpegWriter::SetVideoOptions(std::string codec, int width, int height, Fraction fps, int bit_rate) {
282+
// Call full signature with some default parameters
283+
FFmpegWriter::SetVideoOptions(true, codec, fps, width, height,
284+
openshot::Fraction(1, 1), false, true, bit_rate);
285+
}
286+
287+
280288
// Set audio export options
281289
void FFmpegWriter::SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, ChannelLayout channel_layout, int bit_rate) {
282290
// Set audio options
@@ -312,6 +320,15 @@ void FFmpegWriter::SetAudioOptions(bool has_audio, std::string codec, int sample
312320
info.has_audio = has_audio;
313321
}
314322

323+
324+
// Set audio export options (overloaded function)
325+
void FFmpegWriter::SetAudioOptions(std::string codec, int sample_rate, int bit_rate) {
326+
// Call full signature with some default parameters
327+
FFmpegWriter::SetAudioOptions(true, codec, sample_rate, 2,
328+
openshot::LAYOUT_STEREO, bit_rate);
329+
}
330+
331+
315332
// Set custom options (some codecs accept additional params)
316333
void FFmpegWriter::SetOption(StreamType stream, std::string name, std::string value) {
317334
// Declare codec context

tests/FFmpegWriter_Tests.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
using namespace std;
3737
using namespace openshot;
3838

39-
TEST(FFmpegWriter_Test_Webm)
39+
SUITE(FFMpegWriter) {
40+
TEST(Webm)
4041
{
4142
// Reader
4243
stringstream path;
@@ -82,3 +83,46 @@ TEST(FFmpegWriter_Test_Webm)
8283
CHECK_CLOSE(23, (int)pixels[pixel_index + 2], 5);
8384
CHECK_CLOSE(255, (int)pixels[pixel_index + 3], 5);
8485
}
86+
87+
TEST(Options_Overloads)
88+
{
89+
// Reader
90+
stringstream path;
91+
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
92+
FFmpegReader r(path.str());
93+
r.Open();
94+
95+
/* WRITER ---------------- */
96+
FFmpegWriter w("output1.mp4");
97+
98+
// Set options
99+
w.SetAudioOptions("aac", 48000, 192000);
100+
w.SetVideoOptions("libx264", 1280, 720, Fraction(30,1), 5000000);
101+
102+
// Open writer
103+
w.Open();
104+
105+
// Write some frames
106+
w.WriteFrame(&r, 24, 50);
107+
108+
// Close writer & reader
109+
w.Close();
110+
r.Close();
111+
112+
FFmpegReader r1("output1.mp4");
113+
r1.Open();
114+
115+
// Verify implied settings
116+
CHECK_EQUAL(true, r1.info.has_audio);
117+
CHECK_EQUAL(true, r1.info.has_video);
118+
119+
CHECK_EQUAL(2, r1.GetFrame(1)->GetAudioChannelsCount());
120+
CHECK_EQUAL(LAYOUT_STEREO, r1.info.channel_layout);
121+
122+
CHECK_EQUAL(1, r1.info.pixel_ratio.num);
123+
CHECK_EQUAL(1, r1.info.pixel_ratio.den);
124+
CHECK_EQUAL(false, r1.info.interlaced_frame);
125+
CHECK_EQUAL(true, r1.info.top_field_first);
126+
}
127+
128+
} // SUITE()

0 commit comments

Comments
 (0)