diff mbox series

[FFmpeg-devel] fftools/ffmpeg: fix -t inaccurate recording time

Message ID OS3PR01MB5573937CE7502E2F20977A45DD2F9@OS3PR01MB5573.jpnprd01.prod.outlook.com
State Accepted
Headers show
Series [FFmpeg-devel] fftools/ffmpeg: fix -t inaccurate recording time | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Shiwang.Xie May 15, 2021, 8:53 a.m. UTC
if input start time is not 0 -t is inaccurate doing stream copy,
will record extra duration according to input start time.
it should base on following cases:

input video start time from 60s, duration is 300s,
1. stream copy:
       ffmpeg -ss 40 -t 60 -i in.mp4 -c copy -y out.mp4
   open_input_file() will seek to 100 and set ts_offset to -100,
   process_input() will offset pkt->pts with ts_offset to make it 0,
   so when do_streamcopy() with -t, exits when ist->pts >= recording_time.

2. stream copy with -copyts:
       ffmpeg -ss 40 -t 60 -copyts -i in.mp4 -c copy -y out.mp4
   open_input_file() will seek to 100 and set ts_offset to 0,
   process_input() will keep raw pkt->pts as ts_offset is 0,
   so when do_streamcopy() with -t, exits when
   ist->pts >= (recording_time+f->start_time+f->ctx->start_time).

3. stream copy with -copyts -start_at_zero:
       ffmpeg -ss 40 -t 60 -copyts -start_at_zero -i in.mp4 -c copy -y out.mp4
   open_input_file() will seek to 120 and set ts_offset to -60 as start_to_zero option,
   process_input() will offset pkt->pts with input file start time,
   so when do_streamcopy() with -t, exits when ist->pts >= (recording_time+f->start_time).

0      60     40      60                     360
|_______|_____|_______|_______________________|
      start   -ss     -t

This fixes ticket #9141.

Signed-off-by: Shiwang.Xie <shiwang.xie666@outlook.com>
---
 fftools/ffmpeg.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

Shiwang.Xie May 19, 2021, 11:06 a.m. UTC | #1
Hi, is there objection?

Thanks,
Shiwang.Xie

On Sat, 15 May 2021, Shiwang.Xie wrote:

