2012-07-18 592 views
3

编辑:在以前的版本中,我使用了一个非常老的ffmpeg API。我现在使用最新的库。问题只是轻微改变,从“主”到“高”。ffmpeg API h264编码视频无法在所有平台上播放

我使用ffmpeg C API在C++中创建mp4视频。

我希望生成的视频具有“约束基线”配置文件,以便可以在尽可能多的平台上播放结果视频,尤其是移动视频,但每次我都会获得“高”配置文件,即使我将编解码器配置文件硬编码为FF_PROFILE_H264_CONSTRAINED_BASELINE。因此,视频无法在我们所有的测试平台上播放。

这就是“ffprobe video.mp4 -show_streams”讲述我的视频流:

Metadata: 
major_brand  : isom 
minor_version : 512 
compatible_brands: isomiso2avc1mp41 
creation_time : 1970-01-01 00:00:00 
encoder   : Lavf53.5.0 
    Duration: 00:00:13.20, start: 0.000000, bitrate: 553 kb/s 
Stream #0:0(und): Video: h264 (Main) (avc1/0x31637661), yuv420p, 320x180, 
424 kb/s, 15 fps, 15 tbr, 15 tbn, 30 tbc 
Metadata: 
    creation_time : 1970-01-01 00:00:00 
    handler_name : VideoHandler 
Stream #0:1(und): Audio: aac (mp4a/0x6134706D), 44100 Hz, stereo, s16, 12 
kb/s 
Metadata: 
    creation_time : 1970-01-01 00:00:00 
    handler_name : SoundHandler 
-------VIDEO STREAM-------- 
[STREAM] 
index=0 
codec_name=h264 
codec_long_name=H.264/AVC/MPEG-4 AVC/MPEG-4 part 10 

profile=High <-- This should be "Constrained Baseline" 

codec_type=video 
codec_time_base=1/30 
codec_tag_string=avc1 
codec_tag=0x31637661 
width=320 
height=180 
has_b_frames=0 
sample_aspect_ratio=N/A 
display_aspect_ratio=N/A 
pix_fmt=yuv420p 
level=30 
timecode=N/A 
is_avc=1 
nal_length_size=4 
id=N/A 
r_frame_rate=15/1 
avg_frame_rate=15/1 
time_base=1/15 
start_time=0.000000 
duration=13.200000 
bit_rate=424252 
nb_frames=198 
nb_read_frames=N/A 
nb_read_packets=N/A 
TAG:creation_time=1970-01-01 00:00:00 
TAG:language=und 
TAG:handler_name=VideoHandler 
[/STREAM] 
-------AUDIO STREAM-------- 
[STREAM] 
index=1 
codec_name=aac 
codec_long_name=Advanced Audio Coding 
profile=unknown 
codec_type=audio 
codec_time_base=1/44100 
codec_tag_string=mp4a 
codec_tag=0x6134706d 
sample_fmt=s16 
sample_rate=44100 
channels=2 
bits_per_sample=0 
id=N/A 
r_frame_rate=0/0 
avg_frame_rate=0/0 
time_base=1/44100 
start_time=0.000000 
duration=13.165714 
bit_rate=125301 
nb_frames=567 
nb_read_frames=N/A 
nb_read_packets=N/A 
TAG:creation_time=1970-01-01 00:00:00 
TAG:language=und 
TAG:handler_name=SoundHandler 
[/STREAM] 

这是我使用添加一个视频流的功能。所有这一切都来自ptr-值>从外部定义,做那些值必须在特定的值,以获得正确的配置文件?:

