ant-media / Ant-Media-Server

Ant Media Server is a live streaming engine software that provides adaptive, ultra low latency streaming by using WebRTC technology with ~0.5 seconds latency. Ant Media Server is auto-scalable and it can run on-premise or on-cloud.
https://antmedia.io
Other
4.25k stars 624 forks source link

Recording WebRTC Dynamic fram sizes #4577

Open danavramescu opened 1 year ago

danavramescu commented 1 year ago

When recording a WebRTC Stream as MP4 through ant media server, the recording is created with dynamic frame sizes, captured exactly as the browser changes resolution. Is it possible to add a flag so that the recording is created in a fixed resolution (the maximum) ?

For example if we publish a stream at 1280x720 , it starts in 640x360, later increases to 960x540 later increases again to 1280x720 (which is our currently set max video constraint). later it might drop again and increase again a few times during the session. Because the resolution changes during a livestream, the recording's frames sizes also change accordingly. So we would like to know if we can force a constant frame size (max possible) when starting the recording in AMS

The dynamic frame sizes is creating issues when we reprocess the resulted video in order to apply a watermark, forcing the whole recording into the size of the first frame, which is 640x360 and therefore resulting in a very bad quality video, with a filesize 1/5 of the original video.

we use FFMPEG to apply the watermark, currently using the following command: -- note: ams-recording.mp4 is the recording created in AMS--

"ffmpeg -i ams-recording.mp4 -c:v libx264 -vf 'movie=watermark.png [watermark]; [in][watermark] overlay=main_w-overlay_w-5:5 [out]' watermarked-recording.mp4"

Even without applying the watermark, the moment we use -c:v libx264, the entire recording is reprocessed into 640x360. "ffmpeg -i ams-recording.mp4 -c:v libx264 output.mp4" --> results in full video scaled in 640x360 and bad quality "ffmpeg -i ams-recording.mp4 -c:v copy output.mp4" --> results in an identical file, no rescaling, keeps dynamic frame sizes

USAMAWIZARD commented 1 year ago

You can enable adaptive streaming in the application settings add all of your desired resolutions in your application settings. image you will get the recordings of streams in all of the selected resolutions in your streams directory at /usr/local/antmedia/webapps/WebRTCAppEE/streams image Then you can take the file with maximum resolution and apply the water mark accordingly.

burak-58 commented 1 year ago

I think transcoding may be required to force the server to record with the same resolution while input resolution is changing. That may be the same with adding adaptive setting. As a workaround, you may consider adding some dummy frame with the desired resolution at the beginning of the record by a post processing.

danavramescu commented 1 year ago

Thanks guys! For the moment we will not be using adaptive streaming. In the end found a way to collect the maximum frame size and pass it to ffmpeg and rescale the entire video. The quality loss on upscaling a small frame to a bigger one was insignificant. i will leave it up to you guys if you want to close this issue now.

burak-58 commented 1 year ago

Hi @danavramescu, Can you write the solution here? Then we can learn from you.