Transcoding is the process of decoding a stream and then encoding it again. Since encoding tends to be computationally expensive and in most cases degrades the stream quality (i.e. it is lossy), you should only transcode when you need to and perform streamcopy otherwise. Typical reasons to transcode are:
- applying filters - e.g. resizing, deinterlacing, or overlaying video; resampling or mixing audio;
- you want to feed the stream to something that cannot decode the original codec.
Note that ffmpeg
will transcode all audio, video, and subtitle streams unless you specify -c copy for them.
Consider an example pipeline that reads an input file with one audio and one video stream, transcodes the video and copies the audio into a single output file. This can be schematically represented as follows
┌──────────┬─────────────────────┐ │ demuxer │ │ audio packets ╞══════════╡ stream 0 (audio) ├─────────────────────────────────────╮ │ │ │ │ │INPUT.mkv ├─────────────────────┤ video ┌─────────┐ raw │ │ │ │ packets │ video │ video frames │ │ │ stream 1 (video) ├─────────⮞│ decoder ├──────────────╮ │ │ │ │ │ │ │ │ └──────────┴─────────────────────┘ └─────────┘ │ │ ▼ ▼ │ │ ┌──────────┬─────────────────────┐ video ┌─────────┐ │ │ │ muxer │ │ packets │ video │ │ │ ╞══════════╡ stream 0 (video) │⮜─────────┤ encoder ├──────────────╯ │ │ │ │ │(libx264)│ │ │OUTPUT.mp4├─────────────────────┤ └─────────┘ │ │ │ │ │ │ │ stream 1 (audio) │⮜────────────────────────────────────╯ │ │ │ └──────────┴─────────────────────┘
and implemented with the following commandline:
ffmpeg -i INPUT.mkv -map 0:v -map 0:a -c:v libx264 -c:a copy OUTPUT.mp4
Note how it uses stream specifiers :v
and :a
to select input streams and apply different values of the -c option to them; see the Stream specifiers section for more details.