Learn how to join the audio and video from a recorded session of the Janus WebRTC server using janus-pp-rec and ffmpeg.

How to join the audio and video (.mjr) from a recorded session of Janus Gateway in Ubuntu 18.04

As you may know, after the implementation and testing of the Janus Gateway, you will be able to record sessions either in Video Rooms or with the RecordPlay plugin. When correctly configured, if you decide to record the sessions, the data is stored in 3 different tracks on your server:

  • room-1234-user-0001-video.mjr: the video track that contains all the frames that were transferred by a single user.
  • room-1234-user-0001-audio.mjr: the audio track contains all that the user spoke during the session.
  • room-1234-user-0001-data.mjr: the data track contains the text data that was sent in the DataChannel of the Janus session.

The 3 mentioned files (2 if you are not using the data channel) are created for every user in the VideoRoom. The biggest issue when trying to share the files with the user is, that they won't know what the **** should they do with 2 files with that weird format.

You should obviously find a way to convert the tracks into common formats like MP4 or webm (ideal to keep the files small) for the video track and opus for the audio track, finally merging them into a single file so the user can easily play them in the browser or download it to play it locally.

In this article, we will explain you how to convert the audio and video format from the generated mjr files from Janus into a single video file (with merged audio).

Before starting, check if Janus was compiled with post processing

If you compiled/installed the Janus gateway without the post processing tools, you won't be able to extract the data from the .mjr generated files by Janus without the janus-pp-rec utility. You can easily check if you compiled it with this extension simply looking for the janus-pp-rec binary in the bin installation directory (usually /opt/janus/), so the binary would be located in the /opt/janus/bin directory:

Janus MJR to audio and video binary (janus-pp-rec)

If you don't have this tool, then you will need to follow the following instructions, otherwise continue with the step #1 of the tutorial.

Install dependencies

Before configuring Janus to be compiled again with post processing, you will need to have the following dependencies installed on your system:

  • libavutil: The libavutil library is a utility library to aid portable multimedia programming. It contains safe portable string functions, random number generators and data structures.
  • libavcodec: libavcodec is a free and open-source library of codecs for encoding and decoding video and audio data.
  • libavformat: The libavformat library provides a generic framework for multiplexing and demultiplexing (muxing and demuxing) audio, video and subtitle streams. It encompasses multiple muxers and demuxers for multimedia container formats.

They can be easily installed on Ubuntu 18.04 with the following command:

sudo apt-get install libavutil-dev libavcodec-dev libavformat-dev

Go to the source code of Janus in your server and reconfigure it with the following command (the prefix may vary according to your setup):

./configure --prefix=/opt/janus --enable-post-processing

If you don't install the previously mentioned dependencies, you will see the following exception during the configuration of Janus:

configure: error: Package requirements (
                           glib-2.0 >= 2.34
                           jansson >= 2.5
                           libavutil
                           libavcodec
                           libavformat
                           ogg
                           zlib
                         ) were not met:

No package 'libavutil' found
No package 'libavcodec' found
No package 'libavformat' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

After running the reconfiguration run the make command:

make

And proceed to reinstall Janus with the new extension:

sudo make install

This should rebuild janus in the same path as usual with the new extension. You can check if everything worked properly, searching for the janus-pp-rec file in the /opt/janus/bin directory. Once you have this tool, you can proceed with the tutorial.

1. Converting tracks to regular formats

If you have the janus-pp-rec binary available, you will be able to use it to export the information into regular formats. Janus helps us to keep everything so simple as it can on the server side, so during the recording, there won't be any extra processing, so the recorder will basically dump all of the RTP frames that it receives into a file in a structured way, so we can process that information later to create playable media files. Awesome isn't? Check out the documentation of the janus-pp-rec.c file for more information about this tool.

Of our interest in this case, is how to use this tool to process the information of the files. This can be easily done in the following way:

janus-pp-rec file.mjr output.[opus|wav|webm|h264|srt]

Depending of the input file, you should export it into a different format, for example with our proposed files:

  • room-1234-user-0001-video.mjr: with a default configuration, the tool will generate a .webm if the recording includes VP8 frames, so the output file should have the .webm extension. 
  • room-1234-user-0001-audio.mjr: for audio files, the tool will generate an .opus if the recording includes opus frames.
  • room-1234-user-0001-data.mjr: the output format for the data channel will be a .srt file.

We should be able to convert the video like this:

# Convert the input mjr file to webm
# This will be the video track only
janus-pp-rec ./room-1234-user-0001-video.mjr ./video-track.webm

And the audio track like this:

# Convert the input mjr file to opus
# This will be the audio track only
janus-pp-rec ./room-1234-user-0001-audio.mjr ./audio-track.opus

If you try to play the output files with a regular player you should be able to, however they are 2 different files, one contains the audio and the other has the video, so you should merge them into a single file, that's what we'll explain in the next step.

2. Install ffmpeg

Janus doesn't offer any utility to merge the tracks into a single file, so you will need to rely on another tool to do the job for you. In this case, we will use ffmpeg. 

Be sure to update the apt repository with the following command:

sudo apt update

Then, proceed to install ffmpeg with the following command:

sudo apt install ffmpeg

The installation will take a while, once it finishes you will be able to print the information about the installed version with the following command:

ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)

Having ffmpeg, you can follow the next step to learn how to join the audio and video track generated by janus-pp-rec.

3. Joining the audio and video into a single file

With the resulting files from the step #1, you should just use ffmpeg to join both tracks (the audio and the video) into a single file with the following command (replace the input files with yours):

ffmpeg -i ./audio-track.opus -i ./audio-track.opus  -c:v copy -c:a opus -strict experimental ./final-video.webm

And that's it, the output webm video will have the video synchronized with the audio that janus recorded during the session properly. That should be enough to achieve what you need !

Bash converter

As an interesting approach to simply provide the input prefix of the files that you are trying to merge and the output file, the following bash script does exactly what we did in this tutorial and receives 2 arguments:

  • The input prefix of the generated files by janus.
  • The filename of the output video.

The script is the following one:

#!/bin/bash

# converter.sh

# Declare the binary path of the converter
januspprec_binary=/opt/janus/bin/janus-pp-rec

# Contains the prefix of the recording session of janus e.g
session_prefix="$1"
output_file="$2"

# Create temporary files that will store the individual tracks (audio and video)
tmp_video=/tmp/mjr-$RANDOM.webm
tmp_audio=/tmp/mjr-$RANDOM.opus

echo "Converting mjr files to individual tracks ..."
$januspprec_binary $session_prefix-video.mjr $tmp_video
$januspprec_binary $session_prefix-audio.mjr $tmp_audio

echo "Merging audio track with video ..."

ffmpeg -i $tmp_audio -i $tmp_video  -c:v copy -c:a opus -strict experimental $output_file

echo "Done !"

So you may simply run the script like this, considering that the files are located in the same directory and have the following names:

  • room-1234-user-0001-video.mjr
  • room-1234-user-0001-audio.mjr
  • room-1234-user-0001-data.mjr

The following command should do the trick:

bash converter.sh ./room-1234-user-0001 ./output_merged_video.webm

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors