Created
February 27, 2023 02:14
-
-
Save aynik/7500d830b6221e862339e08b6092f1e1 to your computer and use it in GitHub Desktop.
Post processing custom script for *arr services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
[[ "$radarr_eventtype" == "Test" || "$sonarr_eventtype" == "Test" ]] && exit 0 | |
# Determine if the script is being run by Radarr or Sonarr | |
if [[ -n "$radarr_moviefile_path" ]]; then | |
# Running in Radarr | |
input_file="$radarr_moviefile_path" | |
output_file="${input_file%.*}.mp4" | |
movie_id="$radarr_movie_id" | |
api_key="<RADARR_API_KEY>" | |
host="http://localhost:7878" | |
elif [[ -n "$sonarr_episodefile_path" ]]; then | |
# Running in Sonarr | |
input_file="$sonarr_episodefile_path" | |
output_file="${input_file%.*}.mp4" | |
series_id="$sonarr_series_id" | |
episode_id="$sonarr_episode_id" | |
api_key="<SONARR_API_KEY>" | |
host="http://localhost:8989" | |
else | |
# Unknown environment | |
echo "Unknown environment: not running in Radarr or Sonarr" | |
exit 1 | |
fi | |
input_dir="$(dirname "$input_file")" | |
if [ -z "$SCRIPT" ] | |
then | |
log_dir="$HOME/.logs" | |
mkdir -p "$log_dir" | |
log_file="$(basename "$input_file")" | |
/usr/bin/script "$log_dir/$log_file.log" /bin/bash -c "$0 $*" | |
exit 0 | |
fi | |
# Define the temporary output file path | |
temp_output_file="${input_file%.*}.temp.mp4" | |
# Define the audio transcoding settings | |
audio_codec="-c:a aac -b:a 320k -ac 2" | |
audio_filter="-af dynaudnorm=f=75:g=11:s=12" | |
# Log the start time and input file information | |
echo "Transcoding started at $(date)" | |
echo "Input file: $input_file" | |
echo "Output file: $output_file" | |
echo "Temporary output file: $temp_output_file" | |
# Copy the video stream and transcode the audio to the specified settings | |
/usr/local/bin/ffmpeg -i "$input_file" -map 0:v:0 -map 0:a -c:v copy $audio_codec $audio_filter -sn "$temp_output_file" | |
# Move the temporary output file to the final output file path | |
mv "$temp_output_file" "$output_file" | |
# Log the end time and output file information | |
echo "Transcoding finished at $(date)" | |
echo "Output file size: $(du -h "$output_file" | awk '{print $1}')" | |
echo "Output file duration: $(/usr/local/bin/ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$output_file") seconds" | |
if [[ "$input_file" != "$output_file" ]]; then | |
# Deletes the input file if is different than output file | |
rm "$input_file" | |
fi | |
# Move subtitles if there temporarily | |
tmp_subtitle_dir="$(mktemp -d)" | |
if ls "$input_dir"/*.srt &> /dev/null; then | |
mv "$input_dir"/*.srt "$tmp_subtitle_dir" | |
fi | |
# Notify Radarr or Sonarr of the transcoded file | |
if [[ -n "$movie_id" ]]; then | |
# Notify Radarr | |
for i in {1..2}; do # Because of a bug it needs to be called twice | |
curl -X POST \ | |
-H "accept: */*" \ | |
-H "Content-Type: application/json" \ | |
-H "X-Api-Key: $api_key" \ | |
-d "{\"name\": \"RescanMovie\", \"movieId\": $movie_id}" \ | |
"$host/api/v3/command" | |
done | |
elif [[ -n "$series_id" ]]; then | |
# Notify Sonarr | |
for i in {1..2}; do # Because of a bug it needs to be called twice | |
curl -X POST \ | |
-H "accept: */*" \ | |
-H "Content-Type: application/json" \ | |
-H "X-Api-Key: $api_key" \ | |
-d "{\"name\": \"RescanSeries\", \"seriesId\": $series_id}" \ | |
"$host/api/v3/command" | |
done | |
fi | |
# Move subtitles back | |
if ls "$tmp_subtitle_dir"/*.srt &> /dev/null; then | |
mv "$tmp_subtitle_dir"/*.srt "$input_dir" | |
fi | |
rm -rf "$tmp_subtitle_dir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment