How to transcode a file and stream it simultaneously?¶
This page describes how to “transcode on the fly”, which is transcoding and streaming simultaneously. As an example, we will transcode an MP4
file to an MKV
container by encoding it with h265
and mpga
codecs, and stream the output over HTTP.
Because the sout chain works as a pipeline, we can stream while transcoding by simply combining the code of:
Transcoding a file, and
Streaming over HTTP.
Hence, as a pre-requisite, refer to How to transcode a file to understand how to perform basic transcoding, and refer to Stream over HTTP to understand how to stream a file over HTTP in the local network.
How to send the stream?¶
In order to send the transcoded stream, we will do the following step-by-step:
Transcode the MP4 file by re-encding it with the
h265
and thempga
codecs,Mux the transcoded streams into an
MKV
container, andStream the final output over HTTP at port number
8090
and path namestreaming
(you can choose any port number and path name).
Steps 1 an 2 can be implemented by using the transcode
and the std
blocks as in the following code snippet:
transcode{vcodec=h265, acodec=mpga}:std{mux=mkv}
While step 3 can be implemented by setting the access
and dst
parameters of the std
block in the following manner:
std{access=http, dst=:8090/streaming}
By combining both of these parts we get the code that should be executed on the sending device to start the transcoding and streaming:
$ vlc sample.mp4 --sout="#transcode{vcodec=h265, acodec=mpga}:std{mux=mkv, access=http, dst=:8090/streaming}"
How to receive the stream?¶
As HTTP is a pull protocol, the stream will be published over the IP address of the sending device at the specified port number and path (which are 8090
and streaming
in our example).
Hence, we need the IP address of the streaming device in order to catch the stream. Suppose the IP address of the streaming device is 192.168.0.101
.
Run the following code on the receiving device(s) to catch the transcoded stream:
$ vlc http://192.168.0.101:8090/streaming
As soon as the above code is run, a VLC Media Player window will open and start playing the stream.
Graphically, this is how the network might look: