now with fast codec-copy and output filename selection. not cleaned up, but it works

This commit is contained in:
Jan Koppe 2024-11-01 22:38:37 +01:00
parent 8ddcd251e9
commit e1d6631e9b
Signed by: thunfisch
GPG Key ID: BE935B0735A2129B
1 changed files with 31 additions and 23 deletions

View File

@ -1,6 +1,12 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euxo pipefail set -euxo pipefail
# License MIT
# Authors 2024
# thunfisch
# iiidefix
# l3d
# This script is designed to help you automatically select the correct intro # This script is designed to help you automatically select the correct intro
# and outro files (prerendered with built-in fades) and the correct chunks # and outro files (prerendered with built-in fades) and the correct chunks
# of a chunked recording (e.g. OBS with automatic split every 5 minutes). # of a chunked recording (e.g. OBS with automatic split every 5 minutes).
@ -9,10 +15,7 @@ set -euxo pipefail
# them into the script for now (no idea how to convince mpv to output it). # them into the script for now (no idea how to convince mpv to output it).
# After that the script will render the introfile with the first chunk and the # After that the script will render the introfile with the first chunk and the
# last chunk with the outrofile, and then in the last step assemble everything # last chunk with the outrofile, and then in the last step assemble everything
# into the final recording with audio normalization. The final version should # into the final recording with audio normalization.
# try to use -c:v copy in the last step, so that no re-encoding of the chunks
# in the middle will be done. This does not work yet, so please excuse the slow
# rendering. Upcoming version hopefully fixes it.
# This script requires you to have ffmpeg and fzf installed. # This script requires you to have ffmpeg and fzf installed.
which ffmpeg >/dev/null || (echo "Please install ffmpeg" ; exit 1) which ffmpeg >/dev/null || (echo "Please install ffmpeg" ; exit 1)
@ -30,6 +33,10 @@ SELECTED_OUTRO="$(find "$OUTROS_PATH" -type f | sort --reverse --human-numeric-
SELECTED_CHUNKS="$(find "$CHUNKS_PATH" -type f | sort --reverse | fzf --delimiter / --with-nth -1 -m --prompt "Video Chunks (use tab to select multiple):" | sort )" SELECTED_CHUNKS="$(find "$CHUNKS_PATH" -type f | sort --reverse | fzf --delimiter / --with-nth -1 -m --prompt "Video Chunks (use tab to select multiple):" | sort )"
readarray -t CHUNKS_ARRAY < <(echo "$SELECTED_CHUNKS") readarray -t CHUNKS_ARRAY < <(echo "$SELECTED_CHUNKS")
FOOBARWTF="$(basename "$SELECTED_INTRO")"
DEFAULT_OUTPUT_NAME="${FOOBARWTF%.*}"
read -p "Please enter a name for the outputfile (path and extension will be added automatically): " -i "$DEFAULT_OUTPUT_NAME" -e OUTPUT_NAME
# find the start-offset for the first chunk # find the start-offset for the first chunk
read -p "Do you want to play the first chunk ${CHUNKS_ARRAY[0]} to find the start-offset? (y/n) [n]: " PLAY_FIRST_CHUNK read -p "Do you want to play the first chunk ${CHUNKS_ARRAY[0]} to find the start-offset? (y/n) [n]: " PLAY_FIRST_CHUNK
PLAY_FIRST_CHUNK="${PLAY_FIRST_CHUNK:-n}" PLAY_FIRST_CHUNK="${PLAY_FIRST_CHUNK:-n}"
@ -80,14 +87,15 @@ then
echo "Too few chunks, this script can't handle this yet. Please do that on your own." echo "Too few chunks, this script can't handle this yet. Please do that on your own."
exit 1 exit 1
fi fi
#ffmpeg -i "$SELECTED_INTRO" \
# -i "$SELECTED_OUTRO" \
# -ss "$START_OFFSET" -i "${CHUNKS_ARRAY[0]}" \
echo "${CHUNKS_ARRAY[@]:1:-2}"
# -t "$END_OFFSET" -i "${CHUNKS_ARRAY[-1]}" \
# STEP 1 # STEP 1
# temp dir # temp dir
WORKDIR=$(mktemp -d) WORKDIR=$(mktemp -d)
function finish {
rm -r "$WORKDIR"
}
trap finish EXIT
# STEP 2 # STEP 2
# introfile with first chunk and crossfade encode # introfile with first chunk and crossfade encode
ffmpeg -i "$SELECTED_INTRO" -ss "$START_OFFSET" -i "${CHUNKS_ARRAY[0]}" \ ffmpeg -i "$SELECTED_INTRO" -ss "$START_OFFSET" -i "${CHUNKS_ARRAY[0]}" \
@ -123,26 +131,26 @@ ffmpeg -i "$SELECTED_OUTRO" -t "$END_OFFSET" -i "${CHUNKS_ARRAY[-1]}" \
# STEP 4 # STEP 4
# encoded intro+outro and all chunks in between with c:v copy and audio dynnorm + encode # encoded intro+outro and all chunks in between with c:v copy and audio dynnorm + encode
FFMPEG_CHUNKS="" CHUNKLIST="${WORKDIR}/chunklist.txt"
FFMPEG_CONCAT=""
echo "file '${WORKDIR}/introcombined.mkv'" > "$CHUNKLIST"
FFMPEG_CONCAT_CHUNKS=""
if [[ ${ARRAY_LENGTH} -gt 2 ]] if [[ ${ARRAY_LENGTH} -gt 2 ]]
then then
for index in $(seq 1 $(( ${ARRAY_LENGTH} - 2 )) ) for index in $(seq 1 $(( ${ARRAY_LENGTH} - 2 )) )
do do
FFMPEG_CHUNKS="${FFMPEG_CHUNKS} -i ${CHUNKS_ARRAY[index]}" FFMPEG_CONCAT_CHUNKS="${FFMPEG_CONCAT_CHUNKS}|${CHUNKS_ARRAY[index]}"
FFMPEG_CONCAT="${FFMPEG_CONCAT} [$((index + 1)):v:0][$((index + 1)):a:0]" echo "file '${CHUNKS_ARRAY[index]}'" >> "$CHUNKLIST"
done done
fi fi
ffmpeg -i "${WORKDIR}/introcombined.mkv" -i "${WORKDIR}/outrocombined.mkv" \ echo "file '${WORKDIR}/outrocombined.mkv'" >> "$CHUNKLIST"
$FFMPEG_CHUNKS -filter_complex \
"[0:v:0][0:a:0] ffmpeg \
$FFMPEG_CONCAT -f concat -safe 0 -i "$CHUNKLIST" \
[1:v:0][1:a:0] -af dynaudnorm \
concat=n=${ARRAY_LENGTH}:v=1:a=1 -c:v copy \
[v][a];[a]dynaudnorm[ad]" \
-map '[v]' -map '[ad]' \
-c:v libx264 -threads 0 -pix_fmt yuv420p -crf 18 -profile:v high -level 4.1 -disposition default \
-c:a aac -b:a 192k \ -c:a aac -b:a 192k \
-metadata:s:a:0 language=native \ -metadata:s:a:0 language=native \
"${OUTPUT_PATH}/combinedrender.mkv" "${OUTPUT_PATH}/${OUTPUT_NAME}.mkv"