Instant lossless video editing

11 mar
2021

Categories: Techno

The worst thing in streaming workflow is postprocessing. After successful live event delivery, recorded event should be available for viewers ASAP. My CDN records all incoming streams, my encoder (Tricaster) does beautiful job in terms of compression (720p/1Mb/s stream looks perfect). I can publish archive in seconds after end of live event.

Unfortunately, I have not seen any event starting right on time, there is always few minutes of waiting and chaos (streamed/recorded), and audio level is always variable. Voices are different, mics are different, recorded audio is not perfect.

So, there is no way to use recorded stream as final archive format. Recording has to be edited. And it takes ages just to cut it and slightly process sound (most often compress and then normalize), then upload.

It can not be done onsite, as derigging begins. You have to go to your studio/office, process your video in Premiere or FCP, render (one hour per one hour of recording), then upload to CDN. A day, or so.

Have you ever dreamed to do it all in 15 minutes? Right after the event? W/o recompression? With beautiful quality from your live compressor?

If you have ssh access to your CDN computers, ffmpeg is your friend and everything is possible.

Ffmpeg is free software able to do a lot of processing tasks in streaming. It can recompress your video, convert mp3 audio to aac, Cut, concatenate, mux your audio, video and closed captions streams. Real Swiss army knife of streaming.

You can easily cut beginning and trailing fragments of your recorded video right on server. Command to perform this task is simple. Following command cuts 20 seconds of video from SOURCEFILE starting from position at 10 seconds and saves it into TARGETFILE. It does no compression, just “cut&paste”.

ffmpeg -i SOURCEFILE -vcodec copy -acodec copy -ss 00:00:10.000 -t 00:00:20.000 TARGETFILE

Simple, isn’t it? And it takes a minute or so, to split it!

If your CDN is powered by Wowza you might notice problem playing this new file. Audio might be shifted to video. It is because of way Wowza threats audio and video streams. Audio starts to be played when video reaches first key frame. File might look well in VLC, but will be played with ugly desync from Wowza. Found solution, you have to use ffmpeg again, and –ss option must be before –i in parameter queue. Simple command:

ffmpeg -ss 0 -i SOURCEFILE -acodec copy -vcodec copy TARGETFILE

fixes it perfectly in few seconds.

Target file will be playable by Wowza now. You can help Wowza or other streaming server to better and easier handle your file by running your file by another tool – qtfaststart. This tinny tool written in Python relocates metadata to the front of file. It simplifies scrolling through file, and in case of pseudostreaming (you have no any dedicated CDN) it is only way of enabling seek in your files.

So install Python, use pip (pip install qtfaststart) to install this tool https://github.com/danielgtaylor/qtfaststart

Now you can process your file just by typing

qtfaststart SOURCEFILE

It will take few seconds up to minute on modern computer or server to process it (it takes 20 seconds on my server to process 2 hrs recording).

Now your file is perfect for VOD streaming. Cut and prepared for VOD streaming.

Still your file will have poor audio. ffmpeg is able to help you with audio too. It is possible to use filters on audio and video during ffmpeg conversions. It is most complicated part of process. Not because it needs many actions, it is just one ffmpeg command, but it needs some deeper knowledge, how filtering works. You can study it here: https://ffmpeg.org/ffmpeg-filters.html#compand

In my postproduction workflow I am using audio Compressor/limiter to level everything in my audio stream. Instead of acting globally like normalize Compressor acts locally levelling audio in small areas. Commonly used nowadays Compressor allows you to have nice audible audio fast and in whole file, regardless of peaks..

I am using “compand” parameter to invoke audio compressor filter in ffmpeg.

Ffmpeg is command line tool, so sophisticated parameters are entered “weird” way. Following code compresses audio with preset suitable for conferences. Levels at -15dB and then adds 12dB (so your file peak will be at -3dB). Video stream will be untouched.

ffmpeg -i SOURCEFILE -vcodec copy -strict experimental -c:a aac -b:a 128k -af "compand=0|0:1|1:-90/-900|-70/-70|-21/-21|0/-15:0.01:12:0:0" TARGETFILE

In above command I am invoking aac audio encoding. Regardless of source file audio encoding, result audio stream will be aac encoded (so will be playable on apple devices with audio).

You should do audio compression before other operations.

To sum up.

First operation in my workflow is audio compression and audio codec conversion to aac

ffmpeg -i SOURCEFILE -vcodec copy -strict experimental -c:a aac -b:a 128k -af "compand=0|0:1|1:-90/-900|-70/-70|-21/-21|0/-15:0.01:12:0:0" TARGETFILE

Second – cut beginning and ending

ffmpeg -i SOURCEFILE -vcodec copy -acodec copy -ss 00:00:10.000 -t 00:00:20.000 TARGETFILE

Third - fix audio and video for streaming

ffmpeg -ss 0 -i SOURCEFILE -acodec copy -vcodec copy TARGETFILE qtfaststart SOURCEFILE

In my case it takes 10 to 15 minutes to cut two parts of 5 hrs conference from source material and to publish it on my Wowza. Before ffmpeg workflow implementation it took me day or two to conform, compress audio, render file and upload it to server.

Return to index