FFMPEG复制如何工作?

Tyl*_*oks 5 ffmpeg h.264 http-live-streaming libavformat

如何实现此命令的等效项:

ffmpeg -i test.h264 -vcodec copy -hls_time 5 -hls_list_size 0 foo.m3u8
Run Code Online (Sandbox Code Playgroud)

我的理解是您可以执行以下操作(为清楚起见,使用伪代码):

avformat_open_input(&ctx_in, "test.h264", NULL, NULL);
avformat_find_stream_info(ctx_in, NULL);

avformat_alloc_output_context2(&ctx_out, NULL, "hls", "foo.m3u8");
strm_out = avformat_new_stream(ctx_out, NULL);
strm_out->time_base = (AVRational){1000000, FRAMERATE};

strm_out->codecpar->codec_id = AV_CODEC_ID_H264;
strm_out->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
strm_out->codecpar->width = width;
strm_out->codecpar->height = height;
strm_out->codecpar->bit_rate = bitrate;
strm_out->codecpar->codec_tag = 0;

avcodec_parameters_copy(ctx_out->streams[0]->codecpar,
  ctx_in->streams[0]->codecpar);

avformat_write_header(ctx_out, NULL);

while(1) {
  AVPacket pkt;

  ret = av_read_frame(ctx_in, &pkt);
  if (ret < 0)
    break;

  pkt.pos = -1;
  pkt.stream_index = 0;
  pkt.dts = AV_NOPTS_VALUE;
  pkt.pts = AV_NOPTS_VALUE;
  pkt.pts = SOME_DURATION;

  av_write_frame(ctx_out, &pkt);
  av_packet_unref(&pkt);
}
avformat_close_input(&ctx_in);
av_write_trailer(ctx_out);
avformat_free_context(ctx_out);
Run Code Online (Sandbox Code Playgroud)

我在avformat_find_stream_info()通话中崩溃。我认为是这种情况,因为H264元素流实际上不是容器(avformat想要的)。

实现基本流的FFMPEG'copy'命令的正确方法是什么?