Loading...
Searching...
No Matches
openshot::FFmpegWriter Class Reference

This class uses the FFmpeg libraries, to write and encode video files and audio files. More...

`#include <FFmpegWriter.h>`

Inheritance diagram for openshot::FFmpegWriter:

## Public Member Functions

void | Close ()
| Close the writer.
| FFmpegWriter (const std::string &path)
| Constructor for FFmpegWriter. Throws an exception on failure to open path.
int | GetCacheSize ()
| Get the cache size (number of frames to queue before writing)
bool | IsOpen ()
| Determine if writer is open or closed.
void | Open ()
| Open writer.
void | OutputStreamInfo ()
| Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
void | PrepareStreams ()
| Prepare & initialize streams and open codecs. This method is called automatically by the Open() method if this method has not yet been called.
void | RemoveScalers ()
| Remove & deallocate all software scalers.
void | ResampleAudio (int sample_rate, int channels)
| Set audio resample options.
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 | SetAudioOptions (std::string codec, int sample_rate, int bit_rate)
| Set audio export options.
void | SetCacheSize (int new_size)
| Set the cache size.
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 PrepareStreams() method, otherwise the streams have not been initialized yet.
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 | SetVideoOptions (std::string codec, int width, int height, openshot::Fraction fps, int bit_rate)
| Set video export options.
void | WriteFrame (openshot::ReaderBase *reader, int64_t start, int64_t length)
| Write a block of frames from a reader.
void | WriteFrame (std::shared_ptr< openshot::Frame > frame)
| Add a frame to the stack waiting to be encoded.
void | WriteHeader ()
| Write the file header (after the options are set). This method is called automatically by the Open() method if this method has not yet been called.
void | WriteTrailer ()
| Write the file trailer (after all frames are written). This is called automatically by the Close() method if this method has not yet been called.
Public Member Functions inherited from openshot::WriterBase
void | CopyReaderInfo (openshot::ReaderBase *reader)
| This method copy's the info struct of a reader, and sets the writer with the same info.
void | DisplayInfo (std::ostream *out=&std::cout)
| Display file information in the standard output stream (stdout)
std::string | Json () const
| Generate JSON string of this object.
Json::Value | JsonValue () const
| Generate Json::Value for this object.
void | SetJson (const std::string value)
| Load JSON string into this object.
void | SetJsonValue (const Json::Value root)
| Load Json::Value into this object.
| WriterBase ()
| Constructor for WriterBase class, many things are initialized here.
virtual | ~WriterBase ()=default

## Static Public Member Functions

static bool | IsValidCodec (std::string codec_name)
| Determine if codec name is valid.

## Additional Inherited Members

Public Attributes inherited from openshot::WriterBase
WriterInfo | info
| Information about the current media file.

## Detailed Description

This class uses the FFmpeg libraries, to write and encode video files and audio files.

