FFMpeg – Transcode, copy flv to mp4 and more
Today we want to take a look at the very useful video encoding and decoding tool-set FFMpeg. (ffmpeg, ffplay and ffprobe) FFMpeg allows you to convert Video and Audio files to several different formats and codecs, as well as cutting, resizing and other functions. So why do we need this?
Some Video editors for example have big problems with certain Video formats. Even the widely used FLV container can sometimes be a problem to open or import successfully. In this case we can use FFMpeg, and to change a container we often do not have to convert the file. We can use the copy function of FFMpeg:
ffmpeg -i input.flv -c copy -copyts output.mp4
This very short commandline lets ffmpeg copy everything inside “input.flv” to our new MP4 file “output.mp4”. Before I will give you some more examples of the possible options, I want to show you a useful combination of FFMpeg and a Batch file, or of course several of them:
D:\Programme\Encoding\ffmpeg -i %1 -c copy -copyts %1.mp4
Assuming your ffmpeg.exe is located on drive D: and inside the Programme\Encoding folder save this command line to a batch file, for example “Copy_to_MP4.bat”. Place this bat file in your Windows “SendTo” folder. Now whenever you have to convert a single FLV file to MP4, you can just right-click it in your Windows explorer and use the “Send to” option to send it to your “Copy_to_Mp4.bat” which will automatically copy it.
If you often create several Video files and want to copy or with a different commandline, convert them, you can add a loop function to the batch file. This will make sure each file you send to the batch will get copied/converted one after the other:
@echo off if [%1]== goto :eof :loop D:\Dropbox\Programme\Encoding\ffmpeg -i %1 -c copy -copyts %1.mp4 shift if not [%1]== goto loop
Now let us take a look at some more interesting FFMpeg command lines. The nginx-rtmp-module under linux can start different transcodes for you, using FFMpeg. That way you can setup your own streaming service and offer different resolutions/qualities for your stream in real-time.
ffmpeg -i rtmp://server.ip/app/1080 -vcodec libx264 -preset veryfast -x264opts nal-hrd=cbr:force-cfr=1:keyint=60 -b:v 3000k -maxrate 3000k -bufsize 3000k -s 1280×720 -sws_flags spline -r 30 -acodec copy -f flv rtmp://server.ip/app/720
Assuming you have a 1920×1080 input stream at 60 fps, the above command line will convert the Video to 1280×720 at 30 fps and a bitrate of 3000 kbit. First of all, instead of an input video file, you of course have to tell ffmpeg to grab your rtmp stream.
-i rtmp://server.ip/app/1080 (input Address)
Next we define the different codec settings as well as the resize and fps change:
-vcodec libx264 (Video codec x264)
-preset veryfast (Encoding preset very fast)
-x264opts nal-hrd=cbr:force-cfr=1:keyint=60 (x264 encoding options, use Constant bitrate and constant framerate, and a keyframe interval of 60)
-b:v 3000k -maxrate 3000k -bufsize 3000k (Max bitrate and buffer size)
-s 1280×720 (resize to 720p)
-sws_flags spline (resize using the spline “filter”, which is similar to Lanczos)
-r 30 (change framerate to 30 fps)
-acodec copy (copy the Audio stream without conversion)
And finally the destination our stream will go to:
-f flv rtmp://server.ip/app/720 (output as flv stream to the set server)
This is pretty much all you need to get started, but if you are looking for a command or option that I did not list yet, FFMpeg has a very big help file. And because of the various codecs it supports, there are just too many options available to cover them in this Article. So if you search for a special command, just use the FFMpeg-help.
You can also use the tool FFplay to watch video files and rtmp streams. And FFprobe allows you to get all saved information out of a Video file. Included Media Streams, encoder settings, Meta data, etc.. So check out those two tools as well.
As always, I hope you find this guide helpful and I hope I gave you enough information to get started now!