static AVStream *add_video_stream(Cffmpeg_dll * ptr, AVFormatContext *oc, enum CodecID codec_id) 
{ 
AVCodecContext *c; 
AVStream *st; 
AVCodec* codec; 

// Get correct codec 
codec = avcodec_find_encoder(codec_id); 
if (!codec) { 
    av_log(NULL, AV_LOG_ERROR, "%s","Video codec not found\n"); 
    exit(1); 
} 

// Create stream 
st = avformat_new_stream(oc, codec); 
if (!st) { 
    av_log(NULL, AV_LOG_ERROR, "%s","Could not alloc stream\n"); 
    exit(1); 
} 

c = st->codec; 

/* Get default values */ 
codec = avcodec_find_encoder(codec_id); 
if (!codec) { 
    av_log(NULL, AV_LOG_ERROR, "%s","Video codec not found (default values)\n"); 
    exit(1); 
} 
avcodec_get_context_defaults3(c, codec); 

c->codec_id = codec_id; 
c->codec_type = AVMEDIA_TYPE_VIDEO; 

c->bit_rate = ptr->video_bit_rate; 
av_log(NULL, AV_LOG_ERROR, " Bit rate: %i", c->bit_rate); 

    c->qmin = ptr->qmin; 
    c->qmax = ptr->qmax; 
    c->me_method = ptr->me_method; 
    c->me_subpel_quality = ptr->me_subpel_quality; 
    c->i_quant_factor = ptr->i_quant_factor; 
    c->qcompress = ptr->qcompress; 
    c->max_qdiff = ptr->max_qdiff; 

    // We need to set the level and profile to get videos that play (hopefully) on all platforms 
    c->level = 30; 
    c->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE; 

c->width = ptr->dstWidth; 
c->height = ptr->dstHeight; 

c->time_base.den = ptr->fps; 
c->time_base.num = 1; 
c->gop_size = ptr->fps; 
c->pix_fmt = STREAM_PIX_FMT; 
c->max_b_frames = 0; 

// some formats want stream headers to be separate 
if(oc->oformat->flags & AVFMT_GLOBALHEADER) 
    c->flags |= CODEC_FLAG_GLOBAL_HEADER; 

return st; 
} 

附加信息:

当参考视频,我使用Mozilla作为可在每个平台/浏览器上播放的示例的gizmo.mp4。它绝对具有“约束基线”配置文件,并且绝对适用于我们所有测试的智能手机。 You can download it here.我们自制的视频不适用于所有平台,我相信这是因为配置文件。

我也在使用qt-faststart.exe在创建mp4后将标题移动到文件的开头,因为这不能在C++中直接完成。这可能是问题吗?

显然,我在做东西错了,但我不知道它可能是什么。我会感谢每一个提示;)

+0

你应该在ffmpeg邮件列表上提交一个错误报告。如您所期望的,这应该表示为基线或基线限制。 – SilverbackNet 2012-07-18 09:19:49

+0

我更新了我的ffmpeg版本,问题从“Main”稍微变为“High”配置文件。 @SilverbackNet如果这不能解决,将会做。 – TheSHEEEP 2012-07-18 12:00:10

+0

有趣的是,当您通过命令行使用ffmpeg.exe并设置“-vprofile baseline”时,它按预期工作。我们对ffmpeg.exe和我们使用的C库都使用相同的ffmpeg版本。 – TheSHEEEP 2012-07-18 12:04:56

回答

7

我有解决方案。在ffmpeg bug跟踪器中花费一些时间和讨论并浏览配置文件设置示例后,我终于找到了解决方案。

一个需要使用av_opt_set(codecContext-> priv_data, “轮廓”, “基线”(或任何其他所需的个人资料),AV_OPT_SEARCH_CHILDREN)

所以在我的情况下,这将是:

错误:

// We need to set the level and profile to get videos that play (hopefully) on all platforms 
c->level = 30; 
c->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE; 

正确:

// Set profile to baseline 
av_opt_set(c->priv_data, "profile", "baseline", AV_OPT_SEARCH_CHILDREN); 

完全不直观,违背了API的其他用法,但这是ffmpeg的哲学。你不需要了解它,你只需要了解如何使用它;)

+0

为什么选择配置文件基准?您的意思是配置文件constrainted_baseline的视频无法在某些设备上正常播放? – dragonfly 2015-09-23 10:51:44

+0

“基准”实际上最终在结果文件中被限定为基线。 – TheSHEEEP 2015-09-23 11:22:44

+0

据我所知,baseline和constrained_baseline是h264的2个不同配置文件。你有什么意见? – dragonfly 2015-09-23 11:48:35