User Tools

Site Tools


ffmpeg:video:duration

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
ffmpeg:video:duration [2025/05/27 18:08] – created peterffmpeg:video:duration [2025/05/27 18:34] (current) – [By Packet duration_time] peter
Line 1: Line 1:
 ====== ffmpeg - Video - Duration ====== ====== ffmpeg - Video - Duration ======
 +
 +===== Using Regex =====
  
 <code bash> <code bash>
Line 20: Line 22:
  
 <WRAP info> <WRAP info>
-**NOTE:**  This returns an estimate of the duration based on the bitrate and size of the file.+**NOTE:**  This gets the figures directly from ffmpeg. 
 + 
 +  * This returns an estimate of the duration based on the bitrate and size of the file
 + 
 +</WRAP> 
 + 
 +---- 
 + 
 +===== Getting the data from the video stream ===== 
 + 
 +<code bash> 
 +ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "input.mkv" 
 +</code> 
 + 
 +<WRAP info> 
 +**NOTE:**  This often returns N/A for .mkv files. 
 +</WRAP> 
 + 
 +---- 
 + 
 +===== Getting the data from the container ===== 
 + 
 +<code bash> 
 +ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "input.mkv" 
 + 
 +ffprobe -v error -select_streams v:0 -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "input.mkv" 
 +</code> 
 + 
 +<WRAP important> 
 +**WARNING:**  This returns the duration of the entire container, and not just the duration of the video. 
 +</WRAP> 
 + 
 +---- 
 + 
 +===== By Packet duration_time ===== 
 + 
 +This method reads each video packet, individually, and totals up each packet size. 
 + 
 +  * Sums up the **duration_time** of the **-show_entries** command. 
 + 
 +<code bash> 
 +video_pkt_durations=$(ffprobe -v error -select_streams v:0 -skip_frame nokey -show_entries packet=duration_time -of default=nokey=1:noprint_wrappers=1 "input.mkv"
 + 
 +sum_video_pkt_duration=0 
 +precision=6 
 +count_video_pkt_duration=0 
 + 
 +#echo "Calculating video pkt_duration..." 
 +for video_pkt_duration in $video_pkt_durations; do 
 +  sum_video_pkt_duration=$(echo "${sum_video_pkt_duration} + ${video_pkt_duration}" | bc) 
 + 
 +  (( count_video_pkt_duration++ )) 
 +done; 
 + 
 +video_duration=$( echo "scale=${precision}; ${sum_video_pkt_duration} " | bc) 
 + 
 +echo "DURATION=${video_duration}" 
 +echo "VIDEO FRAMES=${count_video_pkt_duration}" 
 +</code> 
 + 
 +<WRAP info> 
 +**NOTE:**  This is very accurate but may take a while. 
 +</WRAP> 
 + 
 +---- 
 + 
 +===== Calculate duration using Size and Bitrate ===== 
 + 
 +<code bash> 
 +duration=$( echo "scale=6; (${video_size} * 8) / ${bitrate}" | bc) 
 +</code> 
 + 
 +---- 
 + 
 +===== Calculate duration using Size and Bitrate ===== 
 + 
 +<code bash> 
 +duration = NUMBER_OF_FRAMES / FPS 
 +</code> 
 + 
 +<WRAP info> 
 +**NOTE:**  Returns the duration in seconds.
 </WRAP> </WRAP>
  
ffmpeg/video/duration.1748369288.txt.gz · Last modified: 2025/05/27 18:08 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki