0

I would like to use the same command, regardless of if my source file, or source stream, has an audio track or has no track at all. I then want ffmpeg to transcode the audio - using either the audio source (if present), or silence (which I see various various ffmpeg audio filters can generate) only if no source audio track is present.

Could someone give an example or some pointers on the right command for this? Some way to do this with map or similar switches?

Note here I truly mean no audio track at all, versus say detecting silence in the source audio track. It is a key to be able to do this in one command versus knowing two discreet commands that would do each variation.

Dan Pisarski
  • 337
  • 1
  • 2
  • 10

1 Answers1

1
ffmpeg -i input.mp4 -f lavfi -i anullsrc=cl=mono -shortest output.mp4

This is an example where the default stream selection behavior can be used. It will automatically choose the audio stream with the most channels, and if they are the same then it will choose the audio stream with the lowest index number.

  • If input.mp4 does not have audio then it will use the silent audio generated by anullsrc.

  • If input.mp4 does have audio, then it will use the audio from input.mp4.

Downside is that the default stream selection behavior will only choose one stream per stream type. So if input.mp4 has multiple video streams then only one video stream will be chosen.

In that case I suggest using ffprobe to determine if a file has audio, and then using -map to choose the specific streams you want.

This could be automated with simple shell scripting adapting something like Bash example 1 and Bash example 2.

llogan
  • 121,796
  • 28
  • 232
  • 243