[FFmpeg-user] How to get video duration in a script using ffmpeg?

Reindl Harald h.reindl at thelounge.net
Wed Jan 19 14:09:30 EET 2022



Am 19.01.22 um 12:59 schrieb Bo Berglund:
> On Wed, 19 Jan 2022 17:10:26 +0530, Gyan Doshi <ffmpeg at gyani.pro> wrote:
> 
>>> What do I need to just get that output I want?
>>
>> The closest you can get is
>>
>>       ffprobe -v 0 -of compact=p=0:nk=1 -sexagesimal -show_entries
>> format=duration INPUT
> 
> Thanks, this returns the following output:
> 0:43:26.384000
> 
> Can the duration be limited to only seconds resolution (no decimals)?

function music_get_length(string $path): int
{
  ob_start();
  passthru("/usr/bin/ffmpeg -hide_banner -i " . escapeshellarg($path) . 
' 2>&1');
  $ffmpeg_buffer = ob_get_clean();
  $duration = '';
  $search='/Duration: (.*?),/';
  $ffmpeg_buffer = preg_match($search, $ffmpeg_buffer, $matches, 
PREG_OFFSET_CAPTURE, 3);
  if(isset($matches[1][0]))
  {
   $duration_str = $matches[1][0];
   if($duration_str === 'N/A')
   {
    return 0;
   }
   list($duration_hours, $duration_mins, $duration_secs) = explode(':', 
$duration_str);
   $duration = sprintf('%02d', (int)$duration_hours) . ':' . 
sprintf('%02d', (int)$duration_mins) . ':' . sprintf('%02d', 
round($duration_secs));
   if($duration === '00:00:00')
   {
    return 0;
   }
   return (int)(round($duration_secs)) + ((int)$duration_mins * 60) + 
((int)$duration_hours*3600);
  }
  return 0;
}


More information about the ffmpeg-user mailing list