avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
Remuxing

Remuxing moves compressed packets from one container to another without decoding them. It is the cheapest way to change a container, drop a stream, or cut at keyframes, and it is the best first test of a pipeline: if remuxing produces a playable file, the container side is correct.

Copy the stream descriptions

// Copy streams
for (size_t i = 0, ostIdx = 0; i < ictx.streamsCount(); ++i) {
auto ist = ictx.stream(i);
auto icodecpar = ist.codecParameters();
auto ocodec = icodecpar.decodingCodec();
// Source codec can be unsupprted by the target format. Transcoding required or simple skip.
if (!octx.outputFormat().codecSupported(ocodec)) {
clog << "Input stream " << i << " codec '" << ocodec.name() <<
"' does not supported by the output format '" << oformat.name() << "'\n";
continue;
}
auto ost = octx.addStream(ec);
ost.setCodecParameters(icodecpar);
ost.codecParameters().codecTag(0);
// We can omit codec checking above and got error FormatCodecUnsupported (error code or exception)
if (ec) {
if (ec == Errors::FormatCodecUnsupported) {
continue;
} else {
cerr << ec.category().name() << ": " << ec.message() << endl;
return 1;
}
}
ost.setTimeBase(ist.timeBase());
// setup mapping
streamMapping[i] = ostIdx++;
}

The important call is stream.codecParameters(): it returns the portable description of the codec inside the stream. Copying it to the output stream tells the muxer what the packets will contain, without opening any codec.

codecParameters().decodingCodec() looks up the codec by identifier so the program can check whether the target container supports it. A muxer happily accepts a stream it cannot store, and the failure then appears at write time instead of setup time.

codecParameters().codecTag(0) clears the codec tag. Tags are container-specific four-character codes; leaving the input tag would make the output stream claim a tag that belongs to a different container.

Move the packets

while (true) {
auto pkt = ictx.readPacket();
if (!pkt)
break;
auto it = streamMapping.find(pkt.streamIndex());
string skip;
if (it == streamMapping.end()) {
skip = " [skip]";
}
clog << "pkt[" << pkt.streamIndex() << "]: tm=" << pkt.pts() << " / " << pkt.pts().seconds() << skip << '\n';
if (it == streamMapping.end())
continue;
pkt.setStreamIndex(it->second);
octx.writePacket(pkt);
if (duration) {
if (pkt.pts() > duration)
break;
}
}

Packets are rewritten, not re-timed: the loop only maps the input stream index to the output one. Timestamps stay in the input stream time base, which is why the example copies setTimeBase() from the input stream during setup.

writePacket() without arguments flushes the muxer's internal queue before writeTrailer(). Some muxers hold the last few packets until the trailer; without the flush they can be reordered or lost.

What cannot be remuxed

  • A codec the target container does not support. The example skips such streams, which is a legitimate policy: Errors::FormatCodecUnsupported is reported instead of aborting.
  • A change of resolution or codec parameters in the middle of a stream. Packets after the change belong to a different configuration and need a new stream.
  • Anything that requires touching the payload: filtering, scaling, re-timing.

For those, decode and encode instead: Decoding and Encoding.

Next

  • Encoding — when the codec has to change.
  • Filters — when the frames have to change.