code实现从ts切片成m3u8/HLS
从ts文件生成m3u8/hls,其实只要将ts按照keyframe进行切分,生成m3u8即可,使用的是hls或者segment就可以了
#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;
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->codec, input_fmtctx->streams->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;
out_stream = output_fmtctx->streams;
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;
}
编译方法如下:
gcc -v -g demux.c -o demux -lavformat -lavcodec -lavutil -lx264 -lx265 -lmp3lame -lfaac -lbz2 -lz -lm
为什么这么编译呢?因为我的ffmpeg编译的时候用到了x264和265
# ffmpeg
ffmpeg version N-65108-gf2855eb Copyright (c) 2000-2014 the FFmpeg developers
built on Jul 31 2014 02:29:27 with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-4)
configuration: --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libfaac --enable-libfreetype --enable-libx264 --enable-libx265
libavutil 52. 93.100 / 52. 93.100
libavcodec 55. 71.100 / 55. 71.100
libavformat 55. 49.100 / 55. 49.100
libavdevice 55. 13.102 / 55. 13.102
libavfilter 4. 11.102 /4. 11.102
libswscale 2.6.100 /2.6.100
libswresample 0. 19.100 /0. 19.100
libpostproc 52.3.100 / 52.3.100
Hyper fast Audio and Video encoder
usage: ffmpeg [ -i infile]... { outfile}...
Use -h to get full help or, even better, run 'man ffmpeg'
大师兄我从代码里面没有看到关键帧啊 是我眼瞎!!??
segment.c里面判断分片条件是这个if语句
if (pkt->stream_index == seg->reference_stream_index &&
pkt->flags & AV_PKT_FLAG_KEY &&
(seg->cut_pending || seg->frame_count >= start_frame ||
(pkt->pts != AV_NOPTS_VALUE &&
av_compare_ts(pkt->pts, st->time_base,
end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0)))
我这边的需求是把ts(h264+aac)的文件根据IDR帧来分片,这样就能保证分片之间的完全独立性,可行不?
A.Do.Be 发表于 2014-8-20 09:33
大师兄我从代码里面没有看到关键帧啊 是我眼瞎!!??
segment.c里面判断分片条件是这个if语句
if (pkt- ...
可行 A.Do.Be 发表于 2014-8-20 09:33
大师兄我从代码里面没有看到关键帧啊 是我眼瞎!!??
segment.c里面判断分片条件是这个if语句
if (pkt- ...
可行 A.Do.Be 发表于 2014-8-20 09:33
大师兄我从代码里面没有看到关键帧啊 是我眼瞎!!??
segment.c里面判断分片条件是这个if语句
if (pkt- ...
可行 孙悟空 发表于 2014-8-20 12:50
可行
关键是怎么找出IDR帧? AVPacket 结构体没发现, 只有AV_PKT_FLAGS_KEY ,关键帧不一定都是IDR帧,如果是一般的I帧就不完全独立了。 A.Do.Be 发表于 2014-8-20 16:39
关键是怎么找出IDR帧? AVPacket 结构体没发现, 只有AV_PKT_FLAGS_KEY ,关键帧不一定都是IDR帧,如 ...
keyframe不够用么? 孙悟空 发表于 2014-8-20 17:02
keyframe不够用么?
m3u8,在网页内播放,不嵌入vlc播放器能播放出来吗? 有web播放器支持m3u8的,避免广告嫌疑不发了,你google 好东西:lol