Skip to contents

Choose which input streams go into the output, with FFmpeg's -map option. The default, "0", maps every stream from the first input. The glossary in vignette("tidymedia") explains media terms such as stream.

Usage

ffm_map(object, mapping = "0", replace = FALSE)

Arguments

object

An FFmpeg pipeline (ffm) object created by ffm_files().

mapping

A character vector of one or more stream specifiers. Each one adds one -map.

replace

A logical. TRUE discards any mapping already set on object. FALSE (the default) adds to it.

Value

object with an added instruction to map streams.

Details

mapping can be a character vector. Each element adds one -map, in the order given. For example, ffm_map(object, c("0:v", "0:a:1")) keeps the video and the second audio track of the input.

A second ffm_map() call adds to the maps already set. It does not replace them. Pass replace = TRUE to discard them instead. That is how you narrow the all-streams map that ffm_copy sets. Adding to that map puts the stream in the output twice, and does not select it.

Other pipeline functions also add to earlier calls. Each function that adds a filter, such as ffm_crop or ffm_loudnorm, puts its filter after the filters already set. ffm_output_options puts its options after the options already set. ffm_seek, ffm_drop, ffm_codec, ffm_pixel_format and ffm_copy replace the values they set. But with its default streams = TRUE, ffm_copy() gives an error rather than replace a mapping other than "0". ffm_map() adds because its arguments are partial choices that combine. For example, you keep the video, then name one audio track.

When the pipeline uses ffm_hstack, ffm_vstack or ffm_overlay, your mapping is added beside the automatic -map "[vout]" of the filtered stream. For example, ffm_map(object, "0:a") keeps the audio of the first input next to the stacked video.

Examples

video <- system.file("extdata", "sample.mp4", package = "tidymedia")
ffm_files(video, "output.mp4") |>
  ffm_map(mapping = "0") |>
  ffm_compile()
#> [1] "-y -i \"/home/runner/work/_temp/Library/tidymedia/extdata/sample.mp4\" -map \"0\" \"output.mp4\""

# Keep the video and the second audio track only
ffm_files(video, "output.mkv") |>
  ffm_map(mapping = c("0:v", "0:a:1")) |>
  ffm_compile()
#> [1] "-y -i \"/home/runner/work/_temp/Library/tidymedia/extdata/sample.mp4\" -map \"0:v\" -map \"0:a:1\" \"output.mkv\""