This question will seems very weird, please take the time to read

1- I have a .sh containing a ffmpeg command (this commands works, here it's all ok)

2- I trigger the .sh via a API call to start recording (all good here too)

3- the ffmpeg start generating the .mp4

4- I want to symlink the .mp4 to .m3u8 (because safari won't read .mp4 as a manifest)

** You probably wondering : why not just changing the ffmpeg in the .sh to output .m3u8

***** I did try that but for UNKNOWN reasons the script don't like ANY extension except .mp4

***** If I output .mov .mkv .m3u8 => I don't know why, but it stops after 10 seconds :(

***** The solution I found is to let ffmpeg create the .mp4 and then create a symlink to .m3u8 so hls.js can make it play in all browser including safari...

I'm looking for a way to wait for this .mp4 to exists before creating a symlink.

I tested to create the symlink before with ln -s fileThatDontExistsYet.mp4 NewFile.m3u8 but when trying to access NewFile.m3u8 result in a 404 not found.... If I manually wait for .mp4 and then do the same command => it works.

so in this odd situation, I found that sleep x seconds before creating the symlink is risky and not very good practice... is there a way via linux command to loop until the file arrives to symlink it ?

-------- EDIT 1 ----------------

CONTEXT : SERVER1 => website SERVER2 => openvidu running with docker

-1- a user creates a chatroom (SERVER2) -2- SERVER2 sends a webhook to SERVER1 telling a session began -3- then, a API call : SERVER1 tells SERVER2 to records and composed.sh is triggered ---- ALSO : SERVER1 waits x seconds and remote ssh to create the symlink -4- the .sh launch ffmpeg -5- the ffmpeg creates the .mp4 while the chat is going on -6- my modification (see below) is to generate a HLS format stream -7- other users access to the .m3u8 via their browser (the page have hls.js to play the .m3u8) {SERVER1} -8- I can achieve 4 seconds delay for the stream which is not bad ;)

The link to view the FULL raw .sh is here : https://raw.githubusercontent.com/OpenVidu/openvidu/master/openvidu-server/docker/openvidu-recording/scripts/composed.sh

****** LINE 79 ******** I REPLACED

-c:a aac -c:v libx264 -preset ultrafast -crf 28 -refs 4 -qmin 4 -pix_fmt yuv420p


-c:a aac -c:v libx264 -movflags +dash 
-preset ultrafast -crf 28 -refs 4 -qmin 4 -pix_fmt yuv420p 
-tune zerolatency -c:a aac -ac 2 -profile:v main 
-flags -global_header -bufsize 969k 
-hls_time 1 -hls_list_size 0 -g 30 
-start_number 0 -streaming 1 -hls_playlist 1 
-lhls 1 -hls_playlist_type event -f hls

It seems weird but it works... my only worry is the "wait x seconds" to create a symlink... what is the server is very busy and the .mp4 gets generated after the x second... it would create issues for playing the stream.

  • 1
    How do you open/access NewFile.m3u8? (In which program? Using which command?). Also, can you change the ".sh" you have? E.g. can you make it either rename the file or create the symlink after ffmpeg terminates? – fra-san Apr 1 at 23:00
  • @fra-san the .m3u8 is accessed via a browser, not a program. If the browser hit a .m3u8 (symlink created before the .mp4) => the browser won't open it. /// I can change the .sh and I tried to create the symlink there: it creates it, but same issue, because it's created before ffmpeg begins, the browser says 404 not found – Jintor Apr 1 at 23:06
  • "because it's created before ffmpeg begins" – Why not after ffmpeg terminates? – Kamil Maciorowski Apr 2 at 0:30
  • Could you update your question to include the part of the script that makes the mp3 and the symlink? – linux_kettle Apr 2 at 0:32
  • @KamilMaciorowski It don't want to wait, see my EDIT 1 in the question, I added more details, it's for live video streaming – Jintor Apr 2 at 0:53

I have to admit that i didn't read your whole question but judging from the title this should work:

while [ ! -f /tmp/originalfile ] ; do sleep 1 ; done ; ln -s /tmp/originalfile /tmp/symlink

  • Note that you don't have to sleep exactly 1 second, feel free to use sleep 0.1 or lower if you are in a hurry. If you are really in a hurry, use true instead of sleep someseconds – Garo Apr 2 at 1:11
  • There is an unknown (unpredictable) delay between the webhook, the api call and ffmpeg process. Right now I sleep 8 (8 seconds), that's why I was looking for a better solution then sleep(8)..... your answer seem a god way to start, let me do some tests... also, ffmpeg creates a buffer before creating the .mp4 – Jintor Apr 2 at 1:22
  • @Jintor If by "a buffer" you mean an empty file that shouldn't be linked to because it's not a video yet, then the test may be like file -b --mime-type foo.mp4 | grep -q video/. – Kamil Maciorowski Apr 2 at 1:40
  • @KamilMaciorowski I tried the answer from Garo and it's working very well ;) the buffer was not the issue, I simply needed a way to "wait" for the creation of the file – Jintor Apr 2 at 1:52

You can use inotifywait to wait until a file is created in the parent directory:

inotifywait -e create /path/to/parent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.