Loading...
Searching...
No Matches
FFmpegWriter.h
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef OPENSHOT_FFMPEG_WRITER_H
#define OPENSHOT_FFMPEG_WRITER_H
#include "ReaderBase.h"
#include "WriterBase.h"
// Include FFmpeg headers and macros
#include "FFmpegUtilities.h"
namespace openshot {
enum StreamType {
VIDEO_STREAM,
AUDIO_STREAM
};
class FFmpegWriter : public WriterBase {
private:
std::string path;
int cache_size;
bool is_writing;
bool is_open;
int64_t video_timestamp;
int64_t audio_timestamp;
bool prepare_streams;
bool write_header;
bool write_trailer;
AVFormatContext* oc;
AVStream *audio_st, *video_st;
AVCodecContext *video_codec_ctx;
AVCodecContext *audio_codec_ctx;
SwsContext *img_convert_ctx;
int16_t *samples;
uint8_t *audio_outbuf;
uint8_t *audio_encoder_buffer;
int num_of_rescalers;
int rescaler_position;
std::vector<SwsContext *> image_rescalers;
int audio_outbuf_size;
int audio_input_frame_size;
int initial_audio_input_frame_size;
int audio_input_position;
int audio_encoder_buffer_size;
SWRCONTEXT *avr;
SWRCONTEXT *avr_planar;
/* Resample options */
int original_sample_rate;
int original_channels;
std::shared_ptr<openshot::Frame> last_frame;
std::deque<std::shared_ptr<openshot::Frame> > spooled_audio_frames;
std::deque<std::shared_ptr<openshot::Frame> > spooled_video_frames;
std::deque<std::shared_ptr<openshot::Frame> > queued_audio_frames;
std::deque<std::shared_ptr<openshot::Frame> > queued_video_frames;
std::deque<std::shared_ptr<openshot::Frame> > processed_frames;
std::deque<std::shared_ptr<openshot::Frame> > deallocate_frames;
std::map<std::shared_ptr<openshot::Frame>, AVFrame *> av_frames;
void add_avframe(std::shared_ptr<openshot::Frame> frame, AVFrame *av_frame);
AVStream *add_audio_stream();
AVStream *add_video_stream();
AVFrame *allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
void auto_detect_format();
void close_audio(AVFormatContext *oc, AVStream *st);
void close_video(AVFormatContext *oc, AVStream *st);
void flush_encoders();
void initialize_streams();
void InitScalers(int source_width, int source_height);
void open_audio(AVFormatContext *oc, AVStream *st);
void open_video(AVFormatContext *oc, AVStream *st);
void process_video_packet(std::shared_ptr<openshot::Frame> frame);
void write_audio_packets(bool is_final);
bool write_video_packet(std::shared_ptr<openshot::Frame> frame, AVFrame *frame_final);
void write_queued_frames();
public:
FFmpegWriter(const std::string& path);
void Close();
int GetCacheSize() { return cache_size; };
bool IsOpen() { return is_open; };
static bool IsValidCodec(std::string codec_name);
void Open();
void OutputStreamInfo();
void PrepareStreams();
void RemoveScalers();
void ResampleAudio(int sample_rate, int channels);
void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
void SetAudioOptions(std::string codec, int sample_rate, int bit_rate);
void SetCacheSize(int new_size) { cache_size = new_size; };
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);
void SetVideoOptions(std::string codec, int width, int height, openshot::Fraction fps, int bit_rate);
void SetOption(openshot::StreamType stream, std::string name, std::string value);
void WriteHeader();
void WriteFrame(std::shared_ptr<openshot::Frame> frame);
void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
void WriteTrailer();
};
}
#endif
FFmpegUtilities.h
Header file for FFmpegUtilities.
PixelFormat
#define PixelFormat
Definition FFmpegUtilities.h:100
SWRCONTEXT
#define SWRCONTEXT
Definition FFmpegUtilities.h:148
codec
codec
Definition FFmpegWriter.cpp:1511
path
path
Definition FFmpegWriter.cpp:1515
ReaderBase.h
Header file for ReaderBase class.
WriterBase.h
Header file for WriterBase class.
openshot::FFmpegWriter
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition FFmpegWriter.h:116
openshot::FFmpegWriter::Close
void Close()
Close the writer.
Definition FFmpegWriter.cpp:1038
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::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
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::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::IsOpen
bool IsOpen()
Determine if writer is open or closed.
Definition FFmpegWriter.h:231
openshot::FFmpegWriter::SetCacheSize
void SetCacheSize(int new_size)
Set the cache size.
Definition FFmpegWriter.h:278
openshot::FFmpegWriter::ResampleAudio
void ResampleAudio(int sample_rate, int channels)
Set audio resample options.
Definition FFmpegWriter.cpp:2346
openshot::FFmpegWriter::GetCacheSize
int GetCacheSize()
Get the cache size (number of frames to queue before writing)
Definition FFmpegWriter.h:228
openshot::FFmpegWriter::Open
void Open()
Open writer.
Definition FFmpegWriter.cpp:95
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::FFmpegWriter::IsValidCodec
static bool IsValidCodec(std::string codec_name)
Determine if codec name is valid.
Definition FFmpegWriter.cpp:603
openshot::FFmpegWriter::OutputStreamInfo
void OutputStreamInfo()
Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
Definition FFmpegWriter.cpp:2313
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
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:822
openshot::FFmpegWriter::RemoveScalers
void RemoveScalers()
Remove & deallocate all software scalers.
Definition FFmpegWriter.cpp:2352
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot::WriterBase
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition WriterBase.h:70
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition ChannelLayouts.h:29
openshot::StreamType
StreamType
This enumeration designates the type of stream when encoding (video or audio)
Definition FFmpegWriter.h:28
openshot::AUDIO_STREAM
@ AUDIO_STREAM
An audio stream (used to determine which type of stream)
Definition FFmpegWriter.h:30
openshot::VIDEO_STREAM
@ VIDEO_STREAM
A video stream (used to determine which type of stream)
Definition FFmpegWriter.h:29