Commit 26509173 authored by NzSN's avatar NzSN

udpate

parent cd3b010e
Pipeline #15852 failed with stages
This diff is collapsed.
This diff is collapsed.
File added
This diff is collapsed.
This diff is collapsed.
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
int main(int argc, char *argv[]) {
int ret = 0;
AVFormatContext *fmt_ctx = NULL;
AVCodec *dec = NULL;
AVCodecContext *dec_ctx = NULL;
AVPacket *packet = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
/* select the audio stream */
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find an audio stream in the input file\n");
return ret;
}
int audio_stream_index = ret;
/* create decoding context */
dec_ctx = avcodec_alloc_context3(dec);
if (!dec_ctx)
return AVERROR(ENOMEM);
avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[audio_stream_index]->codecpar);
/* init the audio decoder */
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open audio decoder\n");
return ret;
}
/* read all packets */
while (1) {
ret = av_read_frame(fmt_ctx, packet);
if (ret < 0)
break;
printf("PTS: %u, DTS: %u, DUR: %d, TB: %d/%d, sample_fmt: %d, sample_rate: %d, Channel_layout: %d\n",
packet->pts, packet->dts, packet->duration,
fmt_ctx->streams[0]->time_base.num,
fmt_ctx->streams[0]->time_base.den,
frame->format,
frame->sample_rate,
frame->channel_layout);
if (packet->stream_index == audio_stream_index) {
ret = avcodec_send_packet(dec_ctx, packet);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder\n");
return 0;
}
}
}
av_packet_unref(packet);
}
#if 0
while (1) {
ret = av_read_frame(fmt_ctx, packet);
if (ret < 0)
break;
if (packet->stream_index == audio_stream_index) {
ret = avcodec_send_packet(dec_ctx, packet);
if (ret < 0) {
printf("Error while sendign a packet to the decoder\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
printf("Error\n");
return 1;
}
}
printf("PTS: %d, DTS: %d, DUR: %d, TB: %d/%d, sample_fmt: %d, sample_rate: %d, Channel_layout: %d\n",
frame->pts, frame->pkt_dts, frame->pkt_duration,
fmt_ctx->streams[0]->time_base.num,
fmt_ctx->streams[0]->time_base.den,
frame->format,
frame->sample_rate,
frame->channel_layout);
}
av_packet_unref(packet);
}
#endif
}
......@@ -5,6 +5,7 @@ typedef enum {
BEGIN_STREAM = 0,
V_STREAM = 0,
A_STREAM,
A_STREAM_1,
MAX_STREAM
} STREAM_IDX;
......@@ -20,6 +21,7 @@ int main(int argc, char *argv[]) {
char *fileName = "./demo2.mp4";
// This is a packing file
char *audioFile = "./audio.mp3";
char *audioFile_1 = "./audio_1.mp3";
char *outFileName = "./demo3.mp4";
// Number of seconds to shift audio away
......@@ -33,7 +35,8 @@ int main(int argc, char *argv[]) {
AVFormatContext *inputFormatContext = NULL,
*outputFormatContext = NULL,
*audioFormatContext = NULL;
*audioFormatContext = NULL,
*audioFormatContext_1 = NULL;
// Open InputFormatContext of demo2.mp4
ret = avformat_open_input(&inputFormatContext, fileName, NULL, NULL);
......@@ -55,7 +58,19 @@ int main(int argc, char *argv[]) {
}
ret = avformat_find_stream_info(audioFormatContext, NULL);
if (ret < 0) {
printf("Failed to find stream info");
printf("Failed to find stream info\n");
return 0;
}
ret = avformat_open_input(&audioFormatContext_1, audioFile_1, NULL, NULL);
if (ret < 0) {
printf("Failed to open Media_1\n");
return 0;
}
ret = avformat_find_stream_info(audioFormatContext_1, NULL);
if (ret < 0) {
printf("Failed to find stream info\n");
return 0;
}
// Check Precondition
......@@ -69,6 +84,11 @@ int main(int argc, char *argv[]) {
return 0;
}
if (audioFormatContext->nb_streams > 1) {
printf("./audio_1.mp3 has more than one streams\n");
return 0;
}
// Alloc OutputFormatContext
avformat_alloc_output_context2(
&outputFormatContext, NULL, NULL, outFileName);
......@@ -77,13 +97,14 @@ int main(int argc, char *argv[]) {
return 0;
}
int ostreams_idx[MAX_STREAM] = { -1, -1 };
int ostreams_idx[MAX_STREAM] = { -1, -1, -1 };
AVStream* streams[MAX_STREAM];
AVStream *in_stream, *out_stream;
AVCodecParameters *in_codecpar;
streams[V_STREAM] = inputFormatContext->streams[0];
streams[A_STREAM] = audioFormatContext->streams[0];
streams[A_STREAM_1] = audioFormatContext_1->streams[0];
// Create Video Stream and Audio Stream
// for OutputFormtContext.
......@@ -129,7 +150,11 @@ int main(int argc, char *argv[]) {
uint8_t idx = V_STREAM;
AVFormatContext *contexts[MAX_STREAM] = { inputFormatContext, audioFormatContext };
AVFormatContext *contexts[MAX_STREAM] = {
inputFormatContext,
audioFormatContext,
audioFormatContext_1,
};
AVFormatContext *context;
int audioFrameCount = 0;
......@@ -153,16 +178,34 @@ int main(int argc, char *argv[]) {
if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
nbFrames == -1) {
nbFrames = numFramesToMerged(streams[V_STREAM], streams[A_STREAM],
nbFrames = numFramesToMerged(streams[idx], streams[idx],
packet.duration, shift);
}
packet_rescale(&packet, in_stream, out_stream, idx);
switch (idx) {
case V_STREAM:
printf("V-STREAM:\n");
break;
case A_STREAM:
printf("A-STREAM:\n");
break;
case A_STREAM_1:
printf("A_STREAM_1:\n");
break;
}
printf("P: %d, D: %d, Dur: %d, TB: %d/%d\n",
packet.pts, packet.dts, packet.duration,
out_stream->time_base.num,
out_stream->time_base.den);
// Do audio shift
if (nbFrames > 0 && in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
packet.pts += temporal_shift;
packet.dts += temporal_shift;
audioFrameCount++;
}
......@@ -184,6 +227,8 @@ int main(int argc, char *argv[]) {
av_write_trailer(outputFormatContext);
avformat_close_input(&inputFormatContext);
avformat_close_input(&audioFormatContext);
avformat_close_input(&audioFormatContext_1);
avio_closep(&outputFormatContext->pb);
avformat_free_context(outputFormatContext);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment