There have been many articles about the deployment of nginx streaming media server. Today I will talk about how to push the stream to the server while transcoding the stream into an MP4 file and saving it. The main use here is ffmpeg.
1. The first thing to note is that the exec command cannot be used. It will automatically terminate the called external program when the client push stream ends. The result is that ffmpeg cannot perform complete encoding in the end. This command is only suitable for pushing received traffic to other addresses.
2. You should use exec_record_done with the record command. That is, after the recording is completed, the recorded files are automatically transcoded to obtain MP4 files. There are two different situations depending on whether the record_interval command is used.
2A. If record_interval is not used, the transcoding will be performed after the streaming stops (that is, after the record has completely stopped), and no output will be obtained before that.
2B. If record_interval is used, you can set the transcoding every time Restart recording, combined with the record_append on command, can ensure that the last recorded video is in one file, otherwise a separate file will be generated for each recording. After using these two commands, the transcoding will be performed at regular intervals, but in the end they will be output (overwritten) to the same file. The advantage is that it can transcode in near "real time", but the disadvantage is that it will call ffmpeg repeatedly and occupy system resources.
3. It should be noted that the final transcoded files (duration) obtained by the above two methods are the same.
4. nginx.conf configuration example
application hls {
live on;
hls on;
record all;
record_path /home/zhanghui/test;
#record_interval 10s;
#record_append on;
hls_path /tmp/app;
hls_fragment 5s;
# convert recorded file to mp4 format
exec_record_done '/usr/local/bin/ffmpeg' -y -i /home/zhanghui/test/livestream.flv -vcodec libx264 -f mp4 /home/zhanghui/test/test_record.mp4 2>>/home/zhanghui/test/test_record.log;
}
Copy after login
The above introduces how to realize the recording and transcoding of multimedia streams on the nginx server, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.