2021-03-06 12:54:52 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# ks-mp3-album (ks-tools) - Convert folder album to MP3 Audio Format #
|
2022-02-04 17:07:26 +01:00
|
|
|
# Date: 04-02-2022 #
|
2021-03-06 12:54:52 +01:00
|
|
|
# Author: q3aql #
|
2022-01-02 17:20:24 +01:00
|
|
|
# Contact: q3aql@duck.com #
|
2021-03-06 12:54:52 +01:00
|
|
|
######################################################################
|
2023-02-10 13:20:13 +01:00
|
|
|
VERSION="8.4-dev"
|
2022-02-04 17:07:26 +01:00
|
|
|
M_DATE="040222"
|
2021-03-06 12:54:52 +01:00
|
|
|
|
|
|
|
# Variables
|
|
|
|
tempFile="/tmp/ks-mp4s-folder.txt"
|
|
|
|
tempFileTest="/tmp/ks-mp4s-folder-test.txt"
|
2021-03-08 00:28:27 +01:00
|
|
|
formatFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob ogg oga mp3 mp2 aac ac3 flac opus wav"
|
2021-03-06 12:54:52 +01:00
|
|
|
|
|
|
|
# Global parameters
|
|
|
|
dirTemp="/tmp"
|
|
|
|
listTemp="ks-tools.list"
|
|
|
|
ksToolsTempFolder="/tmp/ks-tools"
|
2022-01-02 12:17:53 +01:00
|
|
|
configFolder=${HOME}/.ks-tools
|
|
|
|
configFile=${configFolder}/ks-mp3
|
2021-03-06 12:54:52 +01:00
|
|
|
|
|
|
|
# Basic parameters
|
|
|
|
acodec="libmp3lame"
|
|
|
|
b_acodec="128k"
|
|
|
|
v_ext="mp3"
|
|
|
|
track_init="1"
|
|
|
|
artist_init="Artist"
|
|
|
|
default_lang_audio="spa"
|
|
|
|
|
2022-01-02 17:20:24 +01:00
|
|
|
# Check cygwin alias (for Windows)
|
|
|
|
if [ -f "/usr/bin/cygwin-alias.sh" ] ; then
|
|
|
|
shopt -s expand_aliases
|
|
|
|
source "/usr/bin/cygwin-alias.sh"
|
|
|
|
fi
|
|
|
|
|
2022-01-02 12:17:53 +01:00
|
|
|
# Create inicial config file
|
|
|
|
mkdir -p ${configFolder}
|
|
|
|
if [ -f ${configFile} ] ; then
|
|
|
|
exist_acodec=$(cat ${configFile} | grep "acodec" | cut -c2 | cut -d "_" -f 1)
|
|
|
|
exist_b_acodec=$(cat ${configFile} | grep "b_acodec=")
|
|
|
|
exist_default_lang_audio=$(cat ${configFile} | grep "default_lang_audio=")
|
|
|
|
exist_v_ext=$(cat ${configFile} | grep "v_ext=")
|
|
|
|
if [ -z ${exist_acodec} ] ; then
|
|
|
|
echo "acodec=${acodec}" >> ${configFile}
|
|
|
|
fi
|
|
|
|
if [ -z ${exist_b_acodec} ] ; then
|
|
|
|
echo "b_acodec=${b_acodec}" >> ${configFile}
|
|
|
|
fi
|
|
|
|
if [ -z ${exist_default_lang_audio} ] ; then
|
|
|
|
echo "default_lang_audio=${default_lang_audio}" >> ${configFile}
|
|
|
|
fi
|
|
|
|
if [ -z ${exist_v_ext} ] ; then
|
|
|
|
echo "v_ext=${v_ext}" >> ${configFile}
|
|
|
|
fi
|
|
|
|
source ${configFile}
|
|
|
|
else
|
|
|
|
echo "#!/bin/bash" > ${configFile}
|
|
|
|
echo "" >> ${configFile}
|
|
|
|
echo "acodec=${acodec}" >> ${configFile}
|
|
|
|
echo "b_acodec=${b_acodec}" >> ${configFile}
|
|
|
|
echo "default_lang_audio=${default_lang_audio}" >> ${configFile}
|
|
|
|
echo "v_ext=${v_ext}" >> ${configFile}
|
|
|
|
source ${configFile}
|
|
|
|
fi
|
|
|
|
|
2021-03-06 12:54:52 +01:00
|
|
|
# Check if ffmpeg is installed
|
|
|
|
ffmpeg_test=$(ffmpeg --help 2>&1)
|
|
|
|
error_ffmpeg=$?
|
|
|
|
if [ ${error_ffmpeg} -ne 0 ] ; then
|
2021-03-28 01:56:41 +01:00
|
|
|
echo ""
|
|
|
|
echo "* ks-mp3-album (ks-tools) v${VERSION} (${M_DATE})"
|
|
|
|
echo ""
|
|
|
|
echo "+ The 'ffmpeg' tool is not installed!"
|
|
|
|
echo ""
|
|
|
|
exit
|
2021-03-06 12:54:52 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Funcion to show the name of file/folder from full path
|
2021-03-22 19:01:31 +01:00
|
|
|
# Syntax: extractFolderOrFile <full/path/file.txt>
|
2021-03-06 12:54:52 +01:00
|
|
|
function extractFolderOrFile() {
|
2021-03-28 01:56:41 +01:00
|
|
|
pathToExtract="${1}/"
|
|
|
|
findFolder=0
|
|
|
|
count=1
|
|
|
|
nameFolder=$(echo ${pathToExtract} | cut -d "/" -f ${count})
|
|
|
|
count=$(expr $count + 1)
|
|
|
|
while [ ${findFolder} -eq 0 ] ; do
|
|
|
|
nameFolderTemp=$(echo ${pathToExtract} | cut -d "/" -f ${count})
|
|
|
|
if [ -z "${nameFolderTemp}" ] ; then
|
|
|
|
findFolder=1
|
|
|
|
else
|
|
|
|
nameFolder="${nameFolderTemp}"
|
|
|
|
count=$(expr $count + 1)
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "${nameFolder}"
|
2021-03-06 12:54:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to show files with spaces.
|
2021-03-22 19:01:31 +01:00
|
|
|
# Syntax: showFileWithSpace <file number>
|
2021-03-06 12:54:52 +01:00
|
|
|
function showFileWithSpace() {
|
2021-03-28 01:56:41 +01:00
|
|
|
echo "${1}" > ${dirTemp}/name.tmp
|
|
|
|
sed -i 's/_/ /g' ${dirTemp}/name.tmp
|
|
|
|
DisplayName=$(cat ${dirTemp}/name.tmp)
|
|
|
|
rm -rf ${dirTemp}/name.tmp
|
|
|
|
cutDisplayName=$(echo ${DisplayName} | cut -c1)
|
|
|
|
if [ "${cutDisplayName}" == "-" ] ; then
|
|
|
|
DisplayName=$(echo ${DisplayName} | cut -c2-100)
|
|
|
|
else
|
|
|
|
cutDisplayName=$(echo ${DisplayName} | cut -c2)
|
|
|
|
if [ "${cutDisplayName}" == "-" ] ; then
|
|
|
|
DisplayName=$(echo ${DisplayName} | cut -c3-100)
|
|
|
|
else
|
|
|
|
cutDisplayName=$(echo ${DisplayName} | cut -c3)
|
|
|
|
if [ "${cutDisplayName}" == "-" ] ; then
|
|
|
|
DisplayName=$(echo ${DisplayName} | cut -c4-100)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
echo ${DisplayName}
|
2021-03-06 12:54:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to remove extension from file
|
2021-03-22 19:01:31 +01:00
|
|
|
# Syntax: removeExtension "<text>"
|
2021-03-06 12:54:52 +01:00
|
|
|
function removeExtension() {
|
2021-03-28 01:56:41 +01:00
|
|
|
wordToConvert=${1}
|
|
|
|
ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt"
|
|
|
|
mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null
|
|
|
|
echo "${wordToConvert}" > ${ksToolsSedFile}
|
|
|
|
# Remove extensions
|
|
|
|
sed -i 's/.avi//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.mp4//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.mkv//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i "s/.mov//g" "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.vob//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.mpg//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.mpeg//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.wmv//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.ogv//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.webm//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.flv//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.mp3//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.mp2//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.oga//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.ogg//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.ac3//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.aac//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.flac//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.wav//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
sed -i 's/.opus//g' "${ksToolsSedFile}" &> /dev/null
|
|
|
|
# Show file without extension
|
|
|
|
wordToConvert=$(cat ${ksToolsSedFile})
|
|
|
|
echo ${wordToConvert}
|
2021-03-06 12:54:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 19:01:31 +01:00
|
|
|
# Syntax: ks-mp3-album </absolute/path/album/
|
2021-03-06 12:54:52 +01:00
|
|
|
if [ -z "${1}" ] ; then
|
2021-03-28 01:56:41 +01:00
|
|
|
echo ""
|
|
|
|
echo "* ks-mp3-album (ks-tools) v${VERSION} (${M_DATE})"
|
|
|
|
echo ""
|
|
|
|
echo "- Convert folder album to MP3 Audio Format"
|
|
|
|
echo ""
|
2022-01-02 12:17:53 +01:00
|
|
|
echo "+ Config: ${configFile}"
|
2021-03-28 01:56:41 +01:00
|
|
|
echo ""
|
|
|
|
echo " - Audio codec: ${acodec}"
|
|
|
|
echo " - Bitrate audio: ${b_acodec}"
|
|
|
|
echo " - Container: ${v_ext}"
|
|
|
|
echo ""
|
|
|
|
echo "+ Syntax: "
|
|
|
|
echo ""
|
|
|
|
echo " $ ks-mp3-album </absolute/path/album>"
|
|
|
|
echo ""
|
|
|
|
echo " + Example: ks-mp3-album /data/albums/matrix-soundtrack"
|
|
|
|
echo ""
|
|
|
|
exit
|
2021-03-06 12:54:52 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if folder exist
|
|
|
|
if [ -d "${1}" ] ; then
|
2021-03-28 01:56:41 +01:00
|
|
|
echo null > /dev/null
|
2021-03-06 12:54:52 +01:00
|
|
|
else
|
2021-03-28 01:56:41 +01:00
|
|
|
echo ""
|
|
|
|
echo "* ks-mp3-album (ks-tools) v${VERSION} (${M_DATE})"
|
|
|
|
echo ""
|
|
|
|
echo "+ The folder '${1}' does not exist!"
|
|
|
|
echo ""
|
|
|
|
exit
|
2021-03-06 12:54:52 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Scan videos/audio files format
|
|
|
|
clear
|
|
|
|
echo ""
|
|
|
|
echo "* ks-mp3-album (ks-tools) v${VERSION} (${M_DATE})"
|
|
|
|
echo ""
|
|
|
|
echo "- Convert folder album to MP3 Audio Format"
|
|
|
|
echo ""
|
|
|
|
echo -n "* Scanning ${1} " && sleep 4
|
|
|
|
echo ""
|
|
|
|
rm -rf ${tempFile} && touch ${tempFile}
|
|
|
|
for format in ${formatFiles} ; do
|
2021-03-28 01:56:41 +01:00
|
|
|
find "${1}"/*.${format} &> ${tempFileTest}
|
|
|
|
if [ $? -ne 0 ] ; then
|
|
|
|
echo "null" > /dev/null
|
|
|
|
else
|
|
|
|
echo "+ Video file(s) in .${format} found!"
|
|
|
|
cd "${1}" && ls -1 *.${format} &>> ${tempFile}
|
|
|
|
fail=0
|
|
|
|
fi
|
2021-03-06 12:54:52 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
# Set the global metadata album
|
|
|
|
current_date=$(date +%Y)
|
|
|
|
echo ""
|
|
|
|
echo "* Common METADATA configuration:"
|
|
|
|
echo ""
|
|
|
|
echo -n "* (Default: Album-Disc) Type name of album: " ; read album_file
|
|
|
|
if [ -z "${album_file}" ] ; then
|
2021-03-28 01:56:41 +01:00
|
|
|
album_title="Album-Disc"
|
2021-03-06 12:54:52 +01:00
|
|
|
else
|
2021-03-28 01:56:41 +01:00
|
|
|
album_title="${album_file}"
|
2021-03-06 12:54:52 +01:00
|
|
|
fi
|
|
|
|
echo -n "* (Default: ${current_date}) Type the year of album: " ; read year_file
|
|
|
|
if [ -z "${year_file}" ] ; then
|
2021-03-28 01:56:41 +01:00
|
|
|
year_num="${current_date}"
|
2021-03-06 12:54:52 +01:00
|
|
|
else
|
2021-03-28 01:56:41 +01:00
|
|
|
year_num="${year_file}"
|
2021-03-06 12:54:52 +01:00
|
|
|
fi
|
|
|
|
echo -n "* (Default: Pop) Type the genre of album: " ; read genre_file
|
|
|
|
if [ -z "${genre_file}" ] ; then
|
2021-03-28 01:56:41 +01:00
|
|
|
genre_file="Pop"
|
2021-03-06 12:54:52 +01:00
|
|
|
else
|
2021-03-28 01:56:41 +01:00
|
|
|
genre_file="${genre_file}"
|
2021-03-06 12:54:52 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Init the conversion files
|
|
|
|
convert_files=1
|
|
|
|
num_files=$(cat ${tempFile} | wc -l)
|
|
|
|
if [ ${num_files} -eq 0 ] ; then
|
2021-03-28 01:56:41 +01:00
|
|
|
echo ""
|
|
|
|
echo "+ No video/audio file(s) found in folder '${1}"
|
|
|
|
echo ""
|
|
|
|
exit
|
2021-03-06 12:54:52 +01:00
|
|
|
else
|
2021-03-28 01:56:41 +01:00
|
|
|
echo ""
|
|
|
|
echo "* List of files:"
|
|
|
|
echo ""
|
|
|
|
while [ ${convert_files} -le ${num_files} ] ; do
|
|
|
|
show_file=$(cat ${tempFile} | head -${convert_files} | tail -1)
|
|
|
|
echo " + ${show_file}"
|
|
|
|
convert_files=$(expr ${convert_files} + 1)
|
|
|
|
done
|
|
|
|
echo ""
|
|
|
|
echo "* The output folder will be '${1}/to-mp3'"
|
|
|
|
# Execute commands for conversion
|
|
|
|
echo ""
|
|
|
|
echo -n "* (Default: y) Do you want run the conversion? (y/n): " ; read run_commands_ffmpeg
|
|
|
|
if [ "${run_commands_ffmpeg}" == "n" ] ; then
|
|
|
|
exit
|
|
|
|
else
|
|
|
|
convert_files=1
|
|
|
|
cd "${1}" && mkdir -p to-mp3 && rm -rf to-mp3/*
|
|
|
|
while [ ${convert_files} -le ${num_files} ] ; do
|
|
|
|
show_file=$(cat ${tempFile} | head -${convert_files} | tail -1)
|
|
|
|
fname=$(echo "${show_file}" | cut -d "." -f 1)
|
|
|
|
name_title_default=$(removeExtension "${show_file}")
|
|
|
|
name_title_default=$(showFileWithSpace "${name_title_default}")
|
|
|
|
echo ""
|
|
|
|
echo "* METADATA for '${show_file}'"
|
|
|
|
echo ""
|
|
|
|
echo " - Album: ${album_title}"
|
|
|
|
echo " - Year: ${year_num}"
|
|
|
|
echo " - Genre: ${genre_file}"
|
|
|
|
echo ""
|
|
|
|
echo "* Information of '${show_file}':"
|
|
|
|
echo ""
|
|
|
|
echo "+ Audio Tracks:"
|
|
|
|
echo ""
|
|
|
|
ffmpeg -i "${show_file}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1
|
|
|
|
echo ""
|
|
|
|
# Check the audio track by default
|
|
|
|
audio_default=$(ffmpeg -i "${show_file}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "(${default_lang_audio})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1)
|
|
|
|
if [ -z "${audio_default}" ] ; then
|
|
|
|
audio_default=$(ffmpeg -i "${show_file}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1)
|
|
|
|
if [ -z "${audio_default}" ] ; then
|
|
|
|
audio_default="0:1"
|
|
|
|
else
|
|
|
|
audio_default_patch=$(echo ${audio_default} | cut -c4)
|
|
|
|
if [ "${audio_default_patch}" == ":" ] ; then
|
|
|
|
audio_default=$(echo ${audio_default} | cut -c1-3)
|
|
|
|
else
|
|
|
|
audio_default="${audio_default}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
audio_default_patch=$(echo ${audio_default} | cut -c4)
|
|
|
|
if [ "${audio_default_patch}" == ":" ] ; then
|
|
|
|
audio_default=$(echo ${audio_default} | cut -c1-3)
|
|
|
|
else
|
|
|
|
audio_default="${audio_default}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
# Ask for audio
|
|
|
|
echo -n "* (Default: ${audio_default}) Type the number of audio track: " ; read audio_track
|
|
|
|
if [ -z "${audio_track}" ] ; then
|
|
|
|
audio_track="${audio_default}"
|
|
|
|
else
|
|
|
|
audio_track="${audio_track}"
|
|
|
|
fi
|
|
|
|
echo -n "* (Default: ${name_title_default}) Type name of title: " ; read name_title
|
|
|
|
if [ -z "${name_title}" ] ; then
|
|
|
|
name_title="${name_title_default}"
|
|
|
|
else
|
|
|
|
name_title="${name_title}"
|
|
|
|
fi
|
|
|
|
echo -n "* (Default: ${track_init}) Type number of track: " ; read track_file
|
|
|
|
if [ -z "${track_file}" ] ; then
|
|
|
|
track_num="${track_init}"
|
|
|
|
else
|
|
|
|
track_num="${track_file}"
|
|
|
|
fi
|
|
|
|
echo -n "* (Default: ${artist_init}) Type name of artist: " ; read artist_file
|
|
|
|
if [ -z "${artist_file}" ] ; then
|
|
|
|
artist_title="${artist_init}"
|
|
|
|
else
|
|
|
|
artist_title="${artist_file}"
|
|
|
|
artist_init="${artist_file}"
|
|
|
|
fi
|
|
|
|
# Check audio 5.1 or 7.1
|
|
|
|
audio_5_7_1=$(${p_ffmpeg} "${show_file}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1")
|
|
|
|
audio_5_7_1_2=$(${p_ffmpeg} "${show_file}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "7.1")
|
|
|
|
audio_5_7_1="${audio_5_7_1}${audio_5_7_1_2}"
|
|
|
|
if [ -z "${audio_5_7_1}" ] ; then
|
|
|
|
stereo_params="-ac 2"
|
|
|
|
else
|
|
|
|
stereo_params="-ac 2 -clev 3dB -slev -6dB"
|
|
|
|
fi
|
|
|
|
ffmpeg -i "${show_file}" -map ${audio_track} -c:a ${acodec} -b:a ${b_acodec} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_num}" -metadata genre="${genre_file}" -metadata album="${album_title}" -metadata artist="${artist_title}" -metadata track="${track_num}" "to-mp3/${fname}.${v_ext}"
|
|
|
|
# Increment variables
|
|
|
|
convert_files=$(expr ${convert_files} + 1)
|
|
|
|
track_init=$(expr ${track_num} + 1)
|
|
|
|
done
|
|
|
|
fi
|
2021-03-06 12:54:52 +01:00
|
|
|
fi
|