mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-08 13:13:47 -05:00
Added user selected directory for output files
This commit is contained in:
@@ -104,6 +104,23 @@ function breakLoop() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function outputDir() {
|
||||||
|
if [[ -n "${saveDir}" ]]; then
|
||||||
|
if [[ -e "${saveDir}" && ! -d "${saveDir}" ]]; then
|
||||||
|
die "${saveDir} exists but is not a directory"
|
||||||
|
fi
|
||||||
|
if [[ ! -d "${saveDir}" ]]; then
|
||||||
|
seek_confirmation "${saveDir} does not exist. Create?"
|
||||||
|
if is_confirmed; then
|
||||||
|
mkdir "${saveDir}" && verbose "mkdir ${saveDir}"
|
||||||
|
else
|
||||||
|
die "Can't run without a place to save the files."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
outputDir="${saveDir%/}/" # remove trailing slash if included. Add back to ensure it's always there.
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
convert() {
|
convert() {
|
||||||
if [ -n "${MEDIATYPE}" ]; then
|
if [ -n "${MEDIATYPE}" ]; then
|
||||||
videoTypes=($MEDIATYPE) # Reset the video type array to user-input, if specified
|
videoTypes=($MEDIATYPE) # Reset the video type array to user-input, if specified
|
||||||
@@ -116,6 +133,7 @@ convert() {
|
|||||||
f="${userFile}"
|
f="${userFile}"
|
||||||
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
|
||||||
informationFile="${tmpDir}/${f}.json"
|
informationFile="${tmpDir}/${f}.json"
|
||||||
|
|
||||||
# JSON METADATA FOR EACH ASSET
|
# JSON METADATA FOR EACH ASSET
|
||||||
@@ -187,7 +205,7 @@ convert() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 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
|
||||||
##########################################################
|
##########################################################
|
||||||
@@ -205,8 +223,6 @@ convert() {
|
|||||||
output="$(basename "${f%.*}").$outputFormat"
|
output="$(basename "${f%.*}").$outputFormat"
|
||||||
verbose "output="${output}""
|
verbose "output="${output}""
|
||||||
|
|
||||||
extension="${f##*.}" # Grab file extension of input file
|
|
||||||
|
|
||||||
# Don't convert to self if no other options set
|
# Don't convert to self if no other options set
|
||||||
if [[ -z $height && -z $width && -z $videoSize && $downsize720 == "0" && "$outputFormat" == "$extension" ]]; then
|
if [[ -z $height && -z $width && -z $videoSize && $downsize720 == "0" && "$outputFormat" == "$extension" ]]; then
|
||||||
warning "Can't convert a '"${extension}"' to itself. Skipping all '"${extension}"' files."
|
warning "Can't convert a '"${extension}"' to itself. Skipping all '"${extension}"' files."
|
||||||
@@ -337,11 +353,23 @@ convert() {
|
|||||||
videoCommand="-c:v libx264 -crf 18 -preset slow"
|
videoCommand="-c:v libx264 -crf 18 -preset slow"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# CONVERT THE FILE
|
||||||
|
# ################################
|
||||||
|
|
||||||
|
# Add users output save directory if used
|
||||||
|
if [[ -n "${outputDir}" ]]; then
|
||||||
|
output="${outputDir}${output}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Confirm we're not overwriting an existing file
|
# Confirm we're not overwriting an existing file
|
||||||
if [ -e "$output" ]; then
|
if [ -e "$output" ]; then
|
||||||
seek_confirmation ""${output}" file already exists. Rename to '.new'?"
|
seek_confirmation ""${output}" file already exists. Rename to '.new'?"
|
||||||
if is_confirmed; then
|
if is_confirmed; then
|
||||||
output="$(basename "${f%.*}").new."${outputFormat}""
|
output="$(basename "${f%.*}").new."${outputFormat}""
|
||||||
|
if [[ -n "${outputDir}" ]]; then
|
||||||
|
output="${outputDir}${output}"
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
notice "Skipping...."
|
notice "Skipping...."
|
||||||
breakLoop
|
breakLoop
|
||||||
@@ -349,10 +377,6 @@ convert() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# CONVERT THE FILE
|
|
||||||
# ################################
|
|
||||||
|
|
||||||
# Respect --safe flag.
|
# Respect --safe flag.
|
||||||
if [[ "${safeRun}" == "1" ]]; then
|
if [[ "${safeRun}" == "1" ]]; then
|
||||||
notice "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${audioCommand} "${output}""
|
notice "ffmpeg -i "${f}" ${videoResize} ${videoCommand} ${audioCommand} "${output}""
|
||||||
@@ -390,6 +414,7 @@ convert() {
|
|||||||
|
|
||||||
# Run the functions
|
# Run the functions
|
||||||
checkDependencies
|
checkDependencies
|
||||||
|
outputDir
|
||||||
convert
|
convert
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
@@ -428,6 +453,8 @@ File Options:
|
|||||||
-o, --output Specify the output format for the file(s) to be converted to.
|
-o, --output Specify the output format for the file(s) to be converted to.
|
||||||
('mkv', 'mp4', 'm4a')
|
('mkv', 'mp4', 'm4a')
|
||||||
--delete Delete the original file after conversion.
|
--delete Delete the original file after conversion.
|
||||||
|
--saveDir Specify a folder for the converted files to be saved to. Defaults to
|
||||||
|
the directory the script is invoked in.
|
||||||
|
|
||||||
Video Specific Options:
|
Video Specific Options:
|
||||||
--size Set the size of the target file. Can be one of 'hd1080', 'hd720',
|
--size Set the size of the target file. Can be one of 'hd1080', 'hd720',
|
||||||
@@ -483,15 +510,16 @@ unset options
|
|||||||
# Read the options and set stuff
|
# Read the options and set stuff
|
||||||
while [[ $1 = -?* ]]; do
|
while [[ $1 = -?* ]]; do
|
||||||
case $1 in
|
case $1 in
|
||||||
-f|--file) shift; userFile=$1 ;;
|
-f|--file) shift; userFile="$1" ;;
|
||||||
-i|--input) shift; MEDIATYPE=$1 ;;
|
-i|--input) shift; MEDIATYPE="$1" ;;
|
||||||
-o|--output) shift; userOutput=$1 ;;
|
-o|--output) shift; userOutput="$1" ;;
|
||||||
--safe) safeRun=1 ;;
|
--safe) safeRun=1 ;;
|
||||||
--size) shift; videoSize=$1 ;;
|
--size) shift; videoSize="$1" ;;
|
||||||
--height) shift; height=$1 ;;
|
--height) shift; height="$1" ;;
|
||||||
--width) shift; width=$1 ;;
|
--width) shift; width="$1" ;;
|
||||||
--downsize720) downsize720=1 ;;
|
--downsize720) downsize720=1 ;;
|
||||||
--delete) deleteOriginal=1 ;;
|
--delete) deleteOriginal=1 ;;
|
||||||
|
--saveDir) shift; saveDir="$1" ;;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user