All FFmpeg options can be set using the SetOption() method, and any Reader may be used to generate openshot::Frame objects needed for writing. Be sure to use valid bit rates, frame rates, and sample rates (each format / codec has a limited # of valid options).

SIMPLE EXAMPLE
// Create a reader for a video
openshot::FFmpegReader r("MyAwesomeVideo.webm");
r.Open(); // Open the target reader
// Create a writer (which will create a WebM video)
openshot::FFmpegWriter w("/home/jonathan/NewVideo.webm");
// Set options
// Sample Rate: 44100, Channels: 2, Bitrate: 128000
w.SetAudioOptions(true, "libvorbis", 44100, 2, openshot::ChannelLayout::LAYOUT_STEREO, 128000);
// FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000);
// Open the writer
w.Open();
// Write all frames from the reader
w.WriteFrame(&r, 1, r.info.video_length);
// Close the reader & writer
w.Close();
r.Close();
openshot::FFmpegReader
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition FFmpegReader.h:101
openshot::FFmpegWriter
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition FFmpegWriter.h:116
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::LAYOUT_STEREO
@ LAYOUT_STEREO
Definition ChannelLayouts.h:31

Here is a more advanced example, which sets some additional (and optional) encoding options.

ADVANCED WRITER EXAMPLE
// Create a reader for a video
openshot::FFmpegReader r("MyAwesomeVideo.webm");
r.Open(); // Open the reader
// Create a writer (which will create a WebM video)
openshot::FFmpegWriter w("/home/jonathan/NewVideo.webm");
// Set options
// Sample Rate: 44100, Channels: 2, Bitrate: 128000
w.SetAudioOptions(true, "libvorbis", 44100, 2, openshot::ChannelLayout::LAYOUT_STEREO, 128000);
// FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000);
// Prepare Streams (Optional method that must be called before any SetOption calls)
w.PrepareStreams();
// Set some specific encoding options (Optional methods)
w.SetOption(VIDEO_STREAM, "qmin", "2" );
w.SetOption(VIDEO_STREAM, "qmax", "30" );
w.SetOption(VIDEO_STREAM, "crf", "10" );
w.SetOption(VIDEO_STREAM, "rc_min_rate", "2000000" );
w.SetOption(VIDEO_STREAM, "rc_max_rate", "4000000" );
w.SetOption(VIDEO_STREAM, "max_b_frames", "10" );
// Write the header of the video file
w.WriteHeader();
// Open the writer
w.Open();
// Write all frames from the reader
w.WriteFrame(&r, 1, r.info.video_length);
// Write the trailer of the video file
w.WriteTrailer();
// Close the reader & writer
w.Close();
r.Close();
openshot::VIDEO_STREAM
@ VIDEO_STREAM
A video stream (used to determine which type of stream)
Definition FFmpegWriter.h:29

Definition at line 116 of file FFmpegWriter.h.

## Constructor & Destructor Documentation

## FFmpegWriter()

FFmpegWriter::FFmpegWriter | ( | const std::string & | path | ) |

Constructor for FFmpegWriter. Throws an exception on failure to open path.

Parameters

  path | The file path of the video file you want to open and read

Definition at line 75 of file FFmpegWriter.cpp.

## Member Function Documentation

## Close()

void FFmpegWriter::Close | ( |  | ) |

Close the writer.

Definition at line 1038 of file FFmpegWriter.cpp.

Referenced by openshot::ChunkWriter::Close(), and openshot::ChunkWriter::WriteFrame().

## GetCacheSize()

int openshot::FFmpegWriter::GetCacheSize | ( |  | ) |

inline

Get the cache size (number of frames to queue before writing)

Definition at line 228 of file FFmpegWriter.h.

## IsOpen()

bool openshot::FFmpegWriter::IsOpen | ( |  | ) |

inline virtual

Determine if writer is open or closed.

Implements openshot::WriterBase.

Definition at line 231 of file FFmpegWriter.h.

## IsValidCodec()

bool FFmpegWriter::IsValidCodec | ( | std::string | codec_name | ) |

static

Determine if codec name is valid.

Definition at line 603 of file FFmpegWriter.cpp.

## Open()

void FFmpegWriter::Open | ( |  | ) |

virtual

Open writer.

Implements openshot::WriterBase.

Definition at line 95 of file FFmpegWriter.cpp.

## OutputStreamInfo()

void FFmpegWriter::OutputStreamInfo | ( |  | ) |

Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)

Definition at line 2313 of file FFmpegWriter.cpp.

## PrepareStreams()

void FFmpegWriter::PrepareStreams | ( |  | ) |

Prepare & initialize streams and open codecs. This method is called automatically by the Open() method if this method has not yet been called.

Definition at line 615 of file FFmpegWriter.cpp.

Referenced by Open(), and openshot::ChunkWriter::WriteFrame().

## RemoveScalers()

void FFmpegWriter::RemoveScalers | ( |  | ) |

Remove & deallocate all software scalers.

Definition at line 2352 of file FFmpegWriter.cpp.

Referenced by Close().

## ResampleAudio()

void FFmpegWriter::ResampleAudio | ( | int | sample_rate,
|  | int | channels
| ) |  |

Set audio resample options.

Parameters

  sample_rate | The number of samples per second of the audio
  channels | The number of audio channels

Definition at line 2346 of file FFmpegWriter.cpp.

## SetAudioOptions() [1/2]

void FFmpegWriter::SetAudioOptions | ( | bool | has_audio,
|  | std::string | codec,
|  | int | sample_rate,
|  | int | channels,
|  | openshot::ChannelLayout | channel_layout,
|  | int | bit_rate
| ) |  |

Set audio export options.

Parameters

  has_audio | Does this file need an audio stream?
  codec | The codec used to encode the audio for this file
  sample_rate | The number of audio samples needed in this file
  channels | The number of audio channels needed in this file
  channel_layout | The 'layout' of audio channels (i.e. mono, stereo, surround, etc...)
  bit_rate | The audio bit rate used during encoding

Note
  This is an overloaded function.

Definition at line 286 of file FFmpegWriter.cpp.

Referenced by SetAudioOptions(), and openshot::ChunkWriter::WriteFrame().

## SetAudioOptions() [2/2]

