OpenShot Library | libopenshot 1.1.1
Loading...
Searching...
No Matches
FFmpegWriter.h
Go to the documentation of this file.
1
11
12// Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
13//
14// SPDX-License-Identifier: LGPL-3.0-or-later
15
16#ifndef OPENSHOT_FFMPEG_WRITER_H
17#define OPENSHOT_FFMPEG_WRITER_H
18
19#include "ReaderBase.h"
20#include "WriterBase.h"
21
22// Include FFmpeg headers and macros
23#include "FFmpegUtilities.h"
24
25namespace openshot {
26
32
116 class FFmpegWriter : public WriterBase {
117 private:
118 std::string path;
119 bool is_writing;
120 bool is_open;
121 int64_t video_timestamp;
122 int64_t audio_timestamp;
123
124 bool prepare_streams;
125 bool write_header;
126 bool write_trailer;
127 bool allow_b_frames;
128 bool testing;
129
130 AVFormatContext* oc;
131 AVStream *audio_st, *video_st;
132 AVCodecContext *video_codec_ctx;
133 AVCodecContext *audio_codec_ctx;
134 SwsContext *img_convert_ctx;
135 int16_t *samples;
136 uint8_t *audio_outbuf;
137 uint8_t *audio_encoder_buffer;
138
139 AVFrame *persistent_src_frame = nullptr;
140 AVFrame *persistent_dst_frame = nullptr;
141 uint8_t *persistent_dst_buffer = nullptr;
142 int persistent_dst_size = 0;
143
144 int audio_outbuf_size;
145 int audio_input_frame_size;
146 int initial_audio_input_frame_size;
147 int audio_input_position;
148 int audio_encoder_buffer_size;
149 SWRCONTEXT *avr;
150 SWRCONTEXT *avr_planar;
151
152 /* Resample options */
153 int original_sample_rate;
154 int original_channels;
155
156 std::shared_ptr<openshot::Frame> last_frame;
157 std::map<std::shared_ptr<openshot::Frame>, AVFrame *> av_frames;
158
160 void add_avframe(std::shared_ptr<openshot::Frame> frame, AVFrame *av_frame);
161
163 AVStream *add_audio_stream();
164
166 AVStream *add_video_stream();
167
169 AVFrame *allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
170
172 void auto_detect_format();
173
175 void close_audio(AVFormatContext *oc, AVStream *st);
176
178 void close_video(AVFormatContext *oc, AVStream *st);
179
181 void flush_encoders();
182
184 void initialize_streams();
185
187 void open_audio(AVFormatContext *oc, AVStream *st);
188
190 void open_video(AVFormatContext *oc, AVStream *st);
191
193 void process_video_packet(std::shared_ptr<openshot::Frame> frame);
194
196 void write_audio_packets(bool is_final, std::shared_ptr<openshot::Frame> frame);
197
199 bool write_video_packet(std::shared_ptr<openshot::Frame> frame, AVFrame *frame_final);
200
202 void write_frame(std::shared_ptr<Frame> frame);
203
204 public:
205
210 FFmpegWriter(const std::string& path);
211
213 void Close();
214
216 bool IsOpen() { return is_open; };
217
219 static bool IsValidCodec(std::string codec_name);
220
228 static std::string GetEncoderInfo(std::string codec_name);
229
235 static std::string GetFormatInfo(std::string format_name);
236
242 static std::string GetFormats();
243
247 static std::string GetEncoders(std::string media_type, std::string format_name = "");
248
253 static int IsCodecSupportedByFormat(std::string format_name, std::string codec_name);
254
256 void Open();
257
267 std::string Test();
268
270 void OutputStreamInfo();
271
274 void PrepareStreams();
275
279 void ResampleAudio(int sample_rate, int channels);
280
290 void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
291
301 void SetAudioOptions(std::string codec, int sample_rate, int bit_rate);
302
315 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);
316
329 void SetVideoOptions(std::string codec, int width, int height, openshot::Fraction fps, int bit_rate);
330
337 void SetOption(openshot::StreamType stream, std::string name, std::string value);
338
341 void WriteHeader();
342
347 void WriteFrame(std::shared_ptr<openshot::Frame> frame);
348
355 void WriteFrameAt(std::shared_ptr<openshot::Frame> frame, int64_t frame_number);
356
363 void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
364
367 void WriteTrailer();
368
374 void AddSphericalMetadata(const std::string& projection="equirectangular", float yaw_deg=0.0f, float pitch_deg=0.0f, float roll_deg=0.0f);
375
376 };
377
378} // namespace openshot
379
380#endif
Header file for FFmpegUtilities.
#define PixelFormat
#define SWRCONTEXT
Header file for ReaderBase class.
Header file for WriterBase class.
static std::string GetEncoders(std::string media_type, std::string format_name="")
List the available encoders as a JSON array.
void Close()
Close the writer.
void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate)
Set audio export options.
void SetOption(openshot::StreamType stream, std::string name, std::string value)
Set custom options (some codecs accept additional params). This must be called after the PrepareStrea...
static std::string GetFormats()
List the file extensions the writer can export to, as a JSON array.
void PrepareStreams()
Prepare & initialize streams and open codecs. This method is called automatically by the Open() metho...
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)
Set video export options.
void WriteFrameAt(std::shared_ptr< openshot::Frame > frame, int64_t frame_number)
Add a frame at a specific output video frame number.
bool IsOpen()
Determine if writer is open or closed.
static std::string GetFormatInfo(std::string format_name)
Describe an output format as JSON (name, extensions, and default codecs).
static int IsCodecSupportedByFormat(std::string format_name, std::string codec_name)
Determine if an output format can store a codec.
void ResampleAudio(int sample_rate, int channels)
Set audio resample options.
static std::string GetEncoderInfo(std::string codec_name)
Describe an encoder's capabilities as JSON.
void Open()
Open writer.
void AddSphericalMetadata(const std::string &projection="equirectangular", float yaw_deg=0.0f, float pitch_deg=0.0f, float roll_deg=0.0f)
Add spherical (360°) video metadata to the video stream.
void WriteHeader()
Write the file header (after the options are set). This method is called automatically by the Open() ...
FFmpegWriter(const std::string &path)
Constructor for FFmpegWriter. Throws an exception on failure to open path.
static bool IsValidCodec(std::string codec_name)
Determine if codec name is valid.
void OutputStreamInfo()
Output the ffmpeg info about this format, streams, and codecs (i.e. dump format).
std::string Test()
Check the current settings without writing a file, and return the result as JSON.
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
void WriteTrailer()
Write the file trailer (after all frames are written). This is called automatically by the Close() me...
This class represents a fraction.
Definition Fraction.h:30
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
WriterBase()
Constructor for WriterBase class, many things are initialized here.
This namespace is the default namespace for all code in the openshot library.
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
StreamType
This enumeration designates the type of stream when encoding (video or audio).
@ AUDIO_STREAM
An audio stream (used to determine which type of stream).
@ VIDEO_STREAM
A video stream (used to determine which type of stream).