简体   繁体   English

如何使用FFmpeg C API对H.264视频进行编码,然后使用媒体播放器打开输出?

[英]How to encode H.264 video using FFmpeg C API and then open the output with a media player?

I'm trying to encode a H.264 video with the FFMPEG C API. 我正在尝试使用FFMPEG C API对H.264视频进行编码。 I have successfully compiled and executed the decoding/encoding example provided by FFMPEG. 我已经成功编译并执行了FFMPEG提供的解码/编码示例

The problem I'm facing is that the .mpg file (encoded with AV_CODEC_ID_MPEG1VIDEO) the example creates works. 我面临的问题是.mpg文件(用AV_CODEC_ID_MPEG1VIDEO编码)这个例子创建了作品。 Windows creates a thumbnail and everything. Windows会创建缩略图和所有内容。 On the other hand the .h264 (encoded with AV_CODEC_ID_H264) file does not. 另一方面,.h264(用AV_CODEC_ID_H264编码)文件没有。 When I try to play the file in VLC the play/pause button just flickers, no thumbnail in windows, no nothing. 当我尝试在VLC中播放文件时,播放/暂停按钮只是闪烁,窗口中没有缩略图,没有任何内容。

During encoding libx264 reports the following: 在编码期间,libx264报告以下内容:

[libx264 @ 004b81a0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 004b81a0] profile High, level 1.3
[libx264 @ 004b81a0] frame I:3     Avg QP:26.79  size:  2116
[libx264 @ 004b81a0] frame P:12    Avg QP:26.60  size:   789
[libx264 @ 004b81a0] frame B:10    Avg QP:31.39  size:   499
[libx264 @ 004b81a0] consecutive B-frames: 20.0% 80.0%
[libx264 @ 004b81a0] mb I  I16..4: 78.3% 11.6% 10.1%
[libx264 @ 004b81a0] mb P  I16..4: 77.4%  0.6%  0.1%  P16..4: 20.3%  0.7%  0.9%  0.0%  0.0%    skip: 0.0%
[libx264 @ 004b81a0] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  9.4%  0.2%  0.0%  direct:11.0%  skip:79.4%  L0:25.4% L1:35.4% BI:39.2%
[libx264 @ 004b81a0] final ratefactor: 17.03
[libx264 @ 004b81a0] 8x8 transform intra:3.4% inter:31.3%
[libx264 @ 004b81a0] direct mvs  spatial:0.0% temporal:100.0%
[libx264 @ 004b81a0] coded y,uvDC,uvAC intra: 4.4% 35.8% 1.6% inter: 1.3% 34.5% 9.5%
[libx264 @ 004b81a0] i16 v,h,dc,p:  0%  0%  0% 100%
[libx264 @ 004b81a0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu:  7% 20% 17% 49%  0%  0%  0%  1%  6%
[libx264 @ 004b81a0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 14%  7% 18% 45%  2%  7%  2%  3%  2%
[libx264 @ 004b81a0] i8c dc,h,v,p:  2%  6%  4% 88%
[libx264 @ 004b81a0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 004b81a0] ref P L0: 95.6%  1.6%  2.2%  0.5%  0.2%
[libx264 @ 004b81a0] ref B L0: 67.5% 28.5%  3.2%  0.8%
[libx264 @ 004b81a0] kb/s:166.45

"ffprobe.exe test.h264 -show_streams" returns “ffprobe.exe test.h264 -show_streams”返回

[STREAM]
index=0
codec_name=h264
codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
profile=High
codec_type=video
codec_time_base=1/50
codec_tag_string=[0][0][0][0]
codec_tag=0x0000
width=352
height=288
has_b_frames=1
sample_aspect_ratio=0:1
display_aspect_ratio=0:1
pix_fmt=yuv420p
level=13
timecode=N/A
id=N/A
r_frame_rate=50/2
avg_frame_rate=25/1
time_base=1/1200000
start_pts=N/A
start_time=N/A
duration_ts=N/A
duration=N/A
bit_rate=N/A
nb_frames=N/A
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=0
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
[/STREAM]

There you can see: 在那里你可以看到:

start_time=N/A
duration_ts=N/A
duration=N/A

I have tried countless changes to the AVCodecContext and multitude of parameters options to av_opt_set function. 我已尝试对AVCodecContext和av_opt_set函数的众多参数选项进行无数次更改。 Still no luck getting a working H.264 video. 获得有效的H.264视频仍然没有运气。 I have even tried different builds of FFmpeg. 我甚至尝试过不同版本的FFmpeg。 No luck. 没运气。

I'm using the "FFmpeg git-0fb64da 32-bit Dev" build from Zeranoe and MinGw 4.7.2 我正在使用Zeranoe和MinGw 4.7.2的“FFmpeg git-0fb64da 32位Dev”版本

Your problem probably is not encoding itself but the fact that you output raw H.264 stream (I suppose you used AnnexB format) without any muxing. 您的问题可能不是编码本身,而是您输出原始H.264流(我假设您使用了AnnexB格式)而没有任何多路复用。 Most of players expect H.264 in some container (like MKV, MP4 or FLV) and do not support playing of raw H.264 streams. 大多数玩家希望某些容器中使用H.264(如MKV,MP4或FLV),并且不支持播放原始H.264流。 For players using DirectShow filters (like MPC-HC / WMP) it also needs raw H.264 AnnexB demuxer before decoder. 对于使用DirectShow过滤器(如MPC-HC / WMP)的玩家,它在解码器之前还需要原始的H.264 AnnexB解复用器。 Try to mux this output to MKV with MKVToolnix and test resulting file. 尝试使用MKVToolnix将此输出复用到MKV并测试生成的文件。

As mentioned by nobody555 , maybe your VLC simply cannot open that file because it is a raw stream. 正如nobody555提到的 ,也许你的VLC根本无法打开该文件,因为它是一个原始流。

Alternatives are listed at: https://superuser.com/questions/261774/converting-or-playing-a-264-video-file 替代方案列于: https//superuser.com/questions/261774/converting-or-playing-a-264-video-file

ffplay is an option, but it also works on VLC 2.2.1, Ubnuntu 15.10. ffplay是一个选项,但它也适用于VLC 2.2.1,Ubnuntu 15.10。

Also consider the example: https://github.com/FFmpeg/FFmpeg/blob/n3.0/doc/examples/muxing.c which generates a container format instead of a raw stream. 还要考虑以下示例: https//github.com/FFmpeg/FFmpeg/blob/n3.0/doc/examples/muxing.c ,它生成容器格式而不是原始流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM