Reputation: 31
I have a simple app that is trying to: 1) rotate videos that have a rotation value 2) re-encode HEVC videos so they play in Chrome (until Chrome supports HEVC) 3) stitch several of the results together.
I'm currently developing locally and in testing, I'm trying to scan the current directory for some example mp4's I have in there. It chokes on the very first one. Here is the code:
<?PHP
$files1 = scandir('./');
foreach ($files1 as $file) {
$fileinfo = new SplFileInfo($file);
$extn = $fileinfo->getExtension();
if ($extn == 'mp4'){
fixVideo($file);
}
}
// FUNCTIONS
function fixVideo($file) {
$config = array(
'ffmpeg.binaries' => '/usr/local/bin/ffmpeg',
'ffprobe.binaries' => '/usr/local/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
);
$resultFile = 'FIXED_'.$file;
// create the ffmpeg object
$ffmpeg = FFMpeg\FFMpeg::create($config, null);
// open video file
$video = $ffmpeg->open($file);
// get the first video stream
$videostream = $ffmpeg->getFFProbe()
->streams($file)
->videos()
->first();
//echo "<pre>";print_r($videostream);
if (!$videostream instanceof FFMpeg\FFProbe\DataMapping\Stream) {
throw new \Exception('No stream given');
} else {
echo "<video src='$file' width='100'></video><br>";
echo "$file<br>";
if ($videostream->has('tags')) {
//echo "has tags<BR>";
// MUST WE ROTATE?
$tags = $videostream->get('tags');
if (isset($tags['rotate'])) {
echo "has rotate" . $tags['rotate'] . "<BR>" ;
if ($tags['rotate'] != 0) {
echo "rotate not 0<BR>";
switch($tags['rotate']) {
case 270:
$angle = FFMpeg\Filters\Video\RotateFilter::ROTATE_270;
break;
case 180:
$angle = FFMpeg\Filters\Video\RotateFilter::ROTATE_180;
break;
case 90:
$angle = FFMpeg\Filters\Video\RotateFilter::ROTATE_90;
break;
}
$video->filters()
->rotate($angle); echo "rotating<br>";
} // if ($tags['rotate']
// MUST WE REENCODE TO H.264?
if (isset($tags['encoder'])) {
echo "encoding: " . $tags['encoder'];
}
$format = new FFMpeg\Format\Video\X264();
$format->setAudioCodec("aac");
$video->save($format,$resultFile );
} // if (isset($tags['rotate']
} // if ($videostream->has('tags')
echo "<BR><BR>";
} // if $videostream instanceof
}//function
?>
When I run that in the browser, I get:
appL.mp4
has rotate0
encoding: HEVC
Fatal error: Uncaught Alchemy\BinaryDriver\Exception\ExecutionFailureException: ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-y' '-i' 'appL.mp4' '-threads' '12' '-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' '/var/tmp/ffmpeg-passes5ba267bb96d0abvdgk/pass-5ba267bb96e01' 'FIXED_appL.mp4' in /Users/[redacted]/test3/vendor/alchemy/binary-driver/src/Alchemy/BinaryDriver/ProcessRunner.php:100 Stack trace: #0 /Users/[redacted]/test3/vendor/alchemy/binary-driver/src/Alchemy/BinaryDriver/ProcessRunner.php(72): Alchemy\BinaryDriver\ProcessRunner->doExecutionFailure(''/usr/local/bin...') #1 /Users/[redacted]/test3/vendor/alchemy/binary-driver/src/Alchemy/BinaryDriver/AbstractBinary.php(209): Alchemy\Bi in /Users/[redacted]/test3/vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/Media/AbstractVideo.php on line 106
When I run it in the Terminal, I get:
MacBook-Pro:videos [redacted]$ '/usr/local/bin/ffmpeg' '-y' '-i' 'appL.mp4' '-threads' '12' '-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' '/var/tmp/ffmpeg-passes5ba267bb96d0abvdgk/pass-5ba267bb96e01' 'FIXED_appL.mp4' ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers built with Apple LLVM version 9.1.0 (clang-902.0.39.2) configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags=-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ --host-ldflags= --enable-gpl --enable-chromaprint --enable-ffplay --enable-frei0r --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopus --enable-librsvg --enable-librtmp --enable-librubberband --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtesseract --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --enable-libzmq --enable-opencl --enable-videotoolbox --enable-openssl --enable-libsrt --enable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.3.0/include/openjpeg-2.3 --enable-nonfree libavutil 56. 14.100 / 56. 14.100 libavcodec 58. 18.100 / 58. 18.100 libavformat 58. 12.100 / 58. 12.100 libavdevice 58. 3.100 / 58. 3.100 libavfilter 7. 16.100 / 7. 16.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 1.100 / 5. 1.100 libswresample 3. 1.100 / 3. 1.100 libpostproc 55. 1.100 / 55. 1.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'appL.mp4': Metadata: major_brand : qt
minor_version : 0 compatible_brands: qt
creation_time : 2018-09-17T16:01:54.000000Z Duration: 00:00:03.30, start: 0.000000, bitrate: 4574 kb/s Stream #0:0(und): Video: hevc (Main) (hvc1 / 0x31637668), yuv420p(tv, bt709), 960x540, 4459 kb/s, 29.97 fps, 29.97 tbr, 600 tbn, 600 tbc (default) Metadata: rotate : 0 creation_time : 2018-09-17T16:01:54.000000Z handler_name : Core Media Data Handler encoder : HEVC Side data: displaymatrix: rotation of -0.00 degrees Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 98 kb/s (default) Metadata: creation_time : 2018-09-17T16:01:54.000000Z handler_name : Core Media Data Handler Stream mapping: Stream #0:0 -> #0:0 (hevc (native) -> h264 (libx264)) Stream #0:1 -> #0:1 (aac (native) -> mp3 (libmp3lame)) Press [q] to stop, [?] for help [libx264 @ 0x7f89e0018600] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 0x7f89e0018600] ratecontrol_init: can't open stats file 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 [libmp3lame @ 0x7f89e0019e00] 4 frames left in the queue on closing Conversion failed!
Upvotes: 2
Views: 720
Reputation: 821
Try this.. Change:
$format = new FFMpeg\Format\Video\X264();
To:
$format = new FFMpeg\Format\Video\X264('libmp3lame');
Upvotes: 0