Kevin
Kevin

Reputation: 13

How to change the -ss behavior from closest seek point before position to closest seek point after position

Is there any way to change the -ss behavior from closest seek point before position to closest seek point after position?

Example:

ffmpeg has the capability to select the seek point from 98 secs or 102 secs with in.mp4

If I use the following command, the ffmpeg will select the 98 secs as the seek point.

ffmpeg -ss 100 -i in.mp4 -c copy out.mp4

Is there any way to add some option but keep the -ss 100 to make the ffmpeg to select the 102 as the seek point?

Upvotes: 0

Views: 277

Answers (1)

Kevin
Kevin

Reputation: 13

Finally, I used the shell script with ffprobe to achieve my goal.

Because of that ffmpeg -ss T will select the nearest keyframe before time T, so that I need to use ffprobe to get the next keyframe after time T.

The ffprobe reference command:

ffprobe -skip_frame nokey -select_streams v -show_frames -show_entries frame=pkt_pts_time -of csv -read_intervals T+30

The above command will show the keyframe timestamp from start to T+30. So that I just need use some string processing with bash script to get the smallest timestamp which is greater than time T. And don't forget to add 1 to next keyframe when use it as the -ss parameter with ffmpeg

My detailed bash script:

option="-skip_frame nokey -select_streams v -show_frames -show_entries frame=pkt_pts_time -of csv -read_intervals"
tg_file="test.mp4"
st_time=$1
probe_time=`expr $st_time + 20`
kf_arr=(`ffprobe ${option} %${probe_time} ${tg_file} | sed 's/.\+,//g'`)

for i in ${kf_arr[@]}
do
    if (( $(echo "$i > ${st_time}" | tr -d $'\r' | bc -l) )); then
        kf=${i%.*}
        st=`expr $kf + 1`
        break
    fi
done

$1: Expected start time

tr -d $'\r': Remove the newline symbol in Windows system. (if you use the Linux you can ignore it)

$st: New timestamp (Next keyframe timestamp + 1 after expected start time)

If anyone has more convenient method to do that please kindly let me know, thanks!

Upvotes: 1

Related Questions