mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-11 14:33:47 -05:00
Working on Audio. Added XLD. Added a bunch of other stuff
This commit is contained in:
123
bin/convertMedia
123
bin/convertMedia
@@ -53,6 +53,7 @@ debug=0
|
|||||||
safeRun=0
|
safeRun=0
|
||||||
downsize720=0
|
downsize720=0
|
||||||
deleteOriginal=0
|
deleteOriginal=0
|
||||||
|
XLD=0
|
||||||
args=()
|
args=()
|
||||||
|
|
||||||
# Set Temp Directory
|
# Set Temp Directory
|
||||||
@@ -147,7 +148,7 @@ function identifyUserFile() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function ffmpegCommand() {
|
function doConvert() {
|
||||||
|
|
||||||
# Set the output name, format, and directory
|
# Set the output name, format, and directory
|
||||||
# ###############################################
|
# ###############################################
|
||||||
@@ -188,18 +189,32 @@ function ffmpegCommand() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Invoke the conversion
|
||||||
|
# ##################################
|
||||||
|
|
||||||
|
# Use XLD for audio file conversion if available
|
||||||
|
if [[ "$XLD" == "1" ]]; then
|
||||||
|
# Respect --safe flag.
|
||||||
|
if [[ "${safeRun}" == "1" ]]; then
|
||||||
|
notice "xld -o "${output}" ${audioConvertCommand} "${f}""
|
||||||
|
else
|
||||||
|
verbose "xld -o "${output}" ${audioConvertCommand} "${f}""
|
||||||
|
xld -o "${output}" ${audioConvertCommand} "${f}"
|
||||||
|
fi
|
||||||
|
else # Use ffmpeg when XLD is set to 0
|
||||||
# Respect --safe flag.
|
# Respect --safe flag.
|
||||||
if [[ "${safeRun}" == "1" ]]; then
|
if [[ "${safeRun}" == "1" ]]; then
|
||||||
notice "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}""
|
notice "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}""
|
||||||
else
|
else
|
||||||
verbose "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}""
|
verbose "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}""
|
||||||
ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} $audioConvertCommand "${output}"
|
ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}"
|
||||||
|
|
||||||
# delete original if requested
|
# delete original if requested
|
||||||
if [[ "${deleteOriginal}" == "1" ]]; then
|
if [[ "${deleteOriginal}" == "1" ]]; then
|
||||||
rm -f "${f}" && verbose "Deleting "${f}""
|
rm -f "${f}" && verbose "Deleting "${f}""
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -219,6 +234,8 @@ convertVideo() {
|
|||||||
|
|
||||||
# Output a JSON file for each video asset being parsed.
|
# Output a JSON file for each video asset being parsed.
|
||||||
ffprobe -v quiet -print_format json -show_format -show_streams "${f}" >> "${informationFile}"
|
ffprobe -v quiet -print_format json -show_format -show_streams "${f}" >> "${informationFile}"
|
||||||
|
# uncomment the line below for debugging. It will write a json file for each file in the source directory
|
||||||
|
# ffprobe -v quiet -print_format json -show_format -show_streams "${f}" >> "${f}.json"
|
||||||
|
|
||||||
# Read the necessary information from the JSON
|
# Read the necessary information from the JSON
|
||||||
format="$(jq -r ".format.format_long_name" "${informationFile}")"
|
format="$(jq -r ".format.format_long_name" "${informationFile}")"
|
||||||
@@ -235,7 +252,7 @@ convertVideo() {
|
|||||||
videoCodec="$(jq -r '.streams[1].codec_name' "${informationFile}")"
|
videoCodec="$(jq -r '.streams[1].codec_name' "${informationFile}")"
|
||||||
videoCodecLong="$(jq -r ".streams[1].codec_long_name" "${informationFile}")"
|
videoCodecLong="$(jq -r ".streams[1].codec_long_name" "${informationFile}")"
|
||||||
else
|
else
|
||||||
warning "Missing video information for '"$f"'. Inspect with 'ffprobe'."
|
warning "Missing video information for '"$f"'. Inspecting with 'ffprobe'."
|
||||||
ffprobe -v quiet -print_format json -show_format -show_streams "${f}"
|
ffprobe -v quiet -print_format json -show_format -show_streams "${f}"
|
||||||
safeExit
|
safeExit
|
||||||
fi
|
fi
|
||||||
@@ -250,7 +267,7 @@ convertVideo() {
|
|||||||
audioSampleRate="$(jq -r ".streams[1].sample_rate" "${informationFile}")"
|
audioSampleRate="$(jq -r ".streams[1].sample_rate" "${informationFile}")"
|
||||||
audioBitRate="$(jq -r ".streams[1].bit_rate" "${informationFile}")"
|
audioBitRate="$(jq -r ".streams[1].bit_rate" "${informationFile}")"
|
||||||
else
|
else
|
||||||
warning "Missing audio information for '"$f"'. Inspect with 'ffprobe'."
|
warning "Missing audio information for '"$f"'. Inspecting with 'ffprobe'."
|
||||||
ffprobe -v quiet -print_format json -show_format -show_streams "${f}"
|
ffprobe -v quiet -print_format json -show_format -show_streams "${f}"
|
||||||
safeExit
|
safeExit
|
||||||
fi
|
fi
|
||||||
@@ -402,7 +419,9 @@ convertVideo() {
|
|||||||
videoCommand="-c:v libx264 -crf 18 -preset slow"
|
videoCommand="-c:v libx264 -crf 18 -preset slow"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ffmpegCommand # Invoke FFMpeg
|
# Do the conversion
|
||||||
|
# ##########################
|
||||||
|
doConvert # Invoke FFMpeg
|
||||||
|
|
||||||
# Unset variables
|
# Unset variables
|
||||||
unset videoCodec
|
unset videoCodec
|
||||||
@@ -433,7 +452,6 @@ convertMusic() {
|
|||||||
fi
|
fi
|
||||||
test -f "${f}" || continue # Ensure that what we've found is a file
|
test -f "${f}" || continue # Ensure that what we've found is a file
|
||||||
extension="${f##*.}" # Grab file extension of input file
|
extension="${f##*.}" # Grab file extension of input file
|
||||||
success "format: $extension"
|
|
||||||
informationFile="${tmpDir}/${f}.json"
|
informationFile="${tmpDir}/${f}.json"
|
||||||
|
|
||||||
# JSON METADATA FOR EACH ASSET
|
# JSON METADATA FOR EACH ASSET
|
||||||
@@ -441,7 +459,8 @@ convertMusic() {
|
|||||||
|
|
||||||
# Output a JSON file for each video asset being parsed.
|
# Output a JSON file for each video asset being parsed.
|
||||||
ffprobe -v quiet -print_format json -show_format -show_streams "${f}" >> "${informationFile}"
|
ffprobe -v quiet -print_format json -show_format -show_streams "${f}" >> "${informationFile}"
|
||||||
ffprobe -v quiet -print_format json -show_format -show_streams "${f}" >> "${f}.json"
|
# uncomment the line below for debugging. It will write a json file for each file in the source directory
|
||||||
|
# ffprobe -v quiet -print_format json -show_format -show_streams "${f}" >> "${f}.json"
|
||||||
|
|
||||||
# Read the necessary information from the JSON
|
# Read the necessary information from the JSON
|
||||||
format="$(jq -r ".format.format_long_name" "${informationFile}")"
|
format="$(jq -r ".format.format_long_name" "${informationFile}")"
|
||||||
@@ -473,18 +492,20 @@ convertMusic() {
|
|||||||
# SET OUTPUT FORMAT
|
# SET OUTPUT FORMAT
|
||||||
# Default to 'mp4' for everything.
|
# Default to 'mp4' for everything.
|
||||||
# TODO - think through additional defaults
|
# TODO - think through additional defaults
|
||||||
##########################################################
|
# #########################################################
|
||||||
|
|
||||||
# if you wanted a default target format for a specific input format,
|
# if you wanted a default target format for a specific input format,
|
||||||
# you would put it here.
|
# you would put it here. Defaults to m4a for everything except ALAC files which
|
||||||
case "${format}" in
|
# default to FLAC.
|
||||||
'QuickTime / MOV') outputFormat='m4a' ;; # Convert Apple formats to ....
|
case "${audioCodec}" in
|
||||||
|
'flac') outputFormat='m4a' ;; # Convert Apple formats to ....
|
||||||
|
'alac') outputFormat='flac' ;; # Convert Apple formats to ...
|
||||||
*) outputFormat='m4a' ;;
|
*) outputFormat='m4a' ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# SET AUDIO INFORMATION
|
# SET AUDIO CODECS
|
||||||
# Copy audio in compatible formats. Re-encode audio when needed
|
# Copy audio in compatible formats. Re-encode audio when needed
|
||||||
################################################################
|
# ###############################################################
|
||||||
|
|
||||||
# Pick the best aac audio encoder
|
# Pick the best aac audio encoder
|
||||||
if $(ffmpeg -version | grep enable-libfdk-aac >/dev/null); then
|
if $(ffmpeg -version | grep enable-libfdk-aac >/dev/null); then
|
||||||
@@ -494,13 +515,82 @@ convertMusic() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# LOSSLESS CONVERSIONS
|
# LOSSLESS CONVERSIONS
|
||||||
|
# Performed on any files over 320k
|
||||||
|
#
|
||||||
|
# note on XLD: If you have XLD installed and configured, lossless audio conversion
|
||||||
|
# will be run using built-in XLD profiles. You can disable this by
|
||||||
|
# changing ensuring that XLD=0 in sections below.
|
||||||
|
# #############################################################
|
||||||
|
|
||||||
# FLAC TO ALAC
|
# ffmpeg: FLAC TO ALAC
|
||||||
if [[ "${formatName}" == "flac" ]] && [[ "${formatBit_Rate}" -gt "320000" ]]; then
|
if [[ "${audioCodec}" == "flac" ]] && [[ "${formatBit_Rate}" -gt "320000" ]]; then
|
||||||
|
if type_exists "xld"; then
|
||||||
|
XLD=1
|
||||||
|
# audioConvertCommand="--profile FLACtoALAC"
|
||||||
|
audioConvertCommand="-f alac"
|
||||||
|
else
|
||||||
audioConvertCommand="-acodec alac"
|
audioConvertCommand="-acodec alac"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
ffmpegCommand # Run the conversion function
|
# ffmpeg: ALAC TO FLAC
|
||||||
|
if [[ "${audioCodec}" == "alac" ]] && [[ "${formatBit_Rate}" -gt "320000" ]]; then
|
||||||
|
if type_exists "xld"; then
|
||||||
|
XLD=1
|
||||||
|
audioConvertCommand="-f flac"
|
||||||
|
else
|
||||||
|
audioConvertCommand="-c:a flac"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# NON-LOSSLESS CONVERSIONS
|
||||||
|
# ##############################################
|
||||||
|
|
||||||
|
# Define the codec to be used
|
||||||
|
case "${audioCodec}" in
|
||||||
|
'flac') conversionAudioCodec='aac' ;; # Convert Apple formats to ....
|
||||||
|
'mp3') conversionAudioCodec='aac' ;; # Convert Apple formats to ....
|
||||||
|
'alac') conversionAudioCodec='flac' ;; # Convert Apple formats to ...
|
||||||
|
'aac') conversionAudioCodec='aac' ;; # Convert Apple formats to ....
|
||||||
|
*) conversionAudioCodec='aac' ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Take user specified bitrate
|
||||||
|
if [ -n "$bitrate"]; then
|
||||||
|
bitrate="${bitrate%k}/" # Ensure 'k' is at the end of any bitrate sent to ffmpeg
|
||||||
|
ffmpefBitrate="-b:a $bitrate"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# Convert to aac
|
||||||
|
if [[ "$conversionAudioCodec" == "aac" ]]; then
|
||||||
|
if type_exists "xld"; then
|
||||||
|
XLD=1
|
||||||
|
audioConvertCommand="-f aac"
|
||||||
|
else
|
||||||
|
audioConvertCommand="-acodec ${aacencoder}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Convert to mp3
|
||||||
|
# Can we convert to mp3
|
||||||
|
if $(ffmpeg -version | grep enable-libmp3lame >/dev/null); then
|
||||||
|
mp3Encoder='libmp3lame'
|
||||||
|
else
|
||||||
|
warning "No workable ffmpeg mp3 encoder. Skipping ${f}..."
|
||||||
|
breakLoop
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if [[ "$conversionAudioCodec" == "mp3" ]]; then
|
||||||
|
audioConvertCommand="-acodec ${mp3Encoder} ${ffmpefBitrate}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Do the conversion
|
||||||
|
# ##########################
|
||||||
|
doConvert # Run the conversion function
|
||||||
|
|
||||||
# Unset variables
|
# Unset variables
|
||||||
unset format
|
unset format
|
||||||
@@ -631,6 +721,7 @@ while [[ $1 = -?* ]]; do
|
|||||||
--downsize720) downsize720=1 ;;
|
--downsize720) downsize720=1 ;;
|
||||||
--delete) deleteOriginal=1 ;;
|
--delete) deleteOriginal=1 ;;
|
||||||
--saveDir) shift; saveDir="$1" ;;
|
--saveDir) shift; saveDir="$1" ;;
|
||||||
|
--bitrate) shift; bitrate="$1" ;;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user