Retrieve folder’s name for Automator’s shell script action

automatorbashrenameterminal

For the sake of concatenating video files, I've assembled bits of code found online into a ffmpeg bash script on Automator. It works. However, I'd like for the output file to be named after the folder the videos are in.

I've found solutions online, but none of them follow a shell script workflow. My workflow is just (with an added demanded "Get Specified Finder Items" when tested within Automator):

current_path=$(dirname "$1")
cd "$current_path"

for f in "$@"
do
    /usr/local/Cellar/ffmpeg/4.1_1/bin/ffmpeg -i concat:"$(pipeize() { local OLDIFS="$IFS";IFS='|';echo "$*";IFS="$OLDIFS";}; pipeize *.VOB)" -c copy -map "0:v?" -map "0:a?" -map "0:s?" "THIS_SHOULD_BE_THE_DIRECTORY_NAME.VOB"
done

Best Answer

I'm assuming $1 is e.g. "/path/to/filename.VOB" so $current_path would be: /path/to

So, for $filename use filename="$(basename "$current_path")" placed after: current_path="$(dirname "$1")" and then ext=".VOB" afterwards. You can then change .VOB to other extensions as needed.

Thus, change "THIS_SHOULD_BE_THE_DIRECTORY_NAME.VOB" to "${filename}${ext}" as shown below:

#!/bin/bash

current_path="$(dirname "$1")"
filename="$(basename "$current_path")"
ext=".VOB"
cd "$current_path"

for f in "$@"
do
    /usr/local/Cellar/ffmpeg/4.1_1/bin/ffmpeg -i concat:"$(pipeize() { local OLDIFS="$IFS";IFS='|';echo "$*";IFS="$OLDIFS";}; pipeize *.VOB)" -c copy -map "0:v?" -map "0:a?" -map "0:s?" "${filename}${ext}"
done