[C] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
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;
int ret = 0;
int i = 0;
int have_key = 0;
static int first_pkt = 1;
av_register_all();
if (avformat_open_input(&input_fmtctx, "./a.ts", NULL, NULL) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open the file %s\n", "./a.mp4");
return -ENOENT;
}
if (avformat_find_stream_info(input_fmtctx,0) < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to retrieve input stream information\n");
return -EINVAL;
}
av_dump_format(input_fmtctx, NULL, "./a.ts", 0);
if (avformat_alloc_output_context2(&output_fmtctx, NULL, "hls", "./fuck.m3u8") < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open the file %s\n", "./fuck.m3u8");
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, "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;
}
}
av_dump_format(output_fmtctx, NULL, "./fuck.m3u8", 1);
if (avio_open2(&output_fmtctx->pb, "./fuck.m3u8", 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\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;
ret = av_write_frame(output_fmtctx, &pkt);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "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);
return 0;
}