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

Demux and seek.

Demux and seek.

api2-demux-seek <args>
// Seeks in a container and prints the packets around the new position.
//
// Shows: seeking to a timestamp in a chosen stream, the key-frame/any-frame
// distinction and how the first packets after a seek carry the flag.
//
// Usage: api2-demux-seek <input file>
#include <iostream>
#include <set>
#include <map>
#include <memory>
#include <functional>
#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;
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
av::setFFmpegLoggingLevel(AV_LOG_DEBUG);
string uri {argv[1]};
error_code ec;
ictx.openInput(uri);
clog << "Seekable: " << ictx.seekable() << endl;
{
// Seek to the 200 seconds
ictx.seek({200, {1,1}});
auto pkt = ictx.readPacket();
clog << "Seek 0: [" << pkt.streamIndex() << "] pts=" << pkt.pts() << " / " << pkt.pts().seconds() << endl;
}
{
// Seek to the 0 seconds
ictx.seek({0, {1,1}});
auto pkt = ictx.readPacket();
clog << "Seek 0: [" << pkt.streamIndex() << "] pts=" << pkt.pts() << " / " << pkt.pts().seconds() << endl;
}
{
// Seek to the 200 seconds and read to the end
ictx.seek({200, {1,1}});
clog << "Seek 2:\n";
while (true) {
auto pkt = ictx.readPacket(ec);
if (!pkt)
break;
clog << " [" << pkt.streamIndex() << "] pts=" << pkt.pts() << " / " << pkt.pts().seconds() << endl;
}
}
{
// Seek the the begin and read 100 packets
ictx.seek({0, {1,1}});
clog << "Seek 3:\n";
size_t count = 100;
while (count --> 0) {
auto pkt = ictx.readPacket(ec);
if (!pkt)
break;
clog << " [" << pkt.streamIndex() << "] pts=" << pkt.pts() << " / " << pkt.pts().seconds() << endl;
}
}
}
Audio resampling with libswresample.
Library initialization.
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
Container context: the entry point for demuxing, muxing and seeking.
Definition formatcontext.h:153
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.
bool seekable() const noexcept
Whether the container supports seeking.
void seek(const Timestamp &timestamp, OptionalErrorCode ec=throws())
Seek to a timestamp in any stream.
Packet readPacket(OptionalErrorCode ec=throws())
Read the next packet from any stream.
int streamIndex() const
Index of the stream the packet belongs to.
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.
STL namespace.
Compressed packets and their side data.
Video scaling and pixel format conversion with libswscale.