avcpp 3.1.0
Обёртка над FFmpeg, упрощающая его использование в C++ проектах.
Загрузка...
Поиск...
Не найдено
api2-decode-audio.cpp

Декодирование аудио.

Декодирование аудио.

api2-decode-audio <входной файл> [формат]
// Decodes the first audio stream and prints the parameters of every buffer.
//
// Shows: demuxing, audio decoding with the legacy decode() path, sample layout
// accessors.
//
// Usage: api2-decode-audio <input file> [format]
// format optional demuxer name, for example "wav"
#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/videorescaler.h"
#include "avcpp/audioresampler.h"
#include "avcpp/avutils.h"
// API2
#include "avcpp/format.h"
#include "avcpp/formatcontext.h"
#include "avcpp/codec.h"
#include "avcpp/codeccontext.h"
using namespace std;
using namespace av;
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
av::init();
av::setFFmpegLoggingLevel(AV_LOG_DEBUG);
string uri {argv[1]};
ssize_t audioStream = -1;
AudioDecoderContext adec;
Stream ast;
error_code ec;
string format;
if (argc > 2) {
format = argv[2];
}
int count = 0;
{
InputFormat ifmt;
if (!format.empty()) {
ifmt.setFormat(format);
}
FormatContext ictx;
ictx.openInput(uri, ifmt, ec);
if (ec) {
cerr << "Can't open input\n";
return 1;
}
ictx.findStreamInfo();
for (size_t i = 0; i < ictx.streamsCount(); ++i) {
auto st = ictx.stream(i);
if (st.isAudio()) {
audioStream = i;
ast = st;
break;
}
}
cerr << audioStream << endl;
if (ast.isNull()) {
cerr << "Audio stream not found\n";
return 1;
}
if (ast.isValid()) {
adec = AudioDecoderContext(ast);
Codec codec = findDecodingCodec(adec.raw()->codec_id);
adec.setCodec(codec);
adec.setRefCountedFrames(true);
adec.open(Codec(), ec);
if (ec) {
cerr << "Can't open codec\n";
return 1;
}
}
clog << "NO_PTS: " << av::NoPts << endl;
clog << "Duration: " << ictx.duration() << " / " << ictx.duration().seconds()
<< ", Start Time: " << ictx.startTime() << " / " << ictx.startTime().seconds()
<< endl;
// Substract start time from the Packet PTS and DTS values: PTS starts from the zero
ictx.substractStartTime(true);
while (true) {
Packet pkt = ictx.readPacket(ec);
if (ec)
{
clog << "Packet reading error: " << ec << ", " << ec.message() << endl;
break;
}
// EOF
if (!pkt)
{
break;
}
if (pkt.streamIndex() != audioStream) {
continue;
}
clog << "Read packet: " << pkt.pts() << " / " << pkt.pts().seconds() << " / " << pkt.timeBase() << " / st: " << pkt.streamIndex() << endl;
AudioSamples samples = adec.decode(pkt, ec);
if (ec) {
cerr << "Error: " << ec << endl;
return 1;
} else if (!samples) {
//cerr << "Empty frame\n";
//continue;
}
clog << " Samples: " << samples.samplesCount()
<< ", ch: " << samples.channelsCount()
<< ", freq: " << samples.sampleRate()
<< ", name: " << samples.channelsLayoutString()
<< ", ref=" << samples.isReferenced() << ":" << samples.refCount()
<< ", ts: " << samples.pts()
<< ", tm: " << samples.pts().seconds()
<< endl;
//return 0;
count++;
if (count > 100)
break;
}
// NOTE: stream decodec must be closed/destroyed before
//ictx.close();
//vdec.close();
}
}