avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
api2-remux.cpp

Remux.

Remux.

api2-remux <args>
// Copies streams from one container to another without re-encoding.
//
// Shows: copying codec parameters, mapping input streams to output ones,
// rewriting packet stream indices and flushing the muxer before the trailer.
//
// Usage: api2-remux [options] <input> <output>
// -f format force the input format
// -o format force the output format
// -d seconds stop after the given duration
#include <signal.h>
#include <iostream>
#include <set>
#include <map>
#include <memory>
#include <functional>
#include <map>
// getopt
#include <unistd.h>
#include "avcpp/av.h"
#include "avcpp/ffmpeg.h"
#include "avcpp/codec.h"
#include "avcpp/packet.h"
#include "avcpp/avutils.h"
// API2
#include "avcpp/format.h"
#include "avcpp/codec.h"
using namespace std;
using namespace av;
void usage(int /*argc*/, char **argv, ostream &ost)
{
ost << "Usage: " << argv[0] << " [arguments] input_stream output_stream\n"
" -f format force input format\n"
" -o format force output format\n"
" -d duration duration for transmuxing (double, seconds)";
}
const static char opts[] = "f:o:d:h";
int main(int argc, char **argv)
{
string uri;
string out;
string iformatName;
string oformatName;
Timestamp duration;
while (true) {
int opt = getopt(argc, argv, opts);
if (opt < 0)
break;
switch (opt) {
case 'f':
iformatName = optarg;
break;
case 'o':
oformatName = optarg;
break;
case 'd':
{
double val = stod(optarg);
duration = Timestamp(val * AV_TIME_BASE, AV_TIME_BASE_Q);
break;
}
case 'h':
usage(argc, argv, cout);
return 0;
default:
usage(argc, argv, cerr);
return 1;
}
}
if (optind > argc-2) {
usage(argc, argv, cerr);
return 1;
}
uri = argv[optind + 0];
out = argv[optind + 1];
av::set_logging_level(AV_LOG_DEBUG);
error_code ec;
//
// INPUT
//
InputFormat iformat(iformatName);
ictx.openInput(uri, iformat);
//
// OUTPUT
//
OutputFormat oformat(oformatName, out);
map<size_t, size_t> streamMapping;
octx.setFormat(oformat);
// 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++;
}
octx.openOutput(out);
octx.writeHeader();
octx.dump();
//
// PROCESS
//
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;
}
}
// Flush output context
octx.writePacket();
//octx.flush();
octx.writeTrailer();
}
Audio resampling with libswresample.
Library initialization.
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
Codec decodingCodec() const
Decoder for this codec identifier.
Container context: the entry point for demuxing, muxing and seeking.
Definition formatcontext.h:153
OutputFormat outputFormat() const
The muxer in use.
void openInput(const std::string &uri, OptionalErrorCode ec=throws())
Open an input and probe its container.
Stream addStream(const Codec &codec, OptionalErrorCode ec=throws())
Add a stream for the given codec.
void writeTrailer(OptionalErrorCode ec=throws())
Write the container trailer and close the output.
void writeHeader(OptionalErrorCode ec=throws())
Write the container header.
void openOutput(const std::string &uri, OptionalErrorCode ec=throws())
Open an output.
void writePacket(OptionalErrorCode ec=throws())
Flush the muxer queue.
void dump() const
Print the container layout through the FFmpeg log system.
void setFormat(const InputFormat &format)
Force the demuxer used by openInput().
void findStreamInfo(OptionalErrorCode ec=throws())
Read the stream parameters of an opened input.
size_t streamsCount() const
Number of streams in the container.
Stream stream(size_t idx)
Stream by index.
Packet readPacket(OptionalErrorCode ec=throws())
Read the next packet from any stream.
Demuxer descriptor.
Definition format.h:168
Muxer descriptor.
Definition format.h:202
CodecParametersView codecParameters() const
Codec parameters of the stream.
void setCodecParameters(CodecParametersView codecpar, OptionalErrorCode ec=throws())
Copy codec parameters into the stream.
The Timestamp class represents timestamp value and it timebase.
Definition timestamp.h:25
Codec descriptors and lookup by name or id.
Codec contexts: opening, configuring, encoding and decoding.
FFmpeg types re-exported for the public API and small value wrappers.
Input and output format descriptors.
Container format contexts: opening, demuxing, muxing and seeking.
Root namespace of the library.
Definition audioresampler.h:19
void init()
Initialize all subsystems the library needs.
void set_logging_level(int32_t level)
This method can be used to turn up or down FFmpeg's logging level.
STL namespace.
Compressed packets and their side data.
bool codecSupported(const class Codec &codec) const noexcept
Whether the format can carry a codec.
Definition format.h:135
Video scaling and pixel format conversion with libswscale.