无法播放 Libavcodec (ffmpeg) 编码示例的视频输出

标签 c video ffmpeg video-encoding libavcodec

在 FFMPEG 的 GitHub 上,我使用 encode_video.c 生成 1 秒的视频。这是有问题的示例:https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/encode_video.c

我编译时使用:gcc -Wall -o ffencodeencode_video.c -lavcodec -lavutil -lz -lm

干净编译,零警告。

我通过运行来测试程序:./ffencode video.mp4 libx264

打印出大量统计信息(预计基于源代码)以及 ffmpeg 日志,但最终没有错误或警告。

但是,生成的输出video.mp4只能通过ffplay播放,VLC Player(以及Google Chrome)无法播放视频。

通过 vlc 命令行播放它实际上会打印:

[00007ffd3550fec0] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
TagLib: MP4: Invalid atom size
TagLib: MP4: Invalid atom size
TagLib: MP4: Invalid atom size

查看 ffprobe 输出,比特率和持续时间字段为空:

Input #0, h264, from 'video.mp4':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p(progressive), 352x288, 25 fps, 25 tbr, 1200k tbn, 50 tbc

我使用 ffmpeg 4.1 并进行以下配置:

ffprobe version 4.1 Copyright (c) 2007-2018 the FFmpeg developers
  built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gpl --enable-libmp3lame --enable-libopus --enable-libsnappy --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-opencl --enable-videotoolbox
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100

有什么想法可以解决这个问题吗?看到 API 的官方示例缺乏这样的基本信息,真是令人惊讶。

最佳答案

您需要将视频流混合到视频容器中,例如 .mp4。多路复用的内容以 libav 格式保存。算法应该是这样的:

  • 通过调用 av_register_all 或手动注册感兴趣的格式来初始化格式库。
  • 通过调用 avformat_alloc_context 创建多路复用上下文
  • 通过调用 avformat_new_stream 创建一个或多个媒体流
  • 通过调用 avformat_write_header 写入 header
  • 通过调用av_write_frame写入媒体数据
  • 通过调用 av_write_trailer 写入预告片
  • 通过调用 avformat_free_context 销毁多路复用上下文

关于无法播放 Libavcodec (ffmpeg) 编码示例的视频输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53328946/

相关文章:

iphone - 如何在 iPhone 的 OpenGL ES 中显示视频?

java - ffmpeg 问题(mpeg1video ->?)

c - 使用 avcodec_decode_audio4 解码 FLAC 文件效果不佳

python - 在 AWS Lambda 函数中使用 python 运行 ffmpeg 命令的问题

c++ - 通知线程有关变量(信号?)的变化

c - 当指向前一个节点的指针不可用时从单个链表中删除中间节点

c - 为什么我们不能在 C 中多次初始化结构?

C++ 类型 unsigned long int

c++ - MediaFoundation,位图数组到 mp4

video - 如何使用 FFMPEG 分割视频以便每个 block 都以关键帧开始?