This page builds the smallest useful program: open a file, find the video stream, decode it, and print one line per frame. It is the base every other guide builds on.
Prerequisites: FFmpeg 4.0 or newer and a C++17 compiler. If the library is not installed yet, see Building and installing.
The program
#include <iostream>
int main(int argc, char **argv)
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <input file>\n";
return 1;
}
std::error_code ec;
if (ec) {
std::cerr << "Can't open input: " << ec.message() << '\n';
return 1;
}
if (ec) {
std::cerr << "Can't find streams: " << ec.message() << '\n';
return 1;
}
if (st.mediaType() == AVMEDIA_TYPE_VIDEO) {
vst = st;
break;
}
}
std::cerr << "Video stream not found\n";
return 1;
}
if (ec) {
std::cerr << "Can't open codec: " << ec.message() << '\n';
return 1;
}
for (;;) {
if (ec) {
std::cerr << "Read error: " << ec.message() << '\n';
return 1;
}
if (!pkt)
break;
continue;
if (!dec.
send(pkt, ec)) {
std::cerr << "Can't send a packet: " << ec.message() << '\n';
return 1;
}
for (;;) {
if (ec) {
std::cerr << "Can't receive a frame: " << ec.message() << '\n';
return 1;
}
if (!frame)
break;
}
}
}
void open(OptionalErrorCode ec=throws())
Open the codec with the parameters set so far.
Container context: the entry point for demuxing, muxing and seeking.
Definition formatcontext.h:153
StreamsView streams(OptionalErrorCode ec=throws()) const
All streams as a range, for range-based for and std algorithms.
void openInput(const std::string &uri, OptionalErrorCode ec=throws())
Open an input and probe its container.
void findStreamInfo(OptionalErrorCode ec=throws())
Read the stream parameters of an opened input.
Packet readPacket(OptionalErrorCode ec=throws())
Read the next packet from any stream.
Timestamp pts() const
Presentation timestamp.
Compressed data and its metadata.
Definition packet.h:110
int streamIndex() const
Index of the stream the packet belongs to.
One stream inside a container.
Definition stream.h:34
int index() const
Position of the stream in its container.
bool isValid() const
Whether the view refers to a stream.
double seconds() const noexcept
The timestamp in seconds.
Decodes video packets into frames.
Definition codeccontext.h:1161
bool send(const Packet &packet, OptionalErrorCode ec=throws())
Sends a packet to the decoder.
VideoFrame receive(OptionalErrorCode ec=throws(), bool autoAllocateFrame=true)
Receives one decoded frame.
One video frame.
Definition frame.h:578
int height() const
Frame height.
int width() const
Frame width.
PixelFormat pixelFormat() const
Pixel format of the frame.
Codec contexts: opening, configuring, encoding and decoding.
Container format contexts: opening, demuxing, muxing and seeking.
void init()
Initialize all subsystems the library needs.
Compressed packets and their side data.
Streams inside a container: parameters, time base and disposition.
What each step does
av::init() registers the FFmpeg codecs and formats. Call it once, before any other avcpp or FFmpeg call. Everything else is unsafe before it.
av::FormatContext owns the input. openInput() probes the container, findStreamInfo() reads the stream parameters. Do not skip the second call: stream codec parameters are frequently unknown until the file has been read.
av::Stream is a handle to one stream inside the context. It is copyable and cheap; destroying it does not destroy the stream. mediaType(), index() and timeBase() are the accessors you will use most.
av::VideoDecoderContext is constructed from the stream, so it picks up the codec from the stream parameters. open() with no arguments is the common case; the extra arguments exist for explicit codec selection and options.
send() / receive() are the modern FFmpeg codec API and the only one that is correct for asynchronous decoders. A packet usually produces zero, one, or several frames; a frame can arrive many packets later, and a rejected send() means "drain the decoder and retry". The Decoding page explains the contract in detail.
Errors
Every call that can fail takes an OptionalErrorCode. The default is throws(), which throws av::Exception (a std::system_error). Passing a std::error_code& switches that call to the non-throwing form and stores the error instead. This is why the program above declares one std::error_code ec and reuses it.
The two styles do not mix inside a single call: either you get the code back, or you get an exception. Error handling covers the categories and the complete list of codes.
Link it
add_executable(probe main.cpp)
target_link_libraries(probe PRIVATE avcpp::avcpp)
Next