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 
25 namespace openshot {
26 
28  enum StreamType {
31  };
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 
128  AVFormatContext* oc;
129  AVStream *audio_st, *video_st;
130  AVCodecContext *video_codec_ctx;
131  AVCodecContext *audio_codec_ctx;
132  SwsContext *img_convert_ctx;
133  int16_t *samples;
134  uint8_t *audio_outbuf;
135  uint8_t *audio_encoder_buffer;
136 
137  int num_of_rescalers;
138  int rescaler_position;
139  std::vector<SwsContext *> image_rescalers;
140 
141  int audio_outbuf_size;
142  int audio_input_frame_size;
143  int initial_audio_input_frame_size;
144  int audio_input_position;
145  int audio_encoder_buffer_size;
146  SWRCONTEXT *avr;
147  SWRCONTEXT *avr_planar;
148 
149  /* Resample options */
150  int original_sample_rate;
151  int original_channels;
152 
153  std::shared_ptr<openshot::Frame> last_frame;
154  std::map<std::shared_ptr<openshot::Frame>, AVFrame *> av_frames;
155 
157  void add_avframe(std::shared_ptr<openshot::Frame> frame, AVFrame *av_frame);
158 
160  AVStream *add_audio_stream();
161 
163  AVStream *add_video_stream();
164 
166  AVFrame *allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
167 
169  void auto_detect_format();
170 
172  void close_audio(AVFormatContext *oc, AVStream *st);
173 
175  void close_video(AVFormatContext *oc, AVStream *st);
176 
178  void flush_encoders();
179 
181  void initialize_streams();
182 
186  void InitScalers(int source_width, int source_height);
187 
189  void open_audio(AVFormatContext *oc, AVStream *st);
190 
192  void open_video(AVFormatContext *oc, AVStream *st);
193 
195  void process_video_packet(std::shared_ptr<openshot::Frame> frame);
196 
198  void write_audio_packets(bool is_final, std::shared_ptr<openshot::Frame> frame);
199 
201  bool write_video_packet(std::shared_ptr<openshot::Frame> frame, AVFrame *frame_final);
202 
204  void write_frame(std::shared_ptr<Frame> frame);
205 
206  public:
207 
212  FFmpegWriter(const std::string& path);
213 
215  void Close();
216 
218  bool IsOpen() { return is_open; };
219 
221  static bool IsValidCodec(std::string codec_name);
222 
224  void Open();
225 
227  void OutputStreamInfo();
228 
231  void PrepareStreams();
232 
234  void RemoveScalers();
235 
239  void ResampleAudio(int sample_rate, int channels);
240 
250  void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
251 
261  void SetAudioOptions(std::string codec, int sample_rate, int bit_rate);
262 
275  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);
276 
289  void SetVideoOptions(std::string codec, int width, int height, openshot::Fraction fps, int bit_rate);
290 
297  void SetOption(openshot::StreamType stream, std::string name, std::string value);
298 
301  void WriteHeader();
302 
307  void WriteFrame(std::shared_ptr<openshot::Frame> frame);
308 
315  void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
316 
319  void WriteTrailer();
320 
321  };
322 
323 }
324 
325 #endif
openshot::AUDIO_STREAM
@ AUDIO_STREAM
An audio stream (used to determine which type of stream)
Definition: FFmpegWriter.h:30
FFmpegUtilities.h
Header file for FFmpegUtilities.
WriterBase.h
Header file for WriterBase class.
openshot::FFmpegWriter::ResampleAudio
void ResampleAudio(int sample_rate, int channels)
Set audio resample options.
Definition: FFmpegWriter.cpp:2271
openshot::FFmpegWriter::OutputStreamInfo
void OutputStreamInfo()
Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
Definition: FFmpegWriter.cpp:2238
PixelFormat
#define PixelFormat
Definition: FFmpegUtilities.h:100
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::FFmpegWriter::FFmpegWriter
FFmpegWriter(const std::string &path)
Constructor for FFmpegWriter. Throws an exception on failure to open path.
Definition: FFmpegWriter.cpp:75
openshot::FFmpegWriter::Open
void Open()
Open writer.
Definition: FFmpegWriter.cpp:95
openshot::FFmpegWriter::SetVideoOptions
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.
Definition: FFmpegWriter.cpp:163
openshot::FFmpegWriter
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition: FFmpegWriter.h:116
openshot::FFmpegWriter::IsOpen
bool IsOpen()
Determine if writer is open or closed.
Definition: FFmpegWriter.h:218
openshot::FFmpegWriter::SetOption
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...
Definition: FFmpegWriter.cpp:333
SWRCONTEXT
#define SWRCONTEXT
Definition: FFmpegUtilities.h:148
openshot::VIDEO_STREAM
@ VIDEO_STREAM
A video stream (used to determine which type of stream)
Definition: FFmpegWriter.h:29
path
path
Definition: FFmpegWriter.cpp:1444
openshot::FFmpegWriter::WriteFrame
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: FFmpegWriter.cpp:677
ReaderBase.h
Header file for ReaderBase class.
openshot::FFmpegWriter::Close
void Close()
Close the writer.
Definition: FFmpegWriter.cpp:967
openshot::FFmpegWriter::IsValidCodec
static bool IsValidCodec(std::string codec_name)
Determine if codec name is valid.
Definition: FFmpegWriter.cpp:603
openshot::FFmpegWriter::WriteTrailer
void WriteTrailer()
Write the file trailer (after all frames are written). This is called automatically by the Close() me...
Definition: FFmpegWriter.cpp:754
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
codec
codec
Definition: FFmpegWriter.cpp:1440
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition: ChannelLayouts.h:28
openshot::FFmpegWriter::SetAudioOptions
void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate)
Set audio export options.
Definition: FFmpegWriter.cpp:286
openshot::StreamType
StreamType
This enumeration designates the type of stream when encoding (video or audio)
Definition: FFmpegWriter.h:28
openshot::FFmpegWriter::PrepareStreams
void PrepareStreams()
Prepare & initialize streams and open codecs. This method is called automatically by the Open() metho...
Definition: FFmpegWriter.cpp:615
openshot::FFmpegWriter::RemoveScalers
void RemoveScalers()
Remove & deallocate all software scalers.
Definition: FFmpegWriter.cpp:2277
openshot::FFmpegWriter::WriteHeader
void WriteHeader()
Write the file header (after the options are set). This method is called automatically by the Open() ...
Definition: FFmpegWriter.cpp:632
openshot::WriterBase
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:69