You should first upgrade to at least FFmpeg 0.9. vpre
is not really encouraged to be used for preset control anymore. This is why the ffpreset
files have been removed from newer versions. Preset control for libx264 now happens through x264 itself, not through FFmpeg.
That being said, setting an average bitrate for x264 is the worst rate control mode there is. Rather than an average bitrate, you should consider using two-pass encoding if you are targeting a specific output file size, or setting a Constant Rate Factor (CRF) if you just care about achieving a specific quality. If in doubt, use CRF.
You generally want the following syntax for using FFmpeg and x264:
ffmpeg -i in.mp4 -vcodec libx264 <options> out.mp4
… with the following non-mandatory options. It is important to specify them before the output and after the vcodec
switch. Those are just some (see also the options mapping for more):
-vprofile
: Set h.264 profile constraints, e.g. baseline
, main
, high
-preset
: Set an encoding preset, which generally enables optimization strategies. You can choose from veryslow
, slow
, fast
, ultrafast
, and many more. Slower presets result in better compression at a cost of slower encoding. You generally use the slowest preset you have patience for.
-tune
: Set a special tune factor, e.g. for movies: film
-b
: Set the bitrate. As already said, this is not really what you want with a one-pass encoding, but if you need to, this should work. Possible values are in Bits, or rather 500K
, 1M
, et cetera.
-crf
: Set quality. Mutually exclusive with -b
. A good starting value is 24. A lower value is higher quality. Use the highest value that still looks good to you.
Two-pass example, H.264 video, AAC audio in MP4:
ffmpeg -i input -c:v libx264 -preset slow -b:v 500k -pass 1 -an -f mp4 -y NUL
ffmpeg -i input -c:v libx264 -preset slow -b:v 500k -pass 2 -c:a libfaac -b:a 128k output.mp4
CRF example, H.264 video, MP3 audio in MKV:
ffmpeg -i input -c:v libx264 -preset medium -crf 24 -c:a libmp3lame -q:a 4 output.mkv
Multimedia files are complex, so concatenation has several requirements:
- All segments to be concatenated must have the same number and type of streams.
- All streams must have the same parameters.
Since each input may vary in any arbitrary parameter I recommend using the concat filter. You will have to conform each input to a common set of parameters using filters. The input format doesn't matter so much because you're going to re-encode everything anyway.
Basic, generic example where main.mp4
has video and audio and end.mp4
has video and no audio. All other parameters are assumed to be different. The filters used here are: anullsrc, scale, pad, setsar, fps, format, aformat, and concat.
ffmpeg -i main.mp4 -i end.mp4 -f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 -filter_complex "[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=25,format=yuv420p[v0];[1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=25,format=yuv420p[v1];[0:a]aformat=channel_layouts=stereo:sample_rates=44100[a0];[v0][a0][v1][2:a]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4
There are several ways to do this, and you may have to make adjustments based on your inputs or to fit your requirements.
Best Answer
Preserve settings
ffmpeg
will automatically attempt to use many of the same parameters when encoding including: frame rate, width, height, pixel format, audio channel layout, audio sample rate, etc. So you usually don't have to do anything special.Some settings may change if there are format or encoder restrictions.
Preserve quality
For H.264 video using the encoder libx264 use:
-crf 18
These options will output a lossy video, but it
-crf 18
provides enough bits that it will likely be visually lossless or nearly so. If the output is still too big the general recommendation is to use the highest-crf
value that still provides an acceptable quality.You can change it to
-crf 0
for true lossless, but the resulting output will be a huge file size–probably even bigger than the original.Example:
This example stream copies the audio instead of re-encoding it since the majority of the file size comes from video.
Development is very active, so make sure to use a recent build of
ffmpeg
. See the FFmpeg Download page for links to binaries.Also see: