Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Marton Balint <cus@passwd.hu>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: Re: [FFmpeg-devel] [PATCH 01/10] avformat/segment: Add segment_write_temp option
Date: Sat, 14 Jun 2025 00:07:17 +0200 (CEST)
Message-ID: <34253e5e-c1c1-75bc-1a0a-8b97da8361b9@passwd.hu> (raw)
In-Reply-To: <DM8P223MB0365D720F5325B4766941619BA77A@DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM>



On Fri, 13 Jun 2025, softworkz . wrote:

> Hi Marton,
>
>
>> -----Original Message-----
>> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
>> Marton Balint
>> Sent: Freitag, 13. Juni 2025 22:38
>> To: FFmpeg development discussions and patches <ffmpeg-
>> devel@ffmpeg.org>
>> Subject: Re: [FFmpeg-devel] [PATCH 01/10] avformat/segment: Add
>> segment_write_temp option
>>
>>
>>
>> On Fri, 13 Jun 2025, softworkz wrote:
>>
>>> From: softworkz <softworkz@hotmail.com>
>>>
>>> Allows to write segments as temp files (.tmp) which
>>> are renamed on completion.
>>>
>>> Signed-off-by: softworkz <softworkz@hotmail.com>
>>> ---
>>> libavformat/segment.c | 30 +++++++++++++++++++++++++++---
>>> 1 file changed, 27 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/libavformat/segment.c b/libavformat/segment.c
>>> index 65323ec678..04e973a198 100644
>>> --- a/libavformat/segment.c
>>> +++ b/libavformat/segment.c
>>> @@ -121,6 +121,7 @@ typedef struct SegmentContext {
>>>     int   break_non_keyframes;
>>>     int   write_empty;
>>>
>>> +    int segment_write_temp; ///< write segments as temp files
>> and rename on completion
>>>     int use_rename;
>>>     char temp_list_filename[1024];
>>>
>>> @@ -226,6 +227,15 @@ static int
>> set_segment_filename(AVFormatContext *s)
>>>              seg->entry_prefix ? seg->entry_prefix : "",
>>>              av_basename(oc->url));
>>>
>>> +    // Write segment as a temp file and rename on completion
>>> +    if(seg->segment_write_temp) {
>>> +        av_strlcatf(buf, sizeof(buf), ".tmp");
>>> +        char *temp_name = av_strdup(buf);
>>> +        if (!temp_name)
>>> +            return AVERROR(ENOMEM);
>>
>> You should use av_asprintf() directly instead of strlcatf() +
>> av_strdup()
>
> I could be wrong, but I was thinking that this way is more efficient,
> because in the av_strdup case, the size of the needed allocation is
> already known, while I would suppose that av_asprintf() needs to
> allocate a larger size of memory because it cannot predict the eventual
> size after applying all the formats. Am I on a wrong track?

I think it does not matter, this is not performance critical code, so 
simplicity/readability is more imporant. Also we should avoid hard coded 
path length limits if we can, an by using a static buffer you can hit 
that.


>
>
>>> +        ff_format_set_url(oc, temp_name);
>>> +    }
>>> +
>>>     return 0;
>>> }
>>>
>>> @@ -372,7 +382,7 @@ static int segment_end(AVFormatContext *s,
>> int write_trailer, int is_last)
>>>             SegmentListEntry *entry = av_mallocz(sizeof(*entry));
>>>             if (!entry) {
>>>                 ret = AVERROR(ENOMEM);
>>> -                goto end;
>>> +                goto fail;
>>>             }
>>>
>>>             /* append new element */
>>> @@ -393,7 +403,7 @@ static int segment_end(AVFormatContext *s,
>> int write_trailer, int is_last)
>>>             }
>>>
>>>             if ((ret = segment_list_open(s)) < 0)
>>> -                goto end;
>>> +                goto fail;
>>>             for (entry = seg->segment_list_entries; entry; entry
>> = entry->next)
>>>                 segment_list_print_entry(seg->list_pb, seg-
>>> list_type, entry, s);
>>>             if (seg->list_type == LIST_TYPE_M3U8 && is_last)
>>> @@ -450,7 +460,20 @@ static int segment_end(AVFormatContext *s,
>> int write_trailer, int is_last)
>>>         }
>>>     }
>>>
>>> -end:
>>> +    ff_format_io_close(oc, &oc->pb);
>>> +
>>> +    // Now rename the .tmp file to its actual name.
>>> +    if (seg->segment_write_temp) {
>>> +        char *final_filename = av_strndup(oc->url, strlen(oc-
>>> url) - 4);
>>> +        if (!final_filename)
>>> +            return AVERROR(ENOMEM);
>>
>> goto fail?
>
> At that point,  ff_format_io_close(oc, &oc->pb) has
> already been called.

Ah, I missed that, OK then.

Thanks,
Marton

>
> Here's the full block (after the patch):
>
>
>    ff_format_io_close(oc, &oc->pb);
>
>    // Now rename the .tmp file to its actual name.
>    if (seg->segment_write_temp) {
>        char *final_filename = av_strndup(oc->url, strlen(oc->url) - 4);
>        if (!final_filename)
>            return AVERROR(ENOMEM);
>        ret = ff_rename(oc->url, final_filename, s);
>        av_free(final_filename);
>    }
>
>    return ret;
>
> fail:
>    ff_format_io_close(oc, &oc->pb);
>
>    return ret;
> }
>
>
> Thanks,
> sw
>
> _______________________________________________
> 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".

  reply	other threads:[~2025-06-13 22:10 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-13  5:54 [FFmpeg-devel] [PATCH 00/10] avformat/segment: Various segment muxer improvements ffmpegagent
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 01/10] avformat/segment: Add segment_write_temp option softworkz
2025-06-13 20:37   ` Marton Balint
2025-06-13 21:43     ` softworkz .
2025-06-13 22:07       ` Marton Balint [this message]
2025-06-13 22:26         ` softworkz .
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 02/10] doc/muxers: Add documentation for " softworkz
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 03/10] avformat/segment: Log more detailed information about written segments softworkz
2025-06-13 13:42   ` Derek Buitenhuis
2025-06-13 14:03     ` softworkz .
2025-06-13 20:44   ` Marton Balint
2025-06-13 23:53     ` softworkz .
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 04/10] avformat/segment: Add segment_limit option softworkz
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 05/10] doc/muxers: Add documentation for " softworkz
2025-06-13 20:25   ` Marton Balint
2025-06-13 21:33     ` softworkz .
2025-06-13 21:59       ` Marton Balint
2025-06-13 22:49         ` softworkz .
2025-06-14 15:59           ` Marton Balint
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 06/10] avformat/segment: Fix invalid codecpar extradata_size after copying softworkz
2025-06-13  6:03   ` softworkz .
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 07/10] avformat/segment: Remove non-negative constraint from segment_time_delta softworkz
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 08/10] avformat/segment: Don't allow negative segment duration softworkz
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 09/10] avformat/segment: Fix typo softworkz
2025-06-13  5:54 ` [FFmpeg-devel] [PATCH 10/10] avformat/segment: Indent and whitespace fixes softworkz
2025-06-14  0:59 ` [FFmpeg-devel] [PATCH v2 0/6] avformat/segment: Various segment muxer improvements ffmpegagent
2025-06-14  0:59   ` [FFmpeg-devel] [PATCH v2 1/6] avformat/segment: Add segment_write_temp option softworkz
2025-06-14  0:59   ` [FFmpeg-devel] [PATCH v2 2/6] avformat/segment: Add segment_limit option softworkz
2025-06-14  0:59   ` [FFmpeg-devel] [PATCH v2 3/6] avformat/segment: Remove non-negative constraint from segment_time_delta softworkz
2025-06-14  0:59   ` [FFmpeg-devel] [PATCH v2 4/6] avformat/segment: Don't allow negative segment duration softworkz
2025-06-14 20:40     ` Michael Niedermayer
2025-06-14  0:59   ` [FFmpeg-devel] [PATCH v2 5/6] avformat/segment: Fix typo softworkz
2025-06-14  0:59   ` [FFmpeg-devel] [PATCH v2 6/6] avformat/segment: Indent and whitespace fixes softworkz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=34253e5e-c1c1-75bc-1a0a-8b97da8361b9@passwd.hu \
    --to=cus@passwd.hu \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git