mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-11 22:43:47 -05:00
Think we're ready to merge back into master
This commit is contained in:
174
bin/convertMedia
174
bin/convertMedia
@@ -149,26 +149,23 @@ function identifyUserFile() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function userFormat() {
|
||||||
|
# Reads user input for format (-o, --output)
|
||||||
|
# Override defaults with CLI
|
||||||
|
if [ -n "$userOutput" ]; then
|
||||||
|
outputFormat="${userOutput,,}"
|
||||||
|
fi
|
||||||
|
verbose "outputFormat=${outputFormat}"
|
||||||
|
}
|
||||||
|
|
||||||
function doConvert() {
|
function doConvert() {
|
||||||
|
|
||||||
# Set the output name, format, and directory
|
# Set the output name, format, and directory
|
||||||
# ###############################################
|
# ###############################################
|
||||||
|
|
||||||
# Override defaults with CLI
|
|
||||||
if [[ -n "$userOutput" ]]; then
|
|
||||||
outputFormat="${userOutput}"
|
|
||||||
fi
|
|
||||||
verbose "outputFormat=${outputFormat}"
|
|
||||||
|
|
||||||
# Set output filename
|
# Set output filename
|
||||||
output="$(basename "${f%.*}").$outputFormat" && verbose "output="${output}""
|
output="$(basename "${f%.*}").$outputFormat" && verbose "output="${output}""
|
||||||
|
|
||||||
# Don't convert to self if no other options set
|
|
||||||
if [[ -z $height && -z $width && -z $videoSize && $downsize720 == "0" && "$outputFormat" == "$extension" ]]; then
|
|
||||||
warning "Can't convert a '"${extension}"' to itself. Skipping all '"${extension}"' files."
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Add users output save directory if used
|
# Add users output save directory if used
|
||||||
if [[ -n "${outputDir}" ]]; then
|
if [[ -n "${outputDir}" ]]; then
|
||||||
output="${outputDir}${output}" && verbose "output=${outputDir}${output}"
|
output="${outputDir}${output}" && verbose "output=${outputDir}${output}"
|
||||||
@@ -185,6 +182,16 @@ function doConvert() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Respect the 'Quiet' flag
|
||||||
|
if [[ "${quiet}" == "1" ]]; then
|
||||||
|
ffquiet="-loglevel quiet"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# # Respect the 'logfile' flag
|
||||||
|
# if [[ "${printLog}" == "1" ]]; then
|
||||||
|
# ffmpegLog=">> ${logFile}"
|
||||||
|
# fi
|
||||||
|
|
||||||
# Invoke the conversion
|
# Invoke the conversion
|
||||||
# ##################################
|
# ##################################
|
||||||
|
|
||||||
@@ -201,10 +208,10 @@ function doConvert() {
|
|||||||
else # Use ffmpeg when XLD is set to 0
|
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}" ${ffquiet}"
|
||||||
else
|
else
|
||||||
verbose "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}""
|
verbose "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}" ${ffquiet}"
|
||||||
ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}"
|
ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${videoAudioCommand} ${audioConvertCommand} "${output}" ${ffquiet}
|
||||||
|
|
||||||
# delete original if requested
|
# delete original if requested
|
||||||
if [[ "${deleteOriginal}" == "1" ]]; then
|
if [[ "${deleteOriginal}" == "1" ]]; then
|
||||||
@@ -421,6 +428,13 @@ convertVideo() {
|
|||||||
|
|
||||||
# Do the conversion
|
# Do the conversion
|
||||||
# ##########################
|
# ##########################
|
||||||
|
|
||||||
|
# Don't convert to self if no other options set
|
||||||
|
if [[ -z $height && -z $width && -z $videoSize && $downsize720 == "0" && "$outputFormat" == "$extension" ]]; then
|
||||||
|
warning "Can't convert a '"${extension}"' to itself. Skipping all '"${extension}"' files."
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
doConvert # Invoke FFMpeg
|
doConvert # Invoke FFMpeg
|
||||||
|
|
||||||
# Unset variables
|
# Unset variables
|
||||||
@@ -454,6 +468,11 @@ convertMusic() {
|
|||||||
extension="${f##*.}" # Grab file extension of input file
|
extension="${f##*.}" # Grab file extension of input file
|
||||||
informationFile="${tmpDir}/${f}.json"
|
informationFile="${tmpDir}/${f}.json"
|
||||||
|
|
||||||
|
# For audio files, ensure that the user specifies an output format
|
||||||
|
if [[ -z ${userOutput} ]]; then
|
||||||
|
die "Please specify an output audio format using '-o, --output'"
|
||||||
|
fi
|
||||||
|
|
||||||
# JSON METADATA FOR EACH ASSET
|
# JSON METADATA FOR EACH ASSET
|
||||||
######################################################################
|
######################################################################
|
||||||
verbose "Reading audio data and writing JSON to tmp"
|
verbose "Reading audio data and writing JSON to tmp"
|
||||||
@@ -490,107 +509,71 @@ convertMusic() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# SET OUTPUT FORMAT
|
|
||||||
# Default to 'mp4' for everything.
|
|
||||||
# TODO - think through additional defaults
|
|
||||||
# #########################################################
|
|
||||||
|
|
||||||
# if you wanted a default target format for a specific input format,
|
|
||||||
# you would put it here. Defaults to m4a for everything except ALAC files which
|
|
||||||
# default to FLAC.
|
|
||||||
case "${audioCodec}" in
|
|
||||||
'flac') outputFormat='m4a' ;; # Convert Apple formats to ....
|
|
||||||
'alac') outputFormat='flac' ;; # Convert Apple formats to ...
|
|
||||||
*) outputFormat='m4a' ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# SET AUDIO CODECS
|
|
||||||
# Copy audio in compatible formats. Re-encode audio when needed
|
|
||||||
# ###############################################################
|
|
||||||
|
|
||||||
# Pick the best aac audio encoder
|
|
||||||
if $(ffmpeg -version | grep enable-libfdk-aac >/dev/null); then
|
|
||||||
aacEncoder='libfdk_aac'
|
|
||||||
else
|
|
||||||
aacencoder='libfaac'
|
|
||||||
fi
|
|
||||||
|
|
||||||
# LOSSLESS CONVERSIONS
|
|
||||||
# Performed on any files over 320k
|
|
||||||
#
|
|
||||||
# note on XLD: If you have XLD installed and configured, lossless audio conversion
|
# 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
|
# will be run using built-in XLD profiles. You can disable this by
|
||||||
# changing ensuring that XLD=0 in sections below.
|
# changing ensuring that XLD=0 in sections below.
|
||||||
|
#
|
||||||
# #############################################################
|
# #############################################################
|
||||||
|
|
||||||
# ffmpeg: FLAC TO ALAC
|
# Build the Conversion Command
|
||||||
if [[ "${audioCodec}" == "flac" ]] && [[ "${formatBit_Rate}" -gt "320000" ]]; then
|
# ########################################
|
||||||
if type_exists "xld"; then
|
|
||||||
|
# FLAC TO ALAC
|
||||||
|
if [[ "${userOutput,,}" == "alac" ]]; then
|
||||||
|
if type_exists "xlds"; then
|
||||||
XLD=1
|
XLD=1
|
||||||
# audioConvertCommand="--profile FLACtoALAC"
|
# audioConvertCommand="--profile FLACtoALAC"
|
||||||
audioConvertCommand="-f alac"
|
audioConvertCommand="-f alac" && verbose "Using XLD. audioConvertCommand = -f alac"
|
||||||
else
|
else
|
||||||
audioConvertCommand="-acodec alac"
|
audioConvertCommand="-acodec alac" && verbose "Using ffmpeg. audioConvertCommand = -acodec alac"
|
||||||
|
outputFormat="m4a"
|
||||||
fi
|
fi
|
||||||
fi
|
elif [[ "${userOutput,,}" == "flac" ]]; then
|
||||||
|
if type_exists "xlds"; then
|
||||||
# ffmpeg: ALAC TO FLAC
|
|
||||||
if [[ "${audioCodec}" == "alac" ]] && [[ "${formatBit_Rate}" -gt "320000" ]]; then
|
|
||||||
if type_exists "xld"; then
|
|
||||||
XLD=1
|
XLD=1
|
||||||
audioConvertCommand="-f flac"
|
audioConvertCommand="-f flac" && verbose "Using XLD. audioConvertCommand = -f flac"
|
||||||
else
|
else
|
||||||
audioConvertCommand="-c:a flac"
|
audioConvertCommand="-c:a flac" && verbose "Using ffmpeg. audioConvertCommand = -c:a flac"
|
||||||
fi
|
fi
|
||||||
fi
|
elif [[ "${userOutput,,}" == "aac" || "${userOutput,,}" == "m4a" ]]; then
|
||||||
|
# Pick the best aac audio encoder
|
||||||
# NON-LOSSLESS CONVERSIONS
|
if $(ffmpeg -version | grep enable-libfdk-aac >/dev/null); then
|
||||||
# ##############################################
|
# set variable bit rate to '5', the highest
|
||||||
|
aacEncoder='libfdk_aac -vbr 5' && verbose "aacEncoder = libfdk_aac -vbr 5"
|
||||||
# Define the codec to be used
|
else
|
||||||
case "${audioCodec}" in
|
aacEncoder='libfaac -q:a 400' && verbose "aacEncoder = libfaac -q:a 400"
|
||||||
'flac') conversionAudioCodec='aac' ;; # Convert Apple formats to ....
|
fi
|
||||||
'mp3') conversionAudioCodec='aac' ;; # Convert Apple formats to ....
|
if type_exists "xlds"; then
|
||||||
'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
|
XLD=1
|
||||||
audioConvertCommand="-f aac"
|
audioConvertCommand="-f aac" && verbose "using xld. audioConvertCommand = -f aac "
|
||||||
else
|
else
|
||||||
audioConvertCommand="-acodec ${aacencoder}"
|
audioConvertCommand="-acodec ${aacEncoder}" && verbose "using ffmpeg. audioConvertCommand = -acodec ${aacEncoder}"
|
||||||
|
fi
|
||||||
|
elif [[ "${userOutput,,}" == "mp3" ]]; then
|
||||||
|
# Can we convert to mp3? Do we have an ffmpeg encoder?
|
||||||
|
if $(ffmpeg -version | grep enable-libmp3lame >/dev/null); then
|
||||||
|
mp3Encoder='libmp3lame' && verbose "mp3Encoder = libmp3lame"
|
||||||
|
else
|
||||||
|
warning "No workable ffmpeg mp3 encoder. Skipping ${f}..."
|
||||||
|
breakLoop
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
# Take user specified bitrate
|
||||||
|
if [ -n "$bitrate" ]; then
|
||||||
|
bitrate="${bitrate%k}k" # Ensure 'k' is at the end of any bitrate sent to ffmpeg
|
||||||
|
ffmpegBitrate="-b:a $bitrate" && verbose "bitrate = ${bitrate}"
|
||||||
|
audioConvertCommand="-acodec ${mp3Encoder} ${ffmpegBitrate}" && verbose "audioConvertCommand = -acodec ${mp3Encoder} ${ffmpegBitrate}"
|
||||||
|
else
|
||||||
|
audioConvertCommand="-acodec ${mp3Encoder} -qscale:a 0" && verbose "audioConvertCommand = -acodec ${mp3Encoder} -qscale:a 0"
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
# Convert to mp3
|
|
||||||
# Can we convert to mp3
|
|
||||||
if $(ffmpeg -version | grep enable-libmp3lame >/dev/null); then
|
|
||||||
mp3Encoder='libmp3lame'
|
|
||||||
else
|
else
|
||||||
warning "No workable ffmpeg mp3 encoder. Skipping ${f}..."
|
die "Unknown audio conversion format: ${outputFormat}"
|
||||||
breakLoop
|
|
||||||
continue
|
|
||||||
fi
|
fi
|
||||||
if [[ "$conversionAudioCodec" == "mp3" ]]; then
|
|
||||||
audioConvertCommand="-acodec ${mp3Encoder} ${ffmpefBitrate}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Do the conversion
|
# Do the conversion
|
||||||
# ##########################
|
# ##########################
|
||||||
|
|
||||||
doConvert # Run the conversion function
|
doConvert # Run the conversion function
|
||||||
|
|
||||||
# Unset variables
|
# Unset variables
|
||||||
@@ -611,6 +594,7 @@ convertMusic() {
|
|||||||
# Run the functions
|
# Run the functions
|
||||||
checkDependencies
|
checkDependencies
|
||||||
identifyUserFile
|
identifyUserFile
|
||||||
|
userFormat
|
||||||
outputDir
|
outputDir
|
||||||
convertVideo
|
convertVideo
|
||||||
convertMusic
|
convertMusic
|
||||||
|
|||||||
Reference in New Issue
Block a user