Understanding the behavior of "-ss/-t" option

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding the behavior of "-ss/-t" option

Nitish Prabhu
Hi,

Does FFmpeg apply the option "-ss/-t" based on container time stamp
information, or is it based on bit stream timing information?

I generated a stream with a 60 second start offset using the following command:
$ ffmpeg -itsoffset 60 -i input_clip_x264.mp4 -c copy input_clip_x264_offset.mp4

I tried to seek to a particular time in the generated stream using the
following:
$ ffmpeg -ss 60 -t 10 -i input_clip_x264_offset.mp4 -copyts -filter:v
"select=between(t\,60.000\,70.000)" -c:v libx264 offset_extract.mp4

I see that no frames are extracted in the above command. Using
ffprobe, I checked that the frame timestamps start from 60 in the clip
with offset (input_clip_x264_offset.mp4). So, in theory, this had to
work. If I run the above command with "-ss 0", I see that it is able
to extract frames.

Thus, I wanted to understand how seeking works under the hood in FFmpeg.

Thanks!

Best Regards,
Nitish
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Gyan
On Fri, Sep 8, 2017 at 10:38 AM, Nitish Prabhu <[hidden email]>
wrote:

>
> Does FFmpeg apply the option "-ss/-t" based on container time stamp
> information, or is it based on bit stream timing information?
>

Thus, I wanted to understand how seeking works under the hood in FFmpeg.
>

You'll want to use the seek_timestamp option.

As it says in the docs,

"-seek_timestamp (*input*)

This option enables or disables seeking by timestamp in input files with
the -ss option. It is disabled by default. If enabled, the argument to the
-ss option is considered an actual timestamp, and is not offset by the
start time of the file. This matters only for files which do not start from
timestamp 0, such as transport streams."
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Nitish Prabhu
On Fri, Sep 8, 2017 at 11:13 AM, Gyan <[hidden email]> wrote:
> wrote:

> You'll want to use the seek_timestamp option.
>
> As it says in the docs,
>
> "-seek_timestamp (*input*)
>

I tried using "-seek_timestamp" prior to posting this, but was not
really sure if this works on MP4 files. I used ffprobe to get the
first pts of the offset stream and used this with "-ss" option.

Following is the output of ffprobe for the first frame of
"input_clip_x264_offset.mp4", the clip where offset was introduced:
[FRAME]
media_type=video
key_frame=1
pkt_pts=921923
pkt_pts_time=60.021029
pkt_dts=921923
pkt_dts_time=60.021029
best_effort_timestamp=921923
best_effort_timestamp_time=60.021029
pkt_duration=512
pkt_duration_time=0.033333
pkt_pos=48
pkt_size=123379
width=1920
height=1080
pix_fmt=yuv420p
sample_aspect_ratio=1:1
pict_type=I
coded_picture_number=0
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
[/FRAME]

Following is the command that I used to seek with timestamp value:
$ ffmpeg -seek_timestamp 1 -ss 60.021029 -t 10 -i
input_clip_x264_offset.mp4 -copyts -filter:v
"select=between(t\,60.000\,70.000)" -c:v libx264 offset_extract.mp4

I do not see any change in the behavior with the above command. It
does not seek to the required timestamp.
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Nicolas George
In reply to this post by Nitish Prabhu
Le duodi 22 fructidor, an CCXXV, Nitish Prabhu a écrit :
> I tried to seek to a particular time in the generated stream using the
> following:
> $ ffmpeg -ss 60 -t 10 -i input_clip_x264_offset.mp4 -copyts -filter:v

"take seconds 60-70 of input_clip_x264_offset as input to produce
seconds 0-10 of the resulting stream"

> "select=between(t\,60.000\,70.000)"

"only keep frames between seconds 60 and 70"

