From 96f6e59db1abed18b9fbf64b88c0cf37aafe5e2e Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 22 May 2018 23:36:31 +0200 Subject: [PATCH] Fix non-transcoded audio in Kaeru in streams where the audio does not have index 1. --- kaeru.cpp | 2 +- mux.cpp | 9 ++++++--- mux.h | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/kaeru.cpp b/kaeru.cpp index 011818d..11fea00 100644 --- a/kaeru.cpp +++ b/kaeru.cpp @@ -129,7 +129,7 @@ void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, Audio void audio_frame_callback(Mux *mux, const AVPacket *pkt, AVRational timebase) { - mux->add_packet(*pkt, pkt->pts, pkt->dts == AV_NOPTS_VALUE ? pkt->pts : pkt->dts, timebase); + mux->add_packet(*pkt, pkt->pts, pkt->dts == AV_NOPTS_VALUE ? pkt->pts : pkt->dts, timebase, /*stream_index=*/1); } void adjust_bitrate(int signal) diff --git a/mux.cpp b/mux.cpp index 5e9043e..b1b9db6 100644 --- a/mux.cpp +++ b/mux.cpp @@ -145,7 +145,7 @@ Mux::~Mux() avformat_free_context(avctx); } -void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational timebase) +void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational timebase, int stream_index_override) { AVPacket pkt_copy; av_init_packet(&pkt_copy); @@ -153,11 +153,14 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational t fprintf(stderr, "av_copy_packet() failed\n"); exit(1); } - if (pkt.stream_index == 0) { + if (stream_index_override != -1) { + pkt_copy.stream_index = stream_index_override; + } + if (pkt_copy.stream_index == 0) { pkt_copy.pts = av_rescale_q(pts, timebase, avstream_video->time_base); pkt_copy.dts = av_rescale_q(dts, timebase, avstream_video->time_base); pkt_copy.duration = av_rescale_q(pkt.duration, timebase, avstream_video->time_base); - } else if (pkt.stream_index == 1) { + } else if (pkt_copy.stream_index == 1) { pkt_copy.pts = av_rescale_q(pts, timebase, avstream_audio->time_base); pkt_copy.dts = av_rescale_q(dts, timebase, avstream_audio->time_base); pkt_copy.duration = av_rescale_q(pkt.duration, timebase, avstream_audio->time_base); diff --git a/mux.h b/mux.h index e6193e0..9614bff 100644 --- a/mux.h +++ b/mux.h @@ -62,7 +62,7 @@ public: // will be added to. Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const std::string &video_extradata, const AVCodecParameters *audio_codecpar, int time_base, std::function write_callback, WriteStrategy write_strategy, const std::vector &metrics); ~Mux(); - void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational timebase = { 1, TIMEBASE }); + void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational timebase = { 1, TIMEBASE }, int stream_index_override = -1); // As long as the mux is plugged, it will not actually write anything to disk, // just queue the packets. Once it is unplugged, the packets are reordered by pts -- 2.39.2