Category: Programming

How to remove an unwanted audio stream from an MP4 file

Let’s assume that an MP4 video file has two audio streams, but you only want to keep one.

The following line will create a new mp4 video file that only has the first audio stream:

ffmpeg -i input.mp4 -map 0:v:0 -map 0:a:0 -c copy output.mp4

The following line will create a new mp4 video file that only has the second audio stream:

ffmpeg -i input.mp4 -map 0:v:0 -map 0:a:1 -c copy output.mp4

How to query the name and directory of a Bash script

Once in a while, a Bash script needs to query its own name and directory. Here is an example on how to do that:

#!/usr/bin/bash

ThisScriptsName="$(basename -- "${BASH_SOURCE[0]}")"
ThisScriptsDirectoryAndName="$(readlink -f -- "${BASH_SOURCE[0]}")"
ThisScriptsDirectory="$(dirname -- "$ThisScriptsDirectoryAndName")"
TheCurrentDirectory="$(pwd)"

printf 'ThisScriptsName: %s\n' "$ThisScriptsName"
printf 'ThisScriptsDirectoryAndName: %s\n' "$ThisScriptsDirectoryAndName"
printf 'ThisScriptsDirectory: %s\n' "$ThisScriptsDirectory"
printf 'TheCurrentDirectory: %s\n' "$TheCurrentDirectory"

#EOF

Let’s assume the name of the file is WhatsMyNameAndLocation, and it is located in /usr/scripts and executed from / with /usr/WhatsMyNameAndLocation:

ThisScriptsName: WhatsMyNameAndLocation
ThisScriptsDirectoryAndName: /usr/scripts/WhatsMyNameAndLocation
ThisScriptsDirectory: /usr/scripts
TheCurrentDirectory: /