Php-ffmpeg: Look into the `Encoding failed` error and support troubleshooting

Created on 21 Feb 2017  路  21Comments  路  Source: PHP-FFMpeg/PHP-FFMpeg

Saving to WMV works fine
->save(new FFMpegFormatVideoWMV(), 'export-wmv.wmv')

But when saving to X264
->save(new FFMpegFormatVideoX264(), 'export-x264.mp4')
It throws Encoding failed exception
here is the full message

Exception message: ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-y' '-i' '/tmp/php08gTC4' '-vcodec' 'libx264' '-acodec' 'libmp3lame' '-b:v' '1000k' '-refs' '6' '-coder' '1' '-sc_threshold' '40' '-flags' '+loop' '-me_range' '16' '-subq' '7' '-i_qfactor' '0.71' '-qcomp' '0.6' '-qdiff' '4' '-trellis' '1' '-b:a' '128k' '-pass' '1' '-passlogfile' '/tmp/ffmpeg-passes58ac0992d1792k200a/pass-58ac0992d2042' 'export-wmv.mp4'

Also tried to use different audio encoding
$format = new FFMpegFormatVideoX264('libmp3lame', 'libx264');

I'm using laravel 4.2. Thanks

Enhancement help wanted

Most helpful comment

Here's the proper way to do it:

$format = new FFMpeg\Format\Video\X264();
$format->setAudioCodec("libmp3lame");

$video
    ->save($format, '/path/to/new/file');

All 21 comments

Here's the proper way to do it:

$format = new FFMpeg\Format\Video\X264();
$format->setAudioCodec("libmp3lame");

$video
    ->save($format, '/path/to/new/file');

Thanks @Romain you've made my day!

You're welcome.

Is it good for you @DVendy?

@Romain Why is it using libfaac instead of libmp3lame in default? Is libfaac faster? Anyway, we should check wether the codec exists before trying to encode with a non-existent codec.

And Is there is a way to get more verbose reason why encoding was failed? Because it's always the same error when directory don't have write access, when audio codec not supported and so on.

Still the same error :/
Did I miss something? Should I install some additional file?

@DVendy Could you show us a output from ffmpeg -version?

@jens1o I agree with you. I don't know why libfaac is the default one. It has been developed like this.
Moreover, AAC is really complicated to handle, there has been changes with this format and its codecs lately.
I think we should switch for libmp3lame indeed, and I agree with the check. I'll try to work on something and week or the following one.

audiocodec use aac

@DVendy

When using $format->setAudioCodec('libfdk_aac'); cause this issue too
ffmpeg -version
ffmpeg version N-87353-g183fd30 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 7.2.0 (GCC)
configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib
libavutil 55. 76.100 / 55. 76.100
libavcodec 57.106.101 / 57.106.101
libavformat 57. 82.101 / 57. 82.101
libavdevice 57. 8.101 / 57. 8.101
libavfilter 6.105.100 / 6.105.100
libswscale 4. 7.103 / 4. 7.103
libswresample 2. 8.100 / 2. 8.100
libpostproc 54. 6.100 / 54. 6.100

@nhoctanker This seems to be a ffmpeg issue :/

@jens1o Change to $format->setAudioCodec("libmp3lame"); that is fast solution.

Encountered this issue recently using recently laravel 5.6 and ffmpeg 3.4.2, i found (as many others before me) that although using ("libmp3lame") as the audio codec does succeed, the down side is it does not produce audio when .mp4's are view on iOS devices. After some research i found that many of these older "libaac" codecs have been deprecated. I then found that there was newer stable aac codec included in the latest version of FFMPEG that can be used simply by specifying "aac" in the audio codec parameter. For example:

$format = new FFMpeg\Format\Video\X264('aac', 'libx264')

After i setup my format like that, any conversion to .mp4 or to .mov succeeded and audio and video was viewable on all android and ios devices

I then found that there was newer stable aac codec included in the latest version of FFMPEG that can be used simply by specifying "aac" in the audio codec parameter.

Thanks for the heads up, scheduling this to set as default for v1.x.

Edit: This has been implemented.

I was getting this error because the target folder didn't exist. Perhaps ffmpeg failed to write to non existent folder. It would be awesome if you could handle errors and exceptions separately.
"Encoding failed" doesn't help much except frustrating you all day long :O

"Encoding failed" doesn't help much except frustrating you all day long :O

This would be indeed great. We should look for the actual error instead of throwing a very general one.

If you want to create a pr, feel free!

I had the same problem. I used that

$ chmod -R g+w youroutputmoviedir

Problem is solved.

Here's the proper way to do it:

$format = new FFMpeg\Format\Video\X264();
$format->setAudioCodec("libmp3lame");

$video
    ->save($format, '/path/to/new/file');

Hello @Romain ,
The video generated using this code with No Audio on ios. Please guide.

As loveglove said
His method is correct

Not sure why it is hard for PHP-FFMpeg to show the actual error the ffmpeg has thrown unless there's already a way to enable showing that debug message somewhere?

For me I just var_dump'ed the exception object from save method in FFMpeg/Media/AbstractVideo.php that's caught on line 97 as of version 0.16.0 and it clearly stated the error was,

The encoder 'aac' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it. [aac @ 0xf26720] Alternatively use the non experimental encoder 'libfdk_aac'.

So, I just switched to,
new FFMpeg\Format\Video\X264('libfdk_aac', 'libx264')
and the problem was gone.

This error came from the nux-dextop repository version for CentOS 7.

Also had another error as,

Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

which was caused by width or height having an odd number while resizing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aminyazdanpanah picture aminyazdanpanah  路  6Comments

Gemorroj picture Gemorroj  路  8Comments

miraklo picture miraklo  路  6Comments

pascalbaljet picture pascalbaljet  路  4Comments

bfeyaerts picture bfeyaerts  路  6Comments