0

I'm using the PHP-FFMpeg library found here and code from the "Basic Usage" section.

The outputted video seems to be truncated. I'm using a source video that's 28 seconds long, but the output is only 9 seconds. What's going wrong?

Here, I'm checking the duration of the source video:

$ffprobe = FFMpeg\FFProbe::create();
$duration = $ffprobe
  ->format('test/source.mp4')
  ->get('duration');

28.700000

Then generating an output video:

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('test/source.mp4');
$video->save(new FFMpeg\Format\Video\X264(), 'test/export.mp4');

Then checking the duration of the output video:

$ffprobe = FFMpeg\FFProbe::create();
$duration = $ffprobe
  ->format('test/export.mp4')
  ->get('duration');

9.234000

showdev
  • 28,454
  • 37
  • 55
  • 73

1 Answers1

2

Thanks to slhck for the suggestion to investigate the conversion log.
It seems that my source file was corrupt.
I documented my process below.


I'm not aware of a way to get the ffmpeg output from the PHP-FFMpeg library.
So I used getFinalCommand() to output the ffmpeg command:

$video = $ffmpeg->open('test/source.mp4');
echo $video->getFinalCommand(new FFMpeg\Format\Video\X264(), 'test/export.mp4')[0];

-y -i test/source.mp4 -vcodec libx264 -acodec libfaac -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-passes5d108f0d5007cirj2x/pass-5d108f0d500f9 test/export.mp4

I executed the command got this error:

[libx264 @ 0x2395ec0] 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

The passlogfile seemed to be causing problems, so I removed it and got:

corrupt input packet in stream 0:04.76 bitrate= 880.0kbits/s speed=3.15x
[h264 @ 0x7b4500] Invalid NAL unit size (51618 > 33287).
[h264 @ 0x7b4500] Error splitting the input into NAL units.
Error while decoding stream #0:0: Invalid data found when processing input

It seems that my source video file was corrupt.
I reuploaded it and the error was gone.


In the process, I also found that "audio codec libfaac has been removed from ffmpeg" (iki789), that "there are better alternatives" (llogan), and that the audio codec can be set to "aac" like this:

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

$video = $ffmpeg->open('test/source.mp4');
$video->save($format, 'test/export.mp4');
showdev
  • 28,454
  • 37
  • 55
  • 73
  • Is PHP-FFMpeg (not sure why they decided to misspell FFmpeg) adding all of those unnecessary x264 encoding options? You can remove `-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` because the [x264 presets](https://trac.ffmpeg.org/wiki/Encode/H.264#Preset) deal with all of those. – llogan Jun 24 '19 at 18:57
  • @llogan Yes, I assume so. I'm not very familiar with the library's code, but [this section seems relevant](https://github.com/PHP-FFMpeg/PHP-FFMpeg/blob/master/src/FFMpeg/Media/AbstractVideo.php#L160). – showdev Jun 25 '19 at 00:02