我有一个WebM文件,其中包含一个用VP9编码的视频流(libvpx-VP9(。
我编写了一个C++程序,从视频流中提取帧,并将其保存为PNG。除了生成的PNG缺少alpha之外,这一切都很好。
如果我使用FFMPEG从同一个WebM文件中提取帧,则生成的PNG确实包含alpha。以下是FFMPEG:的输出
$ ffmpeg -c:v libvpx-vp9 -i temp/anim.webm temp/output-%3d.png
[libvpx-vp9 @ 0000024732b106c0] v1.10.0-rc1-11-gcb0d8ce31
Last message repeated 1 times
Input #0, matroska,webm, from 'temp/anim.webm':
Metadata:
ENCODER : Lavf58.45.100
Duration: 00:00:04.04, start: 0.000000, bitrate: 112 kb/s
Stream #0:0: Video: vp9 (Profile 0), yuva420p(tv), 640x480, SAR 1:1 DAR 4:3, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
Metadata:
alpha_mode : 1
ENCODER : Lavc58.91.100 libvpx-vp9
DURATION : 00:00:04.040000000
FFMPEG将流格式标识为yuva420p。
以下是调用av_dump_format时程序的输出:
Input #0, matroska,webm, from 'temp/anim.webm':
Metadata:
ENCODER : Lavf58.45.100
Duration: 00:00:04.04, start: 0.000000, bitrate: 112 kb/s
Stream #0:0: Video: vp9 (Profile 0), yuv420p(tv), 640x480, SAR 1:1 DAR 4:3, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
Metadata:
alpha_mode : 1
ENCODER : Lavc58.91.100 libvpx-vp9
DURATION : 00:00:04.040000000
请注意,检测到的流格式是yuv420p(缺少alpha(。
有人知道如何强制流格式使用alpha吗?
我的设置代码类似于以下(省略了错误处理(
auto result = avformat_open_input(&formatContext, fileName.c_str(), nullptr, nullptr);
auto result = avformat_find_stream_info(formatContext, nullptr);
streamIndex = av_find_best_stream(formatContext, mediaType, -1, -1, nullptr, 0);
auto stream = formatContext->streams[streamIndex];
const auto codecIdentifier{ AV_CODEC_ID_VP9 };
auto decoder = avcodec_find_decoder(codecIdentifier);
pCodecContext = avcodec_alloc_context3(decoder);
auto result = avcodec_open2(pCodecContext, decoder, &options);
// AV_PIX_FMT_YUV420P - missing alpha
auto pixelFormat = pCodecContext->pix_fmt;
Gyan指出了问题所在。这是更正后的代码:
In case anybody else runs into this issue in the future here is the code (error handling omitted):
auto formatContext = avformat_alloc_context();
formatContext->video_codec_id = AV_CODEC_ID_VP9;
const auto decoder = avcodec_find_decoder_by_name("libvpx-vp9");
formatContext->video_codec = decoder;
avformat_open_input(&formatContext, fileName.c_str(), nullptr, nullptr);
avformat_find_stream_info(formatContext.get(), nullptr);
for (unsigned int streamIndex = 0; streamIndex < formatContext->nb_streams; ++streamIndex) {
// Displayed the stream format as yuva420p (contains alpha)
av_dump_format(formatContext, static_cast<int>(streamIndex), fileName.toStdString().c_str(), 0);
}
```
Thanks,
就像您的ffmpeg命令一样,您必须强制使用vpx解码器。
使用
auto decoder = avcodec_find_decoder_by_name("libvpx-vp9");