1212122
来源:互联网 发布:dt990和dt990pro 知乎 编辑:程序博客网 时间:2024/05/22 12:51
#include <stdio.h>#include <stdlib.h>#include <libavformat/avformat.h>#include <libavutil/avutil.h> #define FORMAT "mpegts"#define OUTPUT_FILE "./fuck.ts"#define INPUT_FILE "./a.avi" int main(int argc, char *argv[]){ AVPacket pkt; AVFormatContext *input_fmtctx = NULL; AVFormatContext *output_fmtctx = NULL; AVCodecContext *enc_ctx = NULL; AVCodecContext *dec_ctx = NULL; AVCodec *encoder = NULL; AVBitStreamFilterContext * vbsf = NULL; int ret = 0; int i = 0; int have_key = 0; static int first_pkt = 1; av_register_all(); if (avformat_open_input(&input_fmtctx, INPUT_FILE, NULL, NULL) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot open the file %s bbs.chinaffmpeg.com 孙悟空\n", "./a.mp4"); return -ENOENT; } if (avformat_find_stream_info(input_fmtctx,0) < 0) { av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.com 孙悟空 Failed to retrieve input stream information\n"); return -EINVAL; } av_dump_format(input_fmtctx, NULL, INPUT_FILE, 0); if (avformat_alloc_output_context2(&output_fmtctx, NULL, FORMAT, OUTPUT_FILE) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot open the file %s bbs.chinaffmpeg.com 孙悟空 \n", OUTPUT_FILE); return -ENOENT; } /* Process transcode parameters */ for (i = 0; i < input_fmtctx->nb_streams; i++) { AVStream *out_stream = NULL; AVStream *in_stream = NULL; in_stream = input_fmtctx->streams[i]; out_stream = avformat_new_stream(output_fmtctx, in_stream->codec->codec); if (out_stream < 0) { av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.com 孙悟空 Alloc new Stream error\n"); return -EINVAL; } avcodec_copy_context(output_fmtctx->streams[i]->codec, input_fmtctx->streams[i]->codec); out_stream->codec->codec_tag = 0; if (output_fmtctx->oformat->flags & AVFMT_GLOBALHEADER) { out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; } } vbsf = av_bitstream_filter_init("h264_mp4toannexb"); av_dump_format(output_fmtctx, NULL, OUTPUT_FILE, 1); if (avio_open2(&output_fmtctx->pb, OUTPUT_FILE, AVIO_FLAG_WRITE, &output_fmtctx->interrupt_callback, NULL) < 0) { av_log(NULL, AV_LOG_ERROR, "cannot open the output file '%s'\n", "./fuck.m3u8"); return -ENOENT; } if ((ret = avformat_write_header(output_fmtctx, NULL)) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot write the header for the file '%s' ret = %d bbs.chinaffmpeg.com 孙悟空\n", "fuck.ts", ret); return -ENOENT; } while (1) { AVStream *in_stream = NULL; AVStream *out_stream = NULL; ret = av_read_frame(input_fmtctx, &pkt); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "read frame error %d\n", ret); break; } if (pkt.pts == AV_NOPTS_VALUE && first_pkt) { pkt.pts = pkt.dts; first_pkt = 0; } in_stream = input_fmtctx->streams[pkt.stream_index]; out_stream = output_fmtctx->streams[pkt.stream_index]; pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, 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; if (pkt.stream_index == 0) { AVPacket fpkt = pkt; av_bitstream_filter_filter(vbsf,out_stream->codec, NULL, &fpkt.data, &fpkt.size,pkt.data, pkt.size, pkt.flags & AV_PKT_FLAG_KEY); pkt.data = fpkt.data; pkt.size = fpkt.size; } ret = av_write_frame(output_fmtctx, &pkt); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.com 孙悟空 Muxing Error\n"); break; } av_free_packet(&pkt); } av_write_trailer(output_fmtctx); avformat_close_input(&input_fmtctx); avio_close(output_fmtctx->pb); avformat_free_context(output_fmtctx); av_bitstream_filter_close(vbsf); vbsf = NULL; return 0; }
0 0