Decode subtitles.
Decode subtitles.
#include <cstddef>
#include <iostream>
namespace {
{
std::cout << " rect " << index << ": ";
case SUBTITLE_TEXT:
std::cout <<
"text: " << rect.
text();
break;
case SUBTITLE_ASS:
std::cout <<
"ass: " << rect.
ass();
break;
case SUBTITLE_BITMAP:
std::cout <<
"bitmap: " << rect.
width() <<
'x' << rect.
height()
<<
", x: " << rect.
x() <<
", y: " << rect.
y();
break;
default:
std::cout <<
"type: " <<
static_cast<int>(rect.
type());
break;
}
std::cout << " (forced)";
std::cout << '\n';
}
}
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_SUBTITLE) {
sst = st;
break;
}
}
std::cerr << "Subtitle stream not found\n";
return 1;
}
if (ec) {
std::cerr << "Can't open codec: " << ec.message() << '\n';
return 1;
}
std::size_t packets = 0;
std::size_t frames = 0;
continue;
++packets;
std::size_t decodedBytes = 0;
auto frame = dec.
decode(pkt, 0, decodedBytes, ec);
if (ec) {
std::cerr << "Can't decode a subtitle: " << ec.message() << '\n';
return 1;
}
if (!frame)
continue;
++frames;
std::cout << "subtitle " << frames
<<
": pts " << frame.
pts()
<<
" (" << frame.pts().
seconds() <<
" s)"
<< ", display " << frame.startDisplayTime()
<< ".." << frame.endDisplayTime() << " ms"
<< ", rects: " << frame.numRects() << '\n';
for (std::size_t i = 0; i < frame.numRects(); ++i)
printRect(frame.rect(i), i);
}
if (ec) {
std::cerr << "Read error: " << ec.message() << '\n';
return 1;
}
std::cout << "packets: " << packets << ", subtitles: " << frames << '\n';
return 0;
}
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.
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.
Decodes subtitle packets into subtitle frames.
Definition codeccontext.h:1997
SubtitleFrame decode(const Packet &packet, OptionalErrorCode ec=throws())
Decodes one subtitle packet.
Timestamp pts() const noexcept
Presentation timestamp of the subtitle.
Non-owning view over AVSubtitleRect.
Definition subtitle.h:51
int y() const noexcept
Vertical position of the rectangle.
int width() const noexcept
Width of the bitmap.
int nbColors() const noexcept
Number of palette entries of a bitmap rectangle.
std::string_view ass() const noexcept
ASS/SSA event line.
int x() const noexcept
Horizontal position of the rectangle.
std::string_view text() const noexcept
Plain UTF-8 text.
AVSubtitleType type() const noexcept
Kind of payload the rectangle carries.
int height() const noexcept
Height of the bitmap.
bool isForced() const noexcept
Whether the rectangle is flagged as forced.
double seconds() const noexcept
The timestamp in seconds.
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.
Subtitle frames: text and ASS payloads, bitmap rectangles.