OpenShot Library | libopenshot 0.3.2
FFmpegWriter.h
Go to the documentation of this file.
1
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
31 };
32
116 class FFmpegWriter : public WriterBase {
117 private:
118 std::string path;
119 int cache_size;
120 bool is_writing;
121 bool is_open;
122 int64_t video_timestamp;
123 int64_t audio_timestamp;
124
125 bool prepare_streams;
126 bool write_header;
127 bool write_trailer;
128
129 AVFormatContext* oc;
130 AVStream *audio_st, *video_st;
131 AVCodecContext *video_codec_ctx;
132 AVCodecContext *audio_codec_ctx;
133 SwsContext *img_convert_ctx;
134 int16_t *samples;
135 uint8_t *audio_outbuf;
136 uint8_t *audio_encoder_buffer;
137
138 int num_of_rescalers;
139 int rescaler_position;
140 std::vector<SwsContext *> image_rescalers;
141
142 int audio_outbuf_size;
143 int audio_input_frame_size;
144 int initial_audio_input_frame_size;
145 int audio_input_position;
146 int audio_encoder_buffer_size;
147 SWRCONTEXT *avr;
148 SWRCONTEXT *avr_planar;
149
150 /* Resample options */
151 int original_sample_rate;
152 int original_channels;
153
154 std::shared_ptr<openshot::Frame> last_frame;
155 std::deque<std::shared_ptr<openshot::Frame> > spooled_audio_frames;
156 std::deque<std::shared_ptr<openshot::Frame> > spooled_video_frames;
157
158 std::deque<std::shared_ptr<openshot::Frame> > queued_audio_frames;
159 std::deque<std::shared_ptr<openshot::Frame> > queued_video_frames;
160
161 std::deque<std::shared_ptr<openshot::Frame> > processed_frames;
162 std::deque<std::shared_ptr<openshot::Frame> > deallocate_frames;
163
164 std::map<std::shared_ptr<openshot::Frame>, AVFrame *> av_frames;
165
167 void add_avframe(std::shared_ptr<openshot::Frame> frame, AVFrame *av_frame);
168
170 AVStream *add_audio_stream();
171
173 AVStream *add_video_stream();
174
176 AVFrame *allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
177
179 void auto_detect_format();
180
182 void close_audio(AVFormatContext *oc, AVStream *st);
183
185 void close_video(AVFormatContext *oc, AVStream *st);
186
188 void flush_encoders();
189
191 void initialize_streams();
192
196 void InitScalers(int source_width, int source_height);
197
199 void open_audio(AVFormatContext *oc, AVStream *st);
200
202 void open_video(AVFormatContext *oc, AVStream *st);
203
205 void process_video_packet(std::shared_ptr<openshot::Frame> frame);
206
208 void write_audio_packets(bool is_final);
209
211 bool write_video_packet(std::shared_ptr<openshot::Frame> frame, AVFrame *frame_final);
212
214 void write_queued_frames();
215
216 public:
217
222 FFmpegWriter(const std::string& path);
223
225 void Close();
226
228 int GetCacheSize() { return cache_size; };
229
231 bool IsOpen() { return is_open; };
232
234 static bool IsValidCodec(std::string codec_name);
235
237 void Open();
238
240 void OutputStreamInfo();
241
244 void PrepareStreams();
245
247 void RemoveScalers();
248
252 void ResampleAudio(int sample_rate, int channels);
253
263 void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
264
274 void SetAudioOptions(std::string codec, int sample_rate, int bit_rate);
275
278 void SetCacheSize(int new_size) { cache_size = new_size; };
279
292 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);
293
306 void SetVideoOptions(std::string codec, int width, int height, openshot::Fraction fps, int bit_rate);
307
314 void SetOption(openshot::StreamType stream, std::string name, std::string value);
315
318 void WriteHeader();
319
324 void WriteFrame(std::shared_ptr<openshot::Frame> frame);
325
332 void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
333
336 void WriteTrailer();
337
338 };
339
340}
341
342#endif
Header file for FFmpegUtilities.
#define PixelFormat
#define SWRCONTEXT
Header file for ReaderBase class.
Header file for WriterBase class.
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition: FFmpegWriter.h:116
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...
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.
bool IsOpen()
Determine if writer is open or closed.
Definition: FFmpegWriter.h:231
void SetCacheSize(int new_size)
Set the cache size.
Definition: FFmpegWriter.h:278
void ResampleAudio(int sample_rate, int channels)
Set audio resample options.
int GetCacheSize()
Get the cache size (number of frames to queue before writing)
Definition: FFmpegWriter.h:228
void Open()
Open writer.
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)
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...
void RemoveScalers()
Remove & deallocate all software scalers.
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
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:70
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
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)
Definition: FFmpegWriter.h:28
@ AUDIO_STREAM
An audio stream (used to determine which type of stream)
Definition: FFmpegWriter.h:30
@ VIDEO_STREAM
A video stream (used to determine which type of stream)
Definition: FFmpegWriter.h:29