简体   繁体   English

PHP ffmpeg GIF转MP4

[英]PHP ffmpeg convert GIF to MP4

I use this code to convert GIFs to MP4:我使用此代码将 GIF 转换为 MP4:

$videoname = str_replace('.gif', '.mp4', $picname);
$ffmpeg = FFMpeg\FFMpeg::create(array(
    'ffmpeg.binaries'  => 'ffmpeg/bin/ffmpeg.exe',
    'ffprobe.binaries' => 'ffmpeg/bin/ffprobe.exe',
    'timeout'          => 6600, // The timeout for the underlying process
    'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should use
), $logger);

$video = $ffmpeg->open('memes/' . $folder . '/' . $picname);

$format = new \FFMpeg\Format\Video\X264('libvo_aacenc', 'libx264');
$format->setAdditionalParameters(array('-profile:v', 'baseline', '-pix_fmt', 'yuv420p'));

$video
    ->save($format, 'memes/' . $folder . '/' . $videoname);

Some GIFs are getting converted but some not, this is the error message:有些 GIF 正在转换,但有些没有,这是错误消息:

[16-Aug-2020 14:47:21 Europe/Berlin] PHP Fatal error:  Uncaught Alchemy\BinaryDriver\Exception\ExecutionFailureException: ffmpeg failed to execute command "ffmpeg/bin/ffmpeg.exe" -y -i "memes/38/Dr5W6yzR_gif_38.gif" -threads 12 -vcodec libx264 -acodec libvo_aacenc -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 -profile:v baseline -pix_fmt yuv420p -pass 1 -passlogfile "C:\Windows\TEMP\ffmpeg-passes5f392ad8ba750qewzr/pass-5f392ad8bb9b7" "memes/38/Dr5W6yzR_gif_38.mp4":

Error Output:

 ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 9.2.1 (GCC) 20200122
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --e in C:\Inetpub\vhosts\yolol.de\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\Media\AbstractVideo.php on line 106

What is all this crap?这都是什么废话?

Please help!请帮忙!

EDIT:编辑:

This is what I'm getting when running this code in cmd:这是我在 cmd 中运行此代码时得到的结果:

ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 9.2.1 (GCC) 20200122
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, gif, from 'C://ttt.gif':
  Duration: 00:00:03.08, start: 0.000000, bitrate: 10411 kb/s
    Stream #0:0: Video: gif, bgra, 636x357, 14.25 fps, 14.29 tbr, 100 tbn, 100 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (gif (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0000021dca771a40] height not divisible by 2 (636x357)
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!

So the error is height not divisible by 2 (636x357) I guess, but what would be the solution?所以我猜错误是height not divisible by 2 (636x357) ,但解决方案是什么?

The solution:解决方案:

$format->setAdditionalParameters(array('-profile:v', 'baseline', '-pix_fmt', 'yuv420p', '-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2'));

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

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