ffmpeg - How to copy|extract encoding settings from existing media file?
There is no automatic way to do that. You have to look at the parameters of the original file and apply them to the output file.
In most cases, these will be the following:
- Container format (MP4, MKV, …)
- Video and audio codec (H.264, H.265, …)
- Audio-specific:
- Number of audio channels
- Audio sampling rate
- Audio bitrate
- Video-specific:
- Profile and Level (to ensure compatibility, see here)
- Maximum bitrate limitations (e.g. for H.264)
- Maximum video resolution, change via
-filter:v scale
or-s:v
- Framerate, change via
-filter:v fps
-r
- Chroma subsampling, change via
-pix_fmt
(e.g.,-pix_fmt yuv420p
should give you the best compatibility) - GOP size (distance between IDR-frames), set via
-g
- Other specific encoding settings
But even if you get that all right, some devices may require specific, proprietary information embedded in the bitstream.
As for the specific task of using x264
, this is not going to be trivial. I'm not aware of a single script that'd take care of these tasks, which are usually done manually. For the most info about the encoding settings, on Unix/Linux or OS X, you can use mediainfo
with some Bash tricks.
For example, for an x264-encoded video in an MP4 file:
mediainfo input.mp4 | grep "Encoding settings" | cut -d':' -f2- | tr '/' '\n' | sed 's/ //'
This will output a list of x264 options:
cabac=1
ref=3
deblock=1:-1:-1
analyse=0x3:0x113
me=hex
subme=7
psy=1
…
You could then manually pass these options to the x264 binary.
If you go through FFmpeg, that's a little more complicated though, as not all of x264's options can or should be mapped like this. Note that often a simple preset, tune and profile specification will do as well (as seen in x264 --fullhelp
and the x264 encoding guide), and specifying the CRF level is enough.
And this is not even considering audio, where luckily, there aren't that many options.
Related videos on Youtube
n611x007
Updated on September 18, 2022Comments
-
n611x007 almost 2 years
I wished to be able do this multiple times now so I ask.
If I have an existing video or audio file,
ffmpeg
,mplayer
and other media players can detect at least some of it's "propreties" like container, codec and bitrate used, probably various quality and encoding specific settings, etc.How can I extract these settings from an existing file in order to use them (directly) for encoding with ffmpeg?
For example, I got an
mkv
video encoded withx264
with some settings, and another uncompressedavi
file. I would like to "copy" the encoding settings used in for x264 mkv file to transcode the avi with the same settings.Note: I'm looking for a way that should include no human work with "translating" the settings from a decoder to the encoder. It is okay if I need to extract the settings first and save it somewhere but I would like to be able to simply feed the read options to a command line or preset|configuration file for ffmpeg.
-
evilsoup about 11 yearsWell, you could probably sort through the output of
ffprobe
(especially with the-show_streams
and-show_format
options) and use that information to script the important stuff -
Elisa Cha Cha about 11 yearsOften using the same settings, such as bitrate (attempting to copy exact bitrate seems to be popular among users), will not result in optimal quality. Of course I have no clue what your typical input is like, but with x264 usage of the
-crf
option is generally recommended. See the FFmpeg and x264 Encoding Guide. -
thelolcat over 8 yearsI can't find anything about encoding settings in ffprobe's output :(
-
thelolcat over 8 yearsIt only shows the number of ReFrames, but not the other settings like mediainfo does
-
jiggunjer over 8 yearsInput syntax with ffmpeg is different from the metadata fields. e.g. you could read
channels:6
from a video file, but to encode it you'd need to use the switch-ch:6
. Some switches use 1 letter, some use two, etc.
-
-
thelolcat over 8 yearsSo it's not possible with ffprobe or ffmpeg?
-
slhck over 8 yearsNo, it's not directly possible to do it with ffprobe/ffmpeg. In case of x264-encoded video, I would use the
mediainfo
output and map these options to ffmpeg with-x264-params
. (See also the x264 encoding guide). But if you want a general solution for all kinds of codecs/encoders or container formats, then you'd need to write a moderately sophisticated script that does the conversion. -
cduguet over 3 yearsIt's been 5 years now. Any new answer?
-
slhck over 3 years@cduguet This is as complicated today as it was five years ago, if not more, so no.
-
1110101001 over 3 yearsNote that x264 includes the encoding settings in the bitstream. This is how
mediainfo
is able to show it (or you can usestrings
to see the same.ffprobe
doesn't show it either, presumably because it only deals with generic properties for codecs/containers and not codec-specific metadata).