19

Given an input video, input.mp4, the following command produces an out folder containing a bunch of HLS video segments and m3u8 playlist files for a number of resolutions:

ffmpeg -hide_banner -y -i input.mp4 \
  -vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod  -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_segment_filename out/360p_%03d.ts out/360p.m3u8 \
  -vf scale=w=842:h=480:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 1400k -maxrate 1498k -bufsize 2100k -b:a 128k -hls_segment_filename out/480p_%03d.ts out/480p.m3u8 \
  -vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_segment_filename out/720p_%03d.ts out/720p.m3u8 \
  -vf scale=w=1920:h=1080:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_segment_filename out/1080p_%03d.ts out/1080p.m3u8

When running said command, the output looks like the following:

[hls @ 0x12be06030] Opening 'out/360p_000.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_000.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_000.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_000.ts' for writing
[hls @ 0x12be06030] Opening 'out/360p_001.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_001.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_001.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_001.ts' for writing
[hls @ 0x12be06030] Opening 'out/360p_002.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_002.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_002.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_002.ts' for writing
[hls @ 0x12be06030] Opening 'out/360p_003.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_003.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_003.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_003.ts' for writing
etc

As you can see, the output of ffmpeg tells me what files it is opening for writing. Is there any way for ffmpeg to output when files are closed for writing, or otherwise knowing when it is safe to interact with those files programmatically? I am specifically running this on Unix systems so if the answer involves some unix tricks to monitor the running ffmpeg process that's fine, but I'd rather ffmpeg tell me directly when it's done writing to files.

1
  • Polling the write lock on a file could be a crude solution too.
    – Zimano
    Aug 2, 2022 at 9:44

1 Answer 1

36

Add -hls_flags temp_file to all your outputs. FFmpeg will write data to a temp file and rename once done. So then if the segment file exists, it is complete.

3
  • 2
    That did the trick! Thank you! For anyone reading this later: The files are called out/1080p_%03d.ts.tmp et al given my example, and then they get renamed to out/1080p_%03d.ts et al.
    – knpwrs
    Jul 30, 2022 at 17:25
  • @knpwrs Small grammar tip; et al is used mostly within scientific literature to refer to a collection of authors; not really like the way you're using it here. If you want to use Latin here, I think et cetera would fit better :)
    – Zimano
    Aug 2, 2022 at 9:42
  • Good point but it looks like I can't edit now.
    – knpwrs
    Aug 3, 2022 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .