0

I am new to ffmpeg.

I want to get a video duration through ffmpeg and declare it to a variable. where I can use the value for a different function. I am using power shell.

This is my code:

ffmpeg -i 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4 2>&1 | sed -n 's/Duration: \(.*\), start/\1/gp'
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

Provided you have set up the path for ffmpeg in the Windows PATH variable like explained here, this should get you the duration value for the file:

$filename = '1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4'
$duration = if ((ffmpeg -i $filename 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] }

Mind you, after this $duration is a string.
If you need to create a TimeSpan object of this, use:

$t = [TimeSpan]::Parse($duration)
Theo
  • 57,719
  • 8
  • 24
  • 41
0

Don't use ffmpeg to get the duration: it's output is not meant to be machine parsed and it's messy.

Use ffprobe:

$duration = ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp4
  • This will give the duration in seconds. If you want HH:MM:SS format add the -sexagesimal option.

  • No need for any additional processing with sed, regex, etc, as ffprobe will directly give you the duration and only the duration.

  • More info at FFmpeg Wiki: FFprobe Tips and ffprobe documentation.

llogan
  • 121,796
  • 28
  • 232
  • 243