void FFmpegWriter::SetAudioOptions | ( | std::string | codec,
|  | int | sample_rate,
|  | int | bit_rate
| ) |  |

Set audio export options.

Enables the stream and configures a default 2-channel stereo layout.

Parameters

  codec | The codec used to encode the audio for this file
  sample_rate | The number of audio samples needed in this file
  bit_rate | The audio bit rate used during encoding

Note
  This is an overloaded function.

Definition at line 323 of file FFmpegWriter.cpp.

## SetCacheSize()

void openshot::FFmpegWriter::SetCacheSize | ( | int | new_size | ) |

inline

Set the cache size.

Parameters

  new_size | The number of frames to queue before writing to the file

Definition at line 278 of file FFmpegWriter.h.

## SetOption()

void FFmpegWriter::SetOption | ( | openshot::StreamType | stream,
|  | std::string | name,
|  | std::string | value
| ) |  |

Set custom options (some codecs accept additional params). This must be called after the PrepareStreams() method, otherwise the streams have not been initialized yet.

Parameters

  stream | The stream (openshot::StreamType) this option should apply to
  name | The name of the option you want to set (i.e. qmin, qmax, etc...)
  value | The new value of this option

Definition at line 333 of file FFmpegWriter.cpp.

## SetVideoOptions() [1/2]

void FFmpegWriter::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.

Parameters

  has_video | Does this file need a video stream
  codec | The codec used to encode the images in this video
  fps | The number of frames per second
  width | The width in pixels of this video
  height | The height in pixels of this video
  pixel_ratio | The shape of the pixels represented as a openshot::Fraction (1x1 is most common / square pixels)
  interlaced | Does this video need to be interlaced?
  top_field_first | Which frame should be used as the top field?
  bit_rate | The video bit rate used during encoding

Note
  This is an overloaded function.

Definition at line 163 of file FFmpegWriter.cpp.

Referenced by SetVideoOptions(), and openshot::ChunkWriter::WriteFrame().

## SetVideoOptions() [2/2]

void FFmpegWriter::SetVideoOptions | ( | std::string | codec,
|  | int | width,
|  | int | height,
|  | openshot::Fraction | fps,
|  | int | bit_rate
| ) |  |

Set video export options.

Enables the stream and configures non-interlaced video with a 1:1 pixel aspect ratio.

Parameters

  codec | The codec used to encode the images in this video
  width | The width in pixels of this video
  height | The height in pixels of this video
  fps | The number of frames per second
  bit_rate | The video bit rate used during encoding

Note
  This is an overloaded function.

Warning
  Observe the argument order, which is consistent with the openshot::Timeline constructor, but differs from the other signature.

Definition at line 276 of file FFmpegWriter.cpp.

## WriteFrame() [1/2]

void FFmpegWriter::WriteFrame | ( | openshot::ReaderBase * | reader,
|  | int64_t | start,
|  | int64_t | length
| ) |  |

virtual

Write a block of frames from a reader.

Parameters

  reader | A openshot::ReaderBase object which will provide frames to be written
  start | The starting frame number of the reader
  length | The number of frames to write

Note
  This is an overloaded function.

Implements openshot::WriterBase.

Definition at line 805 of file FFmpegWriter.cpp.

## WriteFrame() [2/2]

void FFmpegWriter::WriteFrame | ( | std::shared_ptr< openshot::Frame > | frame | ) |

virtual

Add a frame to the stack waiting to be encoded.

Parameters

  frame | The openshot::Frame object to write to this image

Note
  This is an overloaded function.

Implements openshot::WriterBase.

Definition at line 677 of file FFmpegWriter.cpp.

Referenced by openshot::ChunkWriter::Close(), WriteFrame(), and openshot::ChunkWriter::WriteFrame().

## WriteHeader()

void FFmpegWriter::WriteHeader | ( |  | ) |

Write the file header (after the options are set). This method is called automatically by the Open() method if this method has not yet been called.

Definition at line 632 of file FFmpegWriter.cpp.

Referenced by Open(), and openshot::ChunkWriter::WriteFrame().

## WriteTrailer()

void FFmpegWriter::WriteTrailer | ( |  | ) |

Write the file trailer (after all frames are written). This is called automatically by the Close() method if this method has not yet been called.

Definition at line 822 of file FFmpegWriter.cpp.

Referenced by openshot::ChunkWriter::Close(), Close(), and openshot::ChunkWriter::WriteFrame().

The documentation for this class was generated from the following files:

- FFmpegWriter.h
- FFmpegWriter.cpp