zombie_transcoding_scripts/ball.md
2023-07-23 08:58:08 -04:00

4 KiB
Executable file

This script transcodes the audio to opus and changes the bitrate according to how many channels the audio has

scripts that i might need later for transcoding

Common Transcoding commands

Transcode input to av1 and transcode the audio to opus with bitrate correction (used for eureka)

#!/bin/bash

# Set bitrate for different channels
BITRATE_1CHANNEL="64k"
BITRATE_2CHANNEL="135k"
BITRATE_6CHANNEL="265k"

# Temporary directory for converted tracks
tmpdir=$(mktemp -d)
outputdir=av1/

mkdir -p $outputdir
echo "Temp dir $tmpdir"
echo "tmpdir\/file $tmpdir/$file"
echo "output dir $outputdir/$(basename "$file")"

ulimit -n 10000
for file in *.mkv; do
  # Check if the file has already been processed
    if [ -f "$outputdir/$(basename "$file")" ]; then
        echo "$file has already been transcoded, skipping..."
        continue
    fi
###############################################################################
#                             audio wizard magic!                             #
# wont work on truehd or dolby atmos or anything with more than 6 channels :( #
###############################################################################
    n_tracks=$(ffprobe -loglevel error -show_streams "$file" | grep -c 'codec_type=audio')

    # Transcode each audio track to Opus
    input_files=(-i "$file")
    map_args=(-map 0:v -map 0:s -c:v copy -c:s copy)
    for ((i=0;i<$n_tracks;i++)); do
        n_channels=$(ffprobe -loglevel error -show_streams -select_streams a:"$i" "$file" | grep 'channels=' | cut -f2 -d=)
        output="$tmpdir/${file%.mkv}_track$i.opus"

        # Choose bitrate based on number of channels
        case "$n_channels" in
            1) bitrate="$BITRATE_1CHANNEL";;
            2) bitrate="$BITRATE_2CHANNEL";;
            6) bitrate="$BITRATE_6CHANNEL";;
            *) echo "Unsupported channel number: $n_channels"; continue ;;
        esac

        echo "Transcoding audio track $i from $file ($n_channels channels) to Opus ($bitrate)..."
        if [ "$n_channels" -eq 6 ]; then
            ffmpeg -loglevel error -i "$file" -map 0:a:$i -af "channelmap=channel_layout=5.1" -c:a libopus -b:a "$bitrate" -mapping_family 1 "$output"
        else
            ffmpeg -loglevel error -i "$file" -map 0:a:$i -c:a libopus -b:a "$bitrate" "$output"
        fi

        input_files+=(-i "$output")
        map_args+=(-map $((i+1)):a -c:a:$(($i)) copy)
    done

    # Merge the transcoded audio tracks, original video track, and original subtitle track back into an MKV file

    ffmpeg -loglevel error "${input_files[@]}" "${map_args[@]}" "$tmpdir/$file"

############################
#          av1an           #
############################

    echo "Audio done! AV1 Time!"
    echo "tmpdir\/file $tmpdir/$file"
    echo "output dir $outputdir/$(basename "$file")"
    av1an -i "$tmpdir/$file" -o "$outputdir/$(basename "$file")" -e aom -c mkvmerge --passes=2 -v "\
    --threads=2\
    --cpu-used=5\
    --end-usage=q\
    --cq-level=24\
    --enable-fwd-kf=1\
    --aq-mode=1\
    --lag-in-frames=48\
    --bit-depth=10\
    --kf-max-dist=240\
    --kf-min-dist=12\
    --enable-qm=1\
    --sb-size=64\
    --enable-keyframe-filtering=2\
    --arnr-strength=2\
    --arnr-maxframes=3\
    --sharpness=1\
    --enable-dnl-denoising=0\
    --denoise-noise-level=5\
    "\
    --chunk-order random -m lsmash -r
done

# Clean up the temporary directory
rm -r "$tmpdir"

For the file in question, all audio tracks will be transcoded to opus. It may be useful to delete unnecessary tracks before transcoding. For example, in the case of Eureka, there were multiple tracks with different channel layouts, one that opus doesnt really support so you might as well delete the one that opus doesnt support, but it still will transcode.

for i in *mkv; do mkvmerge -o "$(basename "$i").balls" --audio-tracks 1 "$i"; done

to delete the unwanted tracks you can use the mkvinfo command to get the track you want to remove