音视频文件转封装操作就是把一种格式转换为另外一种格式,例如从 flv 转到 MP4,或者把流地址数据转换为MP4。
本文主要讲解 流地址数据 转为 flv文件。

2.1 在使用FFmpeg API之前,需要先注册API,然后才能使用API。当然,新版本的库不需要再调用下面的方法。
av_register_all()
2.2构建输入AVFormatContext
声明输入的封装结构体,通过输入文件或者流地址作为封装结构的句柄。韩国电视台的流地址rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp。
- AVFormatContext* inputFmtCtx = nullptr;
- const char* inputUrl = "rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp";
-
- ///打开输入的流,获取数据 begin
- int ret = avformat_open_input(&inputFmtCtx, inputUrl, NULL, NULL);
2.3 查找音视频流信息,通过下面的接口与AVFormatContext中建立输入文件对应的流信息。
-
- //查找;
- if (avformat_find_stream_info(inputFmtCtx, NULL) < 0)
- {
- printf("Couldn't find stream information.\n");
- return -1;
- }
2.4构建输出AVFormatContext
- //输出的文件
- AVOutputFormat *ofmt = NULL;
- AVFormatContext *ofmt_ctx = NULL;
- const char* out_filename = "out.flv";
-
- avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
- if (!ofmt_ctx)
- {
- return -1;
- }
2.5申请输出的stream信息。
- AVStream* out_stream = NULL;
- //创建一个新的流
- out_stream = avformat_new_stream(ofmt_ctx, NULL);
- if (!out_stream)
- {
- return -1;
- }
2.6信息的复制,输出的stream信息建立完成之后,需要从输入的stream中将信息复制到输出的stream中。
- //输入的流 视频、音频、字幕等
- AVStream* in_stream = ifmt_ctx->streams[i];
- AVCodecParameters* in_codecpar = in_stream->codecpar;
-
- //复制输入的流信息到输出流中
- ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
- if (ret < 0)
- {
- return -1;
- }
2.7然后打开文件
- //打开输出文件
- ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
- if (ret < 0)
- {
- return -1;
- }
2.8写文件头
- //写入头
- ret = avformat_write_header(ofmt_ctx, NULL);
- if (ret < 0)
- {
- return -1;
- }
2.9数据包读取和写入
输入和输出均已打开,接下来可以从输入格式中读取数据包,然后将数据包写入到输出文件中,当然,随着输入的封装格式与输出的封装格式差别化,时间戳也需要进行计算改变。
- AVPacket pkt;
- while (1)
- {
- AVStream* in_stream = NULL;
- AVStream* out_stream = NULL;
-
- //从输入流中读取数据到pkt中
- ret = av_read_frame(ifmt_ctx, &pkt);
- if (ret < 0)
- break;
-
- in_stream = ifmt_ctx->streams[pkt.stream_index];
- if (pkt.stream_index >= stream_mapping_size || stream_mapping[pkt.stream_index] < 0)
- {
- av_packet_unref(&pkt);
- continue;
- }
- pkt.stream_index = stream_mapping[pkt.stream_index];
- out_stream = ofmt_ctx->streams[pkt.stream_index];
-
- /* copy packet */
- pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
- pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
- pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
- pkt.pos = -1;
-
- ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
- if (ret < 0)
- {
- fprintf(stderr, "Error muxing packet\n");
- break;
- }
- av_packet_unref(&pkt);
- }
2.10写文件尾
- //写文件尾
- av_write_trailer(ofmt_ctx);
2.11收尾,关闭输入输出,释放资源。
- //关闭
- avformat_close_input(&ifmt_ctx);
-
- if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
- avio_closep(&ofmt_ctx->pb);
-
- avformat_free_context(ofmt_ctx);
- av_freep(&stream_mapping);
- if (ret < 0 && ret != AVERROR_EOF)
- {
- return -1;
- }
- #include "pch.h"
- #include
-
- extern "C"
- {
- #include "libavformat/avformat.h"
- #include "libavutil/dict.h"
- #include "libavutil/opt.h"
- #include "libavutil/timestamp.h"
- #include "libswscale/swscale.h"
- #include "libswresample/swresample.h"
- };
-
- int main()
- {
- //av_register_all();
- avformat_network_init();
-
- AVFormatContext* ifmt_ctx = NULL;
- const char* inputUrl = "rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp";
-
- ///打开输入的流
- int ret = avformat_open_input(&ifmt_ctx, inputUrl, NULL, NULL);
- if (ret != 0)
- {
- printf("Couldn't open input stream.\n");
- return -1;
- }
-
- //查找流信息
- if (avformat_find_stream_info(ifmt_ctx, NULL) < 0)
- {
- printf("Couldn't find stream information.\n");
- return -1;
- }
-
- //输出的文件
- AVOutputFormat *ofmt = NULL;
- AVFormatContext *ofmt_ctx = NULL;
- const char* out_filename = "out.flv";
-
- avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
- if (!ofmt_ctx)
- {
- return -1;
- }
-
- int stream_mapping_size = ifmt_ctx->nb_streams;
-
- //为数组分配内存
- int* stream_mapping = (int *)av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
- if (!stream_mapping)
- {
- return -1;
- }
-
- int stream_index = 0;
- ofmt = ofmt_ctx->oformat;
- for (int i = 0; i < ifmt_ctx->nb_streams; i++)
- {
- //输出的流
- AVStream* out_stream = NULL;
-
- //输入的流 视频、音频、字幕等
- AVStream* in_stream = ifmt_ctx->streams[i];
- AVCodecParameters* in_codecpar = in_stream->codecpar;
- if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO && in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO && in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
- {
- stream_mapping[i] = -1;
- continue;
- }
- stream_mapping[i] = stream_index++;
-
- //创建一个新的流
- out_stream = avformat_new_stream(ofmt_ctx, NULL);
- if (!out_stream)
- {
- return -1;
- }
-
- //复制输入的流信息到输出流中
- ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
- if (ret < 0)
- {
- return -1;
- }
- out_stream->codecpar->codec_tag = 0;
- }
-
- if (!(ofmt->flags & AVFMT_NOFILE))
- {
- //打开输出文件
- ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
- if (ret < 0)
- {
- return -1;
- }
- }
-
- //写入头
- ret = avformat_write_header(ofmt_ctx, NULL);
- if (ret < 0)
- {
- return -1;
- }
-
- AVPacket pkt;
- while (1)
- {
- AVStream* in_stream = NULL;
- AVStream* out_stream = NULL;
-
- //从输入流中读取数据到pkt中
- ret = av_read_frame(ifmt_ctx, &pkt);
- if (ret < 0)
- break;
-
- in_stream = ifmt_ctx->streams[pkt.stream_index];
- if (pkt.stream_index >= stream_mapping_size || stream_mapping[pkt.stream_index] < 0)
- {
- av_packet_unref(&pkt);
- continue;
- }
- pkt.stream_index = stream_mapping[pkt.stream_index];
- out_stream = ofmt_ctx->streams[pkt.stream_index];
-
- /* copy packet */
- pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
- pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
- pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
- pkt.pos = -1;
-
- ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
- if (ret < 0)
- {
- fprintf(stderr, "Error muxing packet\n");
- break;
- }
- av_packet_unref(&pkt);
- }
-
- //写文件尾
- av_write_trailer(ofmt_ctx);
-
- //关闭
- avformat_close_input(&ifmt_ctx);
-
- if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
- avio_closep(&ofmt_ctx->pb);
-
- avformat_free_context(ofmt_ctx);
- av_freep(&stream_mapping);
- if (ret < 0 && ret != AVERROR_EOF)
- {
- return -1;
- }
-
- return 0;
- }
-