There are two main methods to get only the video or audio from a file that contains both. By default the stream selection behavior in ffmpeg will select one stream per stream type. Note that inputs can contain multiple stream types: video, audio, subtitle, and data. Some inputs can even contain several of each of these streams types; such as two video streams.
These options tell ffmpeg to omit the video (
-vn) or the audio (
To omit the video and copy audio:
ffmpeg -i input -vn -codec:a copy output.mka
To omit the audio and copy video:
ffmpeg -i input -an -codec:v copy output.mkv
These examples will stream copy the desired stream. This means that ffmpeg will simply re-mux it instead of re-encoding. This is very fast, and since it is essentially "copying and pasting" it will not affect the quality.
-an will not omit other streams that may be present, such as subtitle or data streams, but audio only formats will ignore these anyway.
-map option may seem more complicated but is more flexible and useful. For example,
-vn tells ffmpeg to only omit the video stream(s), but by using a stream specifier
-map can tell ffmpeg to omit everything but the desired stream.
To copy only the video:
ffmpeg -i input -map 0:v -codec copy output.mkv
To copy only the audio:
ffmpeg -i input -map 0:a -codec copy output.mka
0:v is a stream specifier telling ffmpeg to only include from the first input
0 the video streams
v. This can be extended further. Using
first input:video:first video stream and would be useful if your input contained multiple video streams and you only wanted the first one. Refer to the ffmpeg console output to find out the order of the streams in your input.
You can also use negative mappings. Using
-map 0 -map -0:a:1 would map all streams except for the second audio stream.
-map 0 tells ffmpeg to include all streams from input
0 (the first input), and
-map -0:a:1 then tells ffmpeg to ignore the first audio stream from the first input.