Since [0;10[ \cup [60;70[ = \emptyset, the result you observe is
expected.

Regards,

--
  Nicolas George

_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Nitish Prabhu
On Fri, Sep 8, 2017 at 1:20 PM, Nicolas George <[hidden email]> wrote:
>> I tried to seek to a particular time in the generated stream using the
>> following:
>> $ ffmpeg -ss 60 -t 10 -i input_clip_x264_offset.mp4 -copyts -filter:v
>
> "take seconds 60-70 of input_clip_x264_offset as input to produce
> seconds 0-10 of the resulting stream"
>

I am using "-copyts" to maintain the input timestamps at the output
side. Thus, I believed that using "-ss 60 -t 10" with "-copyts" was
supposed to mean "take seconds 60-70 of input_clip_x264_offset as
input and produce 60-70 of the resultant stream".

>> "select=between(t\,60.000\,70.000)"
>
> "only keep frames between seconds 60 and 70"
>
> Since [0;10[ \cup [60;70[ = \emptyset, the result you observe is
> expected.

If "-ss 60 -t 10" is able to produce output in the rage [60;70[ at the
output side, then the select filter will appropriately select only
those timestamps.

Please let me know if my understanding is right.
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Gyan
Use

   ffmpeg  -i input_clip_x264_offset.mp4 -copyts -ss 60 -t 10 -filter:v
"select=between(t\,60.000\,70.000)" -c:v libx264 offset_extract.mp4

Looks like -seek_timestamp 1 gets inert when used in conjunction with
copyts.
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Nicolas George
In reply to this post by Nitish Prabhu
Le duodi 22 fructidor, an CCXXV, Nitish Prabhu a écrit :
> I am using "-copyts" to maintain the input timestamps at the output
> side. Thus, I believed that using "-ss 60 -t 10" with "-copyts" was
> supposed to mean "take seconds 60-70 of input_clip_x264_offset as
> input and produce 60-70 of the resultant stream".

My bad, I missed that and the explanation was not correct.

After further testing, it seems the -ss option is itself relative to the
file's start time: by setting -ss 60 with a file that starts at 60, you
are asking to seek to timestamp 120. And with -copyts, you get that
timestamp.

Therefore, the problem is that [120;130[ \cup [60;70[ = \emptyset, not
[0;10[ as I said before.

I could ascertain that by using the showinfo filter: it prints:

[Parsed_showinfo_0 @ 0x55c72f26e880] n:   0 pts:2304000 pts_time:120

Regards,

--
  Nicolas George

_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Gyan
On Fri, Sep 8, 2017 at 2:53 PM, Nicolas George <[hidden email]> wrote:


> After further testing, it seems the -ss option is itself relative to the
> file's start time: by setting -ss 60 with a file that starts at 60, you
> are asking to seek to timestamp 120. And with -copyts, you get that
> timestamp.
>

Except with seek_timestamp set to 1, the first packet you 'expect' to get
is pts_time 60. And it does work that way if copyts is *absent*.
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Nicolas George
Le duodi 22 fructidor, an CCXXV, Gyan a écrit :
> Except with seek_timestamp set to 1, the first packet you 'expect' to get
> is pts_time 60.

The purpose of my message was to EXPLAIN what happens, not to provide an
ad-hoc solution to the showcase.

> And it does work that way if copyts is *absent*.

No, it does not work that way without copyts: without copyts, the
timestamps always start at 0.

Regards,

--
  Nicolas George

_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Gyan
On Fri, Sep 8, 2017 at 3:40 PM, Nicolas George <[hidden email]> wrote:


> No, it does not work that way without copyts: without copyts, the
> timestamps always start at 0.
>

There are two issues: a) how to interpret input frame seek point, and b)
what happens to timestamps of selected frames.

Illustration
---------------

Generate source

    ffmpeg -f lavfi -i testsrc2 -t 2 -output_ts_offset 1 src.mp4

As per ffprobe, this gives a file with

    Duration: 00:00:02.00, start: 1.000000, bitrate: 277 kb/s


Running

        ffmpeg -seek_timestamp 1 -ss 2 -i src.mp4 ss-seekts2.mp4

produces, as per ffprobe,

      Duration: 00:00:01.00, start: 0.000000, bitrate: 276 kb/s

The frames in output were taken from TS 2 to 3 of src.mp4, testsrc2 frame
counter in first frame says '25'. TS in output is reset to 0 start. As
expected.


Running

        ffmpeg -seek_timestamp 1 -copyts -ss 2 -i src.mp4
copyts-seekts-ss2.mp4

produces an empty file. It's the same command as above, but with copyts
added.


Running

        ffmpeg -seek_timestamp 1 -copyts -ss 1 -i src.mp4
copyts-seekts-ss1.mp4

produces a file, reported as,

  Duration: 00:00:01.00, start: 2.000000, bitrate: 276 kb/s

whose video contents correspond to TS 2 to 3 of src.mp4, (frame counter
25).  'ss' acts as relative offset, and is apparently immune to
seek_timestamp. Source timestamps are copied over to output.
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Understanding the behavior of "-ss/-t" option

Nitish Prabhu
On Fri, Sep 8, 2017 at 5:34 PM, Gyan <[hidden email]> wrote:

> Running
>
>         ffmpeg -seek_timestamp 1 -copyts -ss 1 -i src.mp4
> copyts-seekts-ss1.mp4
>
> produces a file, reported as,
>
>   Duration: 00:00:01.00, start: 2.000000, bitrate: 276 kb/s
>
> whose video contents correspond to TS 2 to 3 of src.mp4, (frame counter
> 25).  'ss' acts as relative offset, and is apparently immune to
> seek_timestamp. Source timestamps are copied over to output.

On Fri, Sep 8, 2017 at 2:53 PM, Nicolas George <[hidden email]> wrote:
> After further testing, it seems the -ss option is itself relative to the
> file's start time: by setting -ss 60 with a file that starts at 60, you
> are asking to seek to timestamp 120. And with -copyts, you get that
> timestamp.
>
> Therefore, the problem is that [120;130[ \cup [60;70[ = \emptyset, not
> [0;10[ as I said before.
>

Gyan, Nicholas,

Thanks for sharing your observations an insights. I am now able to get
a better understanding of ffmpeg's behavior for "-ss/-t".

To summarize the observations, "-ss/-t" does not operate on timestamps
directly but operates on the relative offset from the start of the
file (even when "-seek_timestamp" is enabled).

From the best of my understanding, when "-seek_timestamp" is enabled,
as the documentation says "-ss" should operate on timestamps rather
than relative offsets. So, this may be a bug, or we might have to
change the documentation with a relevant correction (probably stating
in which case "-seek_timestamp" works).

Thanks!
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".