FFmpeg封裝格式處理4-轉封裝例程
本文為作者原創,轉載請註明出處: https://www.cnblogs.com/leisure_chn/p/10506662.html
FFmpeg封裝格式處理相關內容分為如下幾篇文章:
[1]. FFmpeg封裝格式處理-簡介
[2]. FFmpeg封裝格式處理-解複用例程
[3]. FFmpeg封裝格式處理-複用例程
[4]. FFmpeg封裝格式處理-轉封裝例程
5. 轉封裝例程
轉封裝是將一種封裝格式轉換為另一種封裝格式,不涉及編解碼操作,轉換速度非常快。
5.1 原始碼
原始碼修改自 FFmpeg 4.1 自帶的例程 remuxing.c。程式碼非常簡短:
#include <libavutil/timestamp.h> #include <libavformat/avformat.h> int main(int argc, char **argv) { AVOutputFormat *ofmt = NULL; AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL; AVPacket pkt; const char *in_filename, *out_filename; int ret, i; int stream_index = 0; int *stream_mapping = NULL; int stream_mapping_size = 0; if (argc < 3) { printf("usage: %s input output\n" "API example program to remux a media file with libavformat and libavcodec.\n" "The output format is guessed according to the file extension.\n" "\n", argv[0]); return 1; } in_filename= argv[1]; out_filename = argv[2]; // 1. 開啟輸入 // 1.1 讀取檔案頭,獲取封裝格式相關資訊 if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) { printf("Could not open input file '%s'", in_filename); goto end; } // 1.2 解碼一段資料,獲取流相關資訊 if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) { printf("Failed to retrieve input stream information"); goto end; } av_dump_format(ifmt_ctx, 0, in_filename, 0); // 2. 開啟輸出 // 2.1 分配輸出ctx avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename); if (!ofmt_ctx) { printf("Could not create output context\n"); ret = AVERROR_UNKNOWN; goto end; } stream_mapping_size = ifmt_ctx->nb_streams; stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping)); if (!stream_mapping) { ret = AVERROR(ENOMEM); goto end; } ofmt = ofmt_ctx->oformat; for (i = 0; i < ifmt_ctx->nb_streams; i++) { AVStream *out_stream; 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++; // 2.2 將一個新流(out_stream)新增到輸出檔案(ofmt_ctx) out_stream = avformat_new_stream(ofmt_ctx, NULL); if (!out_stream) { printf("Failed allocating output stream\n"); ret = AVERROR_UNKNOWN; goto end; } // 2.3 將當前輸入流中的引數拷貝到輸出流中 ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar); if (ret < 0) { printf("Failed to copy codec parameters\n"); goto end; } out_stream->codecpar->codec_tag = 0; } av_dump_format(ofmt_ctx, 0, out_filename, 1); if (!(ofmt->flags & AVFMT_NOFILE)) {// TODO: 研究AVFMT_NOFILE標誌 // 2.4 建立並初始化一個AVIOContext,用以訪問URL(out_filename)指定的資源 ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE); if (ret < 0) { printf("Could not open output file '%s'", out_filename); goto end; } } // 3. 資料處理 // 3.1 寫輸出檔案頭 ret = avformat_write_header(ofmt_ctx, NULL); if (ret < 0) { printf("Error occurred when opening output file\n"); goto end; } while (1) { AVStream *in_stream, *out_stream; // 3.2 從輸出流讀取一個packet 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 */ // 3.3 更新packet中的pts和dts // 關於AVStream.time_base的說明: // 輸入:輸入流中含有time_base,在avformat_find_stream_info()中可取到每個流中的time_base // 輸出:avformat_write_header()會根據輸出的封裝格式確定每個流的time_base並寫入檔案中 // AVPacket.pts和AVPacket.dts的單位是AVStream.time_base,不同的封裝格式其AVStream.time_base不同 // 所以輸出檔案中,每個packet需要根據輸出封裝格式重新計算pts和dts av_packet_rescale_ts(&pkt, in_stream->time_base, out_stream->time_base); pkt.pos = -1; // 3.4 將packet寫入輸出 ret = av_interleaved_write_frame(ofmt_ctx, &pkt); if (ret < 0) { printf("Error muxing packet\n"); break; } av_packet_unref(&pkt); } // 3.5 寫輸出檔案尾 av_write_trailer(ofmt_ctx); end: avformat_close_input(&ifmt_ctx); /* close output */ 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) { printf("Error occurred: %s\n", av_err2str(ret)); return 1; } return 0; }
5.2 編譯
原始檔為remuxing.c,在SHELL中執行如下編譯命令:
gcc -o remuxing remuxing.c -lavformat -lavcodec -lavutil -g
生成可執行檔案remuxing
5.3 驗證
think@opensuse> ffprobe tnliny.flv ffprobe version 4.1 Copyright (c) 2007-2018 the FFmpeg developers Input #0, flv, from 'tnliny.flv': Metadata: encoder: Lavf58.20.100 Duration: 00:02:26.54, start: 0.000000, bitrate: 446 kb/s Stream #0:0: Video: h264 (High), yuv420p(progressive), 800x450, 25 fps, 25 tbr, 1k tbn, 50 tbc Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp
執行如下命令進行測試:
./remuxing tnliny.flv tnliny.ts
使用ffprobe檢測輸出檔案正常。使用ffplay播放輸出檔案正常,播放效果和原始的測試檔案一致。