Skip to contents

Take one audio track out of infile, and drop the video. When the input has more than one audio track, audio_stream names which one to take. With no audio_stream, the function takes the first audio track. The glossary in vignette("tidymedia") explains media terms such as codec, container and stream copy.

Usage

extract_audio(
  infile,
  outfile,
  audio_codec = "copy",
  audio_stream = NULL,
  run = TRUE
)

Arguments

infile

A string containing the path to a media file.

outfile

A string containing the path of the audio file to write.

audio_codec

A string naming the audio codec for the output stream. The default "copy" copies the stream into the new container without re-encoding. NULL writes no -codec:a, so the output container's default encoder decides. That is useful when FFmpeg cannot copy the source codec into the extension you asked for.

audio_stream

The audio track to take, as a number that counts from 0 among the audio tracks of the input. 0 is the first audio track and 1 is the second. Other streams in the file, such as video, do not count. NULL (default) takes the first audio track. The first-track family reads NULL this way: extract_audio, convert_audio and normalize_audio, and their _batch forms. The every-track family reads it as every audio track: separate_audio_video, standardize_video, anonymize_video, crop_video, segment_video and format_for_web, and their _batch forms. A track the input does not have gives an FFmpeg error, not an R one. See audio_stream for how this differs from audio_input, the input index on compare_videos and picture_in_picture. (default = NULL)

run

A logical: run the command through FFmpeg (TRUE, default) or return the compiled command without running it (FALSE).

Value

The compiled FFmpeg command (invisibly when run = TRUE).

Details

When no audio_stream is named and the input has tracks that the output will not carry, the function warns. The check costs one FFprobe call per distinct input, which is one call here, because this function takes a single infile. The warning is given when FFprobe is available and the input can be probed. Otherwise the check is skipped silently. It never runs under run = FALSE, and never changes the compiled command. Suppress it by naming a track with audio_stream, or by class with suppressWarnings(classes = "tidymedia_dropped_audio").

To switch the check off and skip its FFprobe call, use options(tidymedia.check_tracks = FALSE) for the session. Use withr::local_options(tidymedia.check_tracks = FALSE) for the rest of one function.

Examples

video <- system.file("extdata", "sample.mp4", package = "tidymedia")
extract_audio(video, "audio.aac", run = FALSE)
#> [1] "-y -i \"/home/runner/work/_temp/Library/tidymedia/extdata/sample.mp4\" -codec:a copy -vn -map \"0:a:0\" \"audio.aac\""
# Take the second audio track instead of the first
extract_audio(video, "audio.aac", audio_stream = 1, run = FALSE)
#> [1] "-y -i \"/home/runner/work/_temp/Library/tidymedia/extdata/sample.mp4\" -codec:a copy -vn -map \"0:a:1\" \"audio.aac\""