ChinaFFmpeg

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 39927|回复: 32

[Linux] code实现从ts切片成m3u8/HLS

[复制链接]
发表于 2014-8-15 15:31:03 | 显示全部楼层 |阅读模式
从ts文件生成m3u8/hls,其实只要将ts按照keyframe进行切分,生成m3u8即可,
使用的是hls或者segment就可以了
[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;

}




编译方法如下:
gcc -v -g demux.c -o demux -lavformat -lavcodec -lavutil -lx264 -lx265 -lmp3lame -lfaac -lbz2 -lz -lm

为什么这么编译呢?因为我的ffmpeg编译的时候用到了x264和265
[AppleScript] 纯文本查看 复制代码
[root@localhost transcode]# 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 [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'


回复

使用道具 举报

发表于 2014-8-20 09:33:43 | 显示全部楼层
大师兄  我从代码里面没有看到关键帧啊 是我眼瞎!!??
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帧来分片,这样就能保证分片之间的完全独立性,可行不?

回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-20 12:50:30 | 显示全部楼层
A.Do.Be 发表于 2014-8-20 09:33
大师兄  我从代码里面没有看到关键帧啊 是我眼瞎!!??
segment.c里面判断分片条件是这个if语句
if (pkt- ...

可行
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-20 12:50:35 | 显示全部楼层
A.Do.Be 发表于 2014-8-20 09:33
大师兄  我从代码里面没有看到关键帧啊 是我眼瞎!!??
segment.c里面判断分片条件是这个if语句
if (pkt- ...

可行
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-20 12:50:49 | 显示全部楼层
A.Do.Be 发表于 2014-8-20 09:33
大师兄  我从代码里面没有看到关键帧啊 是我眼瞎!!??
segment.c里面判断分片条件是这个if语句
if (pkt- ...

可行
回复 支持 反对

使用道具 举报

发表于 2014-8-20 16:39:06 | 显示全部楼层

关键是怎么找出IDR帧?    AVPacket 结构体没发现, 只有AV_PKT_FLAGS_KEY ,关键帧不一定都是IDR帧,如果是一般的I帧就不完全独立了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-20 17:02:38 | 显示全部楼层
A.Do.Be 发表于 2014-8-20 16:39
关键是怎么找出IDR帧?    AVPacket 结构体没发现, 只有AV_PKT_FLAGS_KEY ,关键帧不一定都是IDR帧,如 ...

keyframe不够用么?
回复 支持 反对

使用道具 举报

发表于 2014-9-22 11:18:14 | 显示全部楼层
孙悟空 发表于 2014-8-20 17:02
keyframe不够用么?

m3u8,在网页内播放,不嵌入vlc播放器能播放出来吗?
回复 支持 反对

使用道具 举报

发表于 2014-10-30 15:36:01 | 显示全部楼层
有web播放器支持m3u8的,避免广告嫌疑不发了,你google
回复 支持 反对

使用道具 举报

发表于 2015-3-4 16:43:16 | 显示全部楼层
好东西
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|Archiver|ChinaFFmpeg

GMT+8, 2024-4-20 08:19 , Processed in 0.067675 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表