> if input start time is not 0 -t is inaccurate doing stream copy,
> will record extra duration according to input start time.
> it should base on following cases:
>
> input video start time from 60s, duration is 300s,
> 1. stream copy:
>       ffmpeg -ss 40 -t 60 -i in.mp4 -c copy -y out.mp4
>   open_input_file() will seek to 100 and set ts_offset to -100,
>   process_input() will offset pkt->pts with ts_offset to make it 0,
>   so when do_streamcopy() with -t, exits when ist->pts >= recording_time.
>
> 2. stream copy with -copyts:
>       ffmpeg -ss 40 -t 60 -copyts -i in.mp4 -c copy -y out.mp4
>   open_input_file() will seek to 100 and set ts_offset to 0,
>   process_input() will keep raw pkt->pts as ts_offset is 0,
>   so when do_streamcopy() with -t, exits when
>   ist->pts >= (recording_time+f->start_time+f->ctx->start_time).
>
> 3. stream copy with -copyts -start_at_zero:
>       ffmpeg -ss 40 -t 60 -copyts -start_at_zero -i in.mp4 -c copy -y out.mp4
>   open_input_file() will seek to 120 and set ts_offset to -60 as start_to_zero option,
>   process_input() will offset pkt->pts with input file start time,
>   so when do_streamcopy() with -t, exits when ist->pts >= (recording_time+f->start_time).
>
> 0      60     40      60                     360
> |_______|_____|_______|_______________________|
>      start   -ss     -t
>
> This fixes ticket #9141.
>
> Signed-off-by: Shiwang.Xie <shiwang.xie666@outlook.com>
> ---
> fftools/ffmpeg.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
> index b3658d8f65..309d9dfa6e 100644
> --- a/fftools/ffmpeg.c
> +++ b/fftools/ffmpeg.c
> @@ -2082,9 +2082,11 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
>     }
>
>     if (f->recording_time != INT64_MAX) {
> -        start_time = f->ctx->start_time;
> -        if (f->start_time != AV_NOPTS_VALUE && copy_ts)
> -            start_time += f->start_time;
> +        start_time = 0;
> +        if (copy_ts) {
> +            start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
> +            start_time += start_at_zero ? 0 : f->ctx->start_time;
> +        }
>         if (ist->pts >= f->recording_time + start_time) {
>             close_output_stream(ost);
>             return;
> -- 
> 2.18.5
>
>
Shiwang.Xie May 29, 2021, 6:50 a.m. UTC | #2
Hi, any updates for this?

Thanks,
Shiwang.Xie

On Wed, 19 May 2021, Shiwang.Xie wrote:

> Hi, is there objection?
>
> Thanks,
> Shiwang.Xie
>
> On Sat, 15 May 2021, Shiwang.Xie wrote:
>
>> if input start time is not 0 -t is inaccurate doing stream copy,
>> will record extra duration according to input start time.
>> it should base on following cases:
>> 
>> input video start time from 60s, duration is 300s,
>> 1. stream copy:
>>       ffmpeg -ss 40 -t 60 -i in.mp4 -c copy -y out.mp4
>>   open_input_file() will seek to 100 and set ts_offset to -100,
>>   process_input() will offset pkt->pts with ts_offset to make it 0,
>>   so when do_streamcopy() with -t, exits when ist->pts >= recording_time.
>> 
>> 2. stream copy with -copyts:
>>       ffmpeg -ss 40 -t 60 -copyts -i in.mp4 -c copy -y out.mp4
>>   open_input_file() will seek to 100 and set ts_offset to 0,
>>   process_input() will keep raw pkt->pts as ts_offset is 0,
>>   so when do_streamcopy() with -t, exits when
>>   ist->pts >= (recording_time+f->start_time+f->ctx->start_time).
>> 
>> 3. stream copy with -copyts -start_at_zero:
>>       ffmpeg -ss 40 -t 60 -copyts -start_at_zero -i in.mp4 -c copy -y 
>> out.mp4
>>   open_input_file() will seek to 120 and set ts_offset to -60 as 
>> start_to_zero option,
>>   process_input() will offset pkt->pts with input file start time,
>>   so when do_streamcopy() with -t, exits when ist->pts >= 
>> (recording_time+f->start_time).
>> 
>> 0      60     40      60                     360
>> |_______|_____|_______|_______________________|
>>      start   -ss     -t
>> 
>> This fixes ticket #9141.
>> 
>> Signed-off-by: Shiwang.Xie <shiwang.xie666@outlook.com>
>> ---
>> fftools/ffmpeg.c | 8 +++++---
>> 1 file changed, 5 insertions(+), 3 deletions(-)
>> 
>> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
>> index b3658d8f65..309d9dfa6e 100644
>> --- a/fftools/ffmpeg.c
>> +++ b/fftools/ffmpeg.c
>> @@ -2082,9 +2082,11 @@ static void do_streamcopy(InputStream *ist, 
>> OutputStream *ost, const AVPacket *p
>>     }
>>
>>     if (f->recording_time != INT64_MAX) {
>> -        start_time = f->ctx->start_time;
>> -        if (f->start_time != AV_NOPTS_VALUE && copy_ts)
>> -            start_time += f->start_time;
>> +        start_time = 0;
>> +        if (copy_ts) {
>> +            start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time 
>> : 0;
>> +            start_time += start_at_zero ? 0 : f->ctx->start_time;
>> +        }
>>         if (ist->pts >= f->recording_time + start_time) {
>>             close_output_stream(ost);
>>             return;
>> -- 
>> 2.18.5
>> 
>> 
>
Shiwang.Xie June 5, 2021, 11:18 a.m. UTC | #3
Ping.

Thanks,
Shiwang.Xie

On Sat, 29 May 2021, Shiwang.Xie wrote:

> Hi, any updates for this?
>
> Thanks,
> Shiwang.Xie
>
> On Wed, 19 May 2021, Shiwang.Xie wrote:
>
>> Hi, is there objection?
>> 
>> Thanks,
>> Shiwang.Xie
>> 
>> On Sat, 15 May 2021, Shiwang.Xie wrote:
>> 
>>> if input start time is not 0 -t is inaccurate doing stream copy,
>>> will record extra duration according to input start time.
>>> it should base on following cases:
>>> 
>>> input video start time from 60s, duration is 300s,
>>> 1. stream copy:
>>>       ffmpeg -ss 40 -t 60 -i in.mp4 -c copy -y out.mp4
>>>   open_input_file() will seek to 100 and set ts_offset to -100,
>>>   process_input() will offset pkt->pts with ts_offset to make it 0,
>>>   so when do_streamcopy() with -t, exits when ist->pts >= recording_time.
>>> 
>>> 2. stream copy with -copyts:
>>>       ffmpeg -ss 40 -t 60 -copyts -i in.mp4 -c copy -y out.mp4
>>>   open_input_file() will seek to 100 and set ts_offset to 0,
>>>   process_input() will keep raw pkt->pts as ts_offset is 0,
>>>   so when do_streamcopy() with -t, exits when
>>>   ist->pts >= (recording_time+f->start_time+f->ctx->start_time).
>>> 
>>> 3. stream copy with -copyts -start_at_zero:
>>>       ffmpeg -ss 40 -t 60 -copyts -start_at_zero -i in.mp4 -c copy -y 
>>> out.mp4
>>>   open_input_file() will seek to 120 and set ts_offset to -60 as 
>>> start_to_zero option,
>>>   process_input() will offset pkt->pts with input file start time,
>>>   so when do_streamcopy() with -t, exits when ist->pts >= 
>>> (recording_time+f->start_time).
>>> 
>>> 0      60     40      60                     360
>>> |_______|_____|_______|_______________________|
>>>      start   -ss     -t
>>> 
>>> This fixes ticket #9141.
>>> 
>>> Signed-off-by: Shiwang.Xie <shiwang.xie666@outlook.com>
>>> ---
>>> fftools/ffmpeg.c | 8 +++++---
>>> 1 file changed, 5 insertions(+), 3 deletions(-)
>>> 
>>> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
>>> index b3658d8f65..309d9dfa6e 100644
>>> --- a/fftools/ffmpeg.c
>>> +++ b/fftools/ffmpeg.c
>>> @@ -2082,9 +2082,11 @@ static void do_streamcopy(InputStream *ist, 
>>> OutputStream *ost, const AVPacket *p
>>>     }
>>>
>>>     if (f->recording_time != INT64_MAX) {
>>> -        start_time = f->ctx->start_time;
>>> -        if (f->start_time != AV_NOPTS_VALUE && copy_ts)
>>> -            start_time += f->start_time;
>>> +        start_time = 0;
>>> +        if (copy_ts) {
>>> +            start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time 
>>> : 0;
>>> +            start_time += start_at_zero ? 0 : f->ctx->start_time;
>>> +        }
>>>         if (ist->pts >= f->recording_time + start_time) {
>>>             close_output_stream(ost);
>>>             return;
>>> -- 
>>> 2.18.5
>>> 
>>> 
>> 
>
Shiwang.Xie July 9, 2021, 5:33 a.m. UTC | #4
Pings.

On Sat, 5 Jun 2021, Shiwang.Xie wrote:

> Ping.
>
> Thanks,
> Shiwang.Xie
>
> On Sat, 29 May 2021, Shiwang.Xie wrote:
>
>> Hi, any updates for this?
>> 
>> Thanks,
>> Shiwang.Xie
>> 
>> On Wed, 19 May 2021, Shiwang.Xie wrote:
>> 
>>> Hi, is there objection?
>>> 
>>> Thanks,
>>> Shiwang.Xie
>>> 
>>> On Sat, 15 May 2021, Shiwang.Xie wrote:
>>> 
>>>> if input start time is not 0 -t is inaccurate doing stream copy,
>>>> will record extra duration according to input start time.
>>>> it should base on following cases:
>>>> 
>>>> input video start time from 60s, duration is 300s,
>>>> 1. stream copy:
>>>>       ffmpeg -ss 40 -t 60 -i in.mp4 -c copy -y out.mp4
>>>>   open_input_file() will seek to 100 and set ts_offset to -100,
>>>>   process_input() will offset pkt->pts with ts_offset to make it 0,
>>>>   so when do_streamcopy() with -t, exits when ist->pts >= recording_time.
>>>> 
>>>> 2. stream copy with -copyts:
>>>>       ffmpeg -ss 40 -t 60 -copyts -i in.mp4 -c copy -y out.mp4
>>>>   open_input_file() will seek to 100 and set ts_offset to 0,
>>>>   process_input() will keep raw pkt->pts as ts_offset is 0,
>>>>   so when do_streamcopy() with -t, exits when
>>>>   ist->pts >= (recording_time+f->start_time+f->ctx->start_time).
>>>> 
>>>> 3. stream copy with -copyts -start_at_zero:
>>>>       ffmpeg -ss 40 -t 60 -copyts -start_at_zero -i in.mp4 -c copy -y 
>>>> out.mp4
>>>>   open_input_file() will seek to 120 and set ts_offset to -60 as 
>>>> start_to_zero option,
>>>>   process_input() will offset pkt->pts with input file start time,
>>>>   so when do_streamcopy() with -t, exits when ist->pts >= 
>>>> (recording_time+f->start_time).
>>>> 
>>>> 0      60     40      60                     360
>>>> |_______|_____|_______|_______________________|
>>>>      start   -ss     -t
>>>> 
>>>> This fixes ticket #9141.
>>>> 
>>>> Signed-off-by: Shiwang.Xie <shiwang.xie666@outlook.com>
>>>> ---
>>>> fftools/ffmpeg.c | 8 +++++---
>>>> 1 file changed, 5 insertions(+), 3 deletions(-)
>>>> 
>>>> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
>>>> index b3658d8f65..309d9dfa6e 100644
>>>> --- a/fftools/ffmpeg.c
>>>> +++ b/fftools/ffmpeg.c
>>>> @@ -2082,9 +2082,11 @@ static void do_streamcopy(InputStream *ist, 
>>>> OutputStream *ost, const AVPacket *p
>>>>     }
>>>>
>>>>     if (f->recording_time != INT64_MAX) {
>>>> -        start_time = f->ctx->start_time;
>>>> -        if (f->start_time != AV_NOPTS_VALUE && copy_ts)
>>>> -            start_time += f->start_time;
>>>> +        start_time = 0;
>>>> +        if (copy_ts) {
>>>> +            start_time += f->start_time != AV_NOPTS_VALUE ? 
>>>> f->start_time : 0;
>>>> +            start_time += start_at_zero ? 0 : f->ctx->start_time;
>>>> +        }
>>>>         if (ist->pts >= f->recording_time + start_time) {
>>>>             close_output_stream(ost);
>>>>             return;
>>>> -- 
>>>> 2.18.5
>>>> 
>>>> 
>>> 
>> 
>
Gyan Doshi July 17, 2021, 9:40 a.m. UTC | #5
On 2021-07-10 15:42, Gyan Doshi wrote:
>
>
> On 2021-07-09 11:03, Shiwang.Xie wrote:
>> Pings.
>
> Will test.

Fix confirmed. Will push tomorrow if no one objects.

Regards,
Gyan


>
>>
>> On Sat, 5 Jun 2021, Shiwang.Xie wrote:
>>
>>> Ping.
>>>
>>> Thanks,
>>> Shiwang.Xie
>>>
>>> On Sat, 29 May 2021, Shiwang.Xie wrote:
>>>
>>>> Hi, any updates for this?
>>>>
>>>> Thanks,
>>>> Shiwang.Xie
>>>>
>>>> On Wed, 19 May 2021, Shiwang.Xie wrote:
>>>>
>>>>> Hi, is there objection?
>>>>>
>>>>> Thanks,
>>>>> Shiwang.Xie
>>>>>
>>>>> On Sat, 15 May 2021, Shiwang.Xie wrote:
>>>>>
>>>>>> if input start time is not 0 -t is inaccurate doing stream copy,
>>>>>> will record extra duration according to input start time.
>>>>>> it should base on following cases:
>>>>>>
>>>>>> input video start time from 60s, duration is 300s,
>>>>>> 1. stream copy:
>>>>>>       ffmpeg -ss 40 -t 60 -i in.mp4 -c copy -y out.mp4
>>>>>>   open_input_file() will seek to 100 and set ts_offset to -100,
>>>>>>   process_input() will offset pkt->pts with ts_offset to make it 0,
>>>>>>   so when do_streamcopy() with -t, exits when ist->pts >= 
>>>>>> recording_time.
>>>>>>
>>>>>> 2. stream copy with -copyts:
>>>>>>       ffmpeg -ss 40 -t 60 -copyts -i in.mp4 -c copy -y out.mp4
>>>>>>   open_input_file() will seek to 100 and set ts_offset to 0,
>>>>>>   process_input() will keep raw pkt->pts as ts_offset is 0,
>>>>>>   so when do_streamcopy() with -t, exits when
>>>>>>   ist->pts >= (recording_time+f->start_time+f->ctx->start_time).
>>>>>>
>>>>>> 3. stream copy with -copyts -start_at_zero:
>>>>>>       ffmpeg -ss 40 -t 60 -copyts -start_at_zero -i in.mp4 -c 
>>>>>> copy -y out.mp4
>>>>>>   open_input_file() will seek to 120 and set ts_offset to -60 as 
>>>>>> start_to_zero option,
>>>>>>   process_input() will offset pkt->pts with input file start time,
>>>>>>   so when do_streamcopy() with -t, exits when ist->pts >= 
>>>>>> (recording_time+f->start_time).
>>>>>>
>>>>>> 0      60     40      60                     360
>>>>>> |_______|_____|_______|_______________________|
>>>>>>      start   -ss     -t
>>>>>>
>>>>>> This fixes ticket #9141.
>>>>>>
>>>>>> Signed-off-by: Shiwang.Xie <shiwang.xie666@outlook.com>
>>>>>> ---
>>>>>> fftools/ffmpeg.c | 8 +++++---
>>>>>> 1 file changed, 5 insertions(+), 3 deletions(-)
>>>>>>
>>>>>> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
>>>>>> index b3658d8f65..309d9dfa6e 100644
>>>>>> --- a/fftools/ffmpeg.c
>>>>>> +++ b/fftools/ffmpeg.c
>>>>>> @@ -2082,9 +2082,11 @@ static void do_streamcopy(InputStream 
>>>>>> *ist, OutputStream *ost, const AVPacket *p
>>>>>>     }
>>>>>>
>>>>>>     if (f->recording_time != INT64_MAX) {
>>>>>> -        start_time = f->ctx->start_time;
>>>>>> -        if (f->start_time != AV_NOPTS_VALUE && copy_ts)
>>>>>> -            start_time += f->start_time;
>>>>>> +        start_time = 0;
>>>>>> +        if (copy_ts) {
>>>>>> +            start_time += f->start_time != AV_NOPTS_VALUE ? 
>>>>>> f->start_time : 0;
>>>>>> +            start_time += start_at_zero ? 0 : f->ctx->start_time;
>>>>>> +        }
>>>>>>         if (ist->pts >= f->recording_time + start_time) {
>>>>>>             close_output_stream(ost);
>>>>>>             return;
>>>>>> -- 
>>>>>> 2.18.5
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
>> To unsubscribe, visit link above, or email
>> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Gyan Doshi July 18, 2021, 4:31 a.m. UTC | #6
On 2021-07-17 15:10, Gyan Doshi wrote:
>
>
> On 2021-07-10 15:42, Gyan Doshi wrote:
>>
>>
>> On 2021-07-09 11:03, Shiwang.Xie wrote:
>>> Pings.
>>
>> Will test.
>
> Fix confirmed. Will push tomorrow if no one objects.

Pushed as 694545b6d5fff5a8242b5fab8c1746e74a06f9ba

Gyan
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index b3658d8f65..309d9dfa6e 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2082,9 +2082,11 @@  static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
     }
 
     if (f->recording_time != INT64_MAX) {
-        start_time = f->ctx->start_time;
-        if (f->start_time != AV_NOPTS_VALUE && copy_ts)
-            start_time += f->start_time;
+        start_time = 0;
+        if (copy_ts) {
+            start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
+            start_time += start_at_zero ? 0 : f->ctx->start_time;
+        }
         if (ist->pts >= f->recording_time + start_time) {
             close_output_stream(ost);
             return;