diff --git a/src/ks-avi b/src/ks-avi new file mode 100755 index 0000000..383f782 --- /dev/null +++ b/src/ks-avi @@ -0,0 +1,361 @@ +#!/bin/bash + +############################################################### +# ks-avi (ks-tools) - Convert video to AVI format # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +############################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +rel_size="720x480" +vcodec="libxvid" +b_vcodec="3000k" +acodec="libmp3lame" +b_acodec="192k" +default_lang_audio="spa" +default_lang_subt="spa" +v_ext="avi" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-avi (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + echo "${1}" > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Sintax: ks-avi +if [ -z "${1}" ] ; then + echo "" + echo "* ks-avi (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient AVI" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-avi " + echo "" + echo " + Example: ks-avi /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-avi (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" +p_ffmpeg_patched="-max_muxing_queue_size 9999" +f_conversion="-vsync 1 -async 1" +p_conversion="-s ${rel_size} -c:v ${vcodec} -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-avi (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient AVI" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-avi " + echo "" + echo " + Example: ks-avi /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Video Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + echo "+ Subtitle Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 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=$(${p_ffmpeg} "${1}" 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 + # Check the subtitle track by default + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | grep "(forced)" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default="0:3" + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + # Ask for subtitle + echo -n "* (Default: n) Do you want include subtitles? (y/n): " ; read subtitles_y_n + if [ "${subtitles_y_n}" == "y" ] ; then + echo -n "* (Default: ${subtitle_default}) Type the number of subtitle track: " ; read subtitle_track + if [ -z "${subtitle_track}" ] ; then + subtitle_track="${subtitle_default}" + else + subtitle_track="${subtitle_track}" + fi + fi + echo -n "* (Default: ${rel_size}) Type the resolution: " ; read resolution + if [ -z "${resolution}" ] ; then + resolution="${rel_size}" + else + rel_size="${resolution}" + p_conversion="-s ${rel_size} -c:v ${vcodec} -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + fi + echo -n "* (Default: n) Do you want apply '-max_muxing_queue_size 9999' patch? (y/n): " ; read patch_thread + if [ "${patch_thread}" == "y" ] ; then + patch_thread="y" + else + patch_thread="n" + fi + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + 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: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_file="${current_date}" + else + year_file="${year_file}" + fi + echo -n "* (Default: Unknown) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Unknown" + else + genre_file="${genre_file}" + fi + + # Check audio 5.1 or 7.1 + audio_5_7_1=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1") + audio_5_7_1_2=$(${p_ffmpeg} "${1}" 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 + + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + if [ "${subtitles_y_n}" == "y" ] ; then + echo " # Extract subtitles from file ${1}" + echo " ${p_ffmpeg} \"${1}\" -map ${subtitle_track} \"${2}.srt\"" + echo "" + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to AVI" + echo " ${p_ffmpeg} \"${1}\" -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to AVI" + echo " ${p_ffmpeg} \"${1}\" -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + else + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to AVI" + echo " ${p_ffmpeg} \"${1}\" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to AVI" + echo " ${p_ffmpeg} \"${1}\" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + # 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 + if [ "${subtitles_y_n}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${subtitle_track} "${2}.srt" + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + else + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + fi +fi diff --git a/src/ks-mix b/src/ks-mix new file mode 100755 index 0000000..744a50a --- /dev/null +++ b/src/ks-mix @@ -0,0 +1,536 @@ +#!/bin/bash + +##################################################################### +# ks-mix (ks-tools) - Extract video/audio and mix video/audio files # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +##################################################################### +VERSION="6.6" +M_DATE="100221" + +# File(s) detection +current_dir=$(pwd) +video_file_dir="${current_dir}/video.mkv" +audio_file_dir="${current_dir}/audio.mka" +subs_file_dir="${current_dir}/subs.srt" +output_file_dir="${current_dir}/output.mkv" + +# Language config +default_lang_audio="spa" +default_lang_subt="spa" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-mix (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Detect if video file exist +if [ -f "${video_file_dir}" ] ; then + video_found="FOUND" +else + video_found="NOT FOUND" +fi +# Detect if audio file exist +if [ -f "${audio_file_dir}" ] ; then + audio_found="FOUND" +else + audio_found="NOT FOUND" +fi +# Detect if audio file exist +if [ -f "${subs_file_dir}" ] ; then + subs_found="FOUND" +else + subs_found="NOT FOUND" +fi + +# Function to show menu again when sintax is wrong +function show_menu() { + echo "" + echo "* ks-mix (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Extract video/audio and mix video/audio files" + echo "" + echo " + Current dir: ${current_dir}" + echo " + Video file: ${video_file_dir} (${video_found})" + echo " + Audio file: ${audio_file_dir} (${audio_found})" + echo " + Subs file: ${subs_file_dir} (${subs_found})" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mix -evid --> Extract video track" + echo " $ ks-mix -ev25 --> Extract video track to 25 fps (only for H264)" + echo " $ ks-mix -evpf --> Extract video track & set fps (only for H264)" + echo " $ ks-mix -eaud --> Extract audio track" + echo " $ ks-mix -esub --> Extract subtitle track (only for subrip/srt)" + echo " $ ks-mix -chec --> Analyze tracks of video file" + echo " $ ks-mix -mixf --> Mix video and audio file" + echo " $ ks-mix -mixs --> Mix video, audio and subtitle file" + echo "" + echo " + Examples: " + echo " ks-mix -evid /data/movies/Example.mkv" + echo " ks-mix -eaud /data/movies/Example.avi" + echo "" + exit +} + +# Show menu and sintax +if [ -z "${1}" ] ; then + show_menu +elif [ "${1}" == "-evid" ] ; then + # Run commands for -evid parameter + if [ -z "${2}" ] ; then + show_menu + elif [ -f "${2}" ] ; then + echo "" + echo "+ Video Tracks:" + ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + echo "" + echo -n "* Starting extract video track (${2}) " + sleep 3 + ffmpeg -y -i "${2}" -map ${video_track} -c:v copy "${current_dir}/seeing.mp4" + ffmpeg -y -i "${current_dir}/seeing.mp4" -map 0:0 -vsync 1 -c:v copy "${video_file_dir}" + echo "" + echo -n "* Extracted video into ${video_file_dir}" + sleep 2 + rm -rf "${current_dir}/seeing.mp4" + echo "" && echo "" + exit + else + echo "" + echo "* The file '${2}' does not exist" + echo "" + exit + fi +elif [ "${1}" == "-ev25" ] ; then + # Run commands for -ev25 parameter + if [ -z "${2}" ] ; then + show_menu + elif [ -f "${2}" ] ; then + echo "" + echo -n "* Starting extract video track and change to 25 fps (${2}) " + sleep 3 + echo "" && echo "" + ffmpeg -y -i "${2}" -c copy -f h264 "${current_dir}/seeing_noaudio.h264" + ffmpeg -y -r 25 -i "${current_dir}/seeing_noaudio.h264" -c copy "${current_dir}/seeing.mp4" + ffmpeg -y -i "${current_dir}/seeing.mp4" -map 0:0 -vsync 1 -c:v copy "${video_file_dir}" + echo "" + echo -n "* Extracted video into ${video_file_dir} (25 fps)" + sleep 2 + rm -rf "${current_dir}/seeing_noaudio.h264" + rm -rf "${current_dir}/seeing.mp4" + echo "" && echo "" + exit + else + echo "" + echo "* The file '${2}' does not exist" + echo "" + exit + fi +elif [ "${1}" == "-evpf" ] ; then + # Run commands for -evfp parameter + if [ -z "${2}" ] ; then + show_menu + elif [ -f "${2}" ] ; then + frames_default="25" + echo "" + echo -n "[Default: ${frames_default}] Set fps to change: " ; read frames_change + if [ -z "${frames_change}" ] ; then + frames_default=${frames_default} + else + frames_default=${frames_change} + fi + echo "" + echo -n "* Starting extract video track and change to ${frames_default} fps (${2}) " + sleep 3 + echo "" && echo "" + ffmpeg -y -i "${2}" -c copy -f h264 "${current_dir}/seeing_noaudio.h264" + ffmpeg -y -r ${frames_default} -i "${current_dir}/seeing_noaudio.h264" -c copy "${current_dir}/seeing.mp4" + ffmpeg -y -i "${current_dir}/seeing.mp4" -map 0:0 -vsync 1 -c:v copy "${video_file_dir}" + echo "" + echo -n "* Extracted video into ${video_file_dir} (${frames_default} fps)" + sleep 2 + rm -rf "${current_dir}/seeing_noaudio.h264" + rm -rf "${current_dir}/seeing.mp4" + echo "" && echo "" + exit + else + echo "" + echo "* The file '${2}' does not exist" + echo "" + exit + fi +elif [ "${1}" == "-eaud" ] ; then + # Run commands for -eaud parameter + if [ -z "${2}" ] ; then + show_menu + elif [ -f "${2}" ] ; then + echo "" + echo "+ Audio Tracks:" + ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + # Check the audio track by default + audio_default=$(ffmpeg -i "${2}" 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 "${2}" 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 "" + echo -n "* Starting extract audio track (${2}) " + sleep 3 + ffmpeg -y -i "${2}" -map ${audio_track} -c:a copy "${audio_file_dir}" + echo "" + echo -n "* Extracted audio into ${audio_file_dir}" + sleep 2 + echo "" && echo "" + exit + else + echo "" + echo "* The file '${2}' does not exist" + echo "" + exit + fi +elif [ "${1}" == "-esub" ] ; then + # Run commands for -esub parameter + if [ -z "${2}" ] ; then + show_menu + elif [ -f "${2}" ] ; then + echo "" + echo "+ Subtitle Tracks:" + ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d "," -f 1 + echo "" + # Check the subtitle track by default + subtitle_default=$(ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | grep "(forced)" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default="0:3" + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + # Ask for subtitle + echo -n "* (Default: ${subtitle_default}) Type the number of subtitle track: " ; read subtitle_track + if [ -z "${subtitle_track}" ] ; then + subtitle_track="${subtitle_default}" + else + subtitle_track="${subtitle_track}" + fi + echo "" + echo -n "* Starting extract subtitles track (${2}) " + sleep 3 + ffmpeg -y -i "${2}" -map ${subtitle_track} -c:s copy "${subs_file_dir}" + echo "" + echo -n "* Extracted subtitles into ${subs_file_dir}" + sleep 2 + echo "" && echo "" + exit + else + echo "" + echo "* The file '${2}' does not exist" + echo "" + exit + fi +elif [ "${1}" == "-chec" ] ; then + # Run commands for -chec parameter + if [ -z "${2}" ] ; then + show_menu + elif [ -f "${2}" ] ; then + echo "" + echo "* Information of ${2}:" + echo "" + echo "+ Video Tracks:" + ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Video:" + echo "" + echo "+ Audio Tracks:" + ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" + echo "" + echo "+ Subtitle Tracks:" + ffmpeg -i "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" + echo "" + else + echo "" + echo "* The file '${2}' does not exist" + echo "" + exit + fi +elif [ "${1}" == "-mixf" ] ; then + # Run commands for -mixf parameter + if [ "${video_found}" == "FOUND" ] ; then + echo null > /dev/null + else + echo "" + echo "* File '${video_file_dir}' not found!" + echo "" + echo "* Aborted mix" + echo "" + exit + fi + if [ "${audio_found}" == "FOUND" ] ; then + echo null > /dev/null + else + echo "" + echo "* File '${audio_file_dir}' not found!" + echo "" + echo "* Aborted mix" + echo "" + exit + fi + cut_video=0 + cut_audio=0 + # Set the secons to cut on video/audio + echo "" + echo -n "* [Default: ${cut_video}] Set the seconds to cut in the video (0-59): " ; read seconds_video + if [ -z "${seconds_video}" ] ; then + seconds_video=${cut_video} + else + seconds_video=${seconds_video} + fi + echo -n "* [Default: ${cut_audio}] Set the seconds to cut in the audio (0-59): " ; read seconds_audio + if [ -z "${seconds_audio}" ] ; then + seconds_audio=${cut_audio} + else + seconds_audio=${seconds_audio} + fi + # Set if you want -shortest mode + echo -n "* [Default: n] Finish the mix when one of the two (video or audio) finishes first? (y/n): " ; read finish_mix + if [ -z "${finish_mix}" ] ; then + finish_mix=n + elif [ "${finish_mix}" == "y" ] ; then + finish_mix=y + else + finish_mix=n + fi + # Cut video + if [ ${seconds_video} -eq 0 ] ; then + video_input_stream=${video_file_dir} + remove_video_input=${current_dir}/video-cut.mkv + else + if [ ${seconds_video} -le 9 ] ; then + ffmpeg -y -i ${video_file_dir} -ss 00:00:0${seconds_video} -c:v copy ${current_dir}/video-cut.mkv + video_input_stream=${current_dir}/video-cut.mkv + remove_video_input=${current_dir}/video-cut.mkv + else + ffmpeg -y -i ${video_file_dir} -ss 00:00:${seconds_video} -c:v copy ${current_dir}/video-cut.mkv + video_input_stream=${current_dir}/video-cut.mkv + remove_video_input=${current_dir}/video-cut.mkv + fi + fi + # Cut audio + if [ ${seconds_audio} -eq 0 ] ; then + audio_input_stream=${audio_file_dir} + remove_audio_input=${current_dir}/audio-cut.mka + else + if [ ${seconds_audio} -le 9 ] ; then + ffmpeg -y -i ${audio_file_dir} -ss 00:00:0${seconds_audio} -c:a copy ${current_dir}/audio-cut.mka + audio_input_stream=${current_dir}/audio-cut.mka + remove_audio_input=${current_dir}/audio-cut.mka + else + ffmpeg -y -i ${audio_file_dir} -ss 00:00:${seconds_audio} -c:a copy ${current_dir}/audio-cut.mka + audio_input_stream=${current_dir}/audio-cut.mka + remove_audio_input=${current_dir}/audio-cut.mka + fi + fi + echo "" + echo "* Starting mix of:" + echo " - Video: ${video_file_dir}" + echo " - Audio: ${audio_file_dir}" + echo "" + echo -n " Output file: ${output_file_dir} " + sleep 3 + echo "" && echo "" + if [ "${finish_mix}" == "y" ] ; then + ffmpeg -y -i "${video_input_stream}" -i "${audio_input_stream}" -c:v copy -c:a copy -shortest "${output_file_dir}" + else + ffmpeg -y -i "${video_input_stream}" -i "${audio_input_stream}" -c:v copy -c:a copy "${output_file_dir}" + fi + echo "" + echo -n "* Mixed video/audio into ${output_file_dir}" + rm -rf ${remove_video_input} + rm -rf ${remove_audio_input} + sleep 2 + echo "" && echo "" + exit +elif [ "${1}" == "-mixs" ] ; then + # Run commands for -mixs parameter + if [ "${video_found}" == "FOUND" ] ; then + echo null > /dev/null + else + echo "" + echo "* File '${video_file_dir}' not found!" + echo "" + echo "* Aborted mix" + echo "" + exit + fi + if [ "${audio_found}" == "FOUND" ] ; then + echo null > /dev/null + else + echo "" + echo "* File '${audio_file_dir}' not found!" + echo "" + echo "* Aborted mix" + echo "" + exit + fi + if [ "${subs_found}" == "FOUND" ] ; then + echo null > /dev/null + else + echo "" + echo "* File '${subs_file_dir}' not found!" + echo "" + echo "* Aborted mix" + echo "" + exit + fi + cut_video=0 + cut_audio=0 + # Set the secons to cut on video/audio + echo "" + echo -n "* [Default: ${cut_video}] Set the seconds to cut in the video (0-59): " ; read seconds_video + if [ -z "${seconds_video}" ] ; then + seconds_video=${cut_video} + else + seconds_video=${seconds_video} + fi + echo -n "* [Default: ${cut_audio}] Set the seconds to cut in the audio (0-59): " ; read seconds_audio + if [ -z "${seconds_audio}" ] ; then + seconds_audio=${cut_audio} + else + seconds_audio=${seconds_audio} + fi + # Set if you want -shortest mode + echo -n "* [Default: n] Finish the mix when one of the three (video, audio or subs) finishes first? (y/n): " ; read finish_mix + if [ -z "${finish_mix}" ] ; then + finish_mix=n + elif [ "${finish_mix}" == "y" ] ; then + finish_mix=y + else + finish_mix=n + fi + # Cut video + if [ ${seconds_video} -eq 0 ] ; then + video_input_stream=${video_file_dir} + remove_video_input=${current_dir}/video-cut.mkv + else + if [ ${seconds_video} -le 9 ] ; then + ffmpeg -y -i ${video_file_dir} -ss 00:00:0${seconds_video} -c:v copy ${current_dir}/video-cut.mkv + video_input_stream=${current_dir}/video-cut.mkv + remove_video_input=${current_dir}/video-cut.mkv + else + ffmpeg -y -i ${video_file_dir} -ss 00:00:${seconds_video} -c:v copy ${current_dir}/video-cut.mkv + video_input_stream=${current_dir}/video-cut.mkv + remove_video_input=${current_dir}/video-cut.mkv + fi + fi + # Cut audio + if [ ${seconds_audio} -eq 0 ] ; then + audio_input_stream=${audio_file_dir} + remove_audio_input=${current_dir}/audio-cut.mka + else + if [ ${seconds_audio} -le 9 ] ; then + ffmpeg -y -i ${audio_file_dir} -ss 00:00:0${seconds_audio} -c:a copy ${current_dir}/audio-cut.mka + audio_input_stream=${current_dir}/audio-cut.mka + remove_audio_input=${current_dir}/audio-cut.mka + else + ffmpeg -y -i ${audio_file_dir} -ss 00:00:${seconds_audio} -c:a copy ${current_dir}/audio-cut.mka + audio_input_stream=${current_dir}/audio-cut.mka + remove_audio_input=${current_dir}/audio-cut.mka + fi + fi + echo "" + echo "* Starting mix of:" + echo " - Video: ${video_file_dir}" + echo " - Audio: ${audio_file_dir}" + echo " - Subs: ${subs_file_dir}" + echo "" + echo -n " Output file: ${output_file_dir} " + sleep 3 + echo "" && echo "" + if [ "${finish_mix}" == "y" ] ; then + ffmpeg -y -i "${video_input_stream}" -i "${audio_input_stream}" -i "${subs_file_dir}" -c:v copy -c:a copy -c:s copy -shortest "${output_file_dir}" + else + ffmpeg -y -i "${video_input_stream}" -i "${audio_input_stream}" -i "${subs_file_dir}" -c:v copy -c:a copy -c:s copy "${output_file_dir}" + fi + echo "" + echo -n "* Mixed video/audio/subs into ${output_file_dir}" + rm -rf ${remove_video_input} + rm -rf ${remove_audio_input} + sleep 2 + echo "" && echo "" + exit +else + show_menu +fi diff --git a/src/ks-mp3 b/src/ks-mp3 new file mode 100755 index 0000000..efcf2ff --- /dev/null +++ b/src/ks-mp3 @@ -0,0 +1,265 @@ +#!/bin/bash + +######################################################################### +# ks-mp3 (ks-tools) - Convert video/audio file(s) to MP3 (Audio) Format # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +######################################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +acodec="libmp3lame" +b_acodec="128k" +v_ext="mp3" +default_lang_audio="spa" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-mp3 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + 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} +} + +# Sintax: ks-mp3 +if [ -z "${1}" ] ; then + echo "" + echo "* ks-mp3 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video/audio file(s) to MP3 (Audio) Format" + echo "" + echo "+ Config:" + echo "" + echo " - Audio codec: ${acodec}" + echo " - Bitrate audio: ${b_acodec}" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp3 " + echo "" + echo " + Example: ks-mp3 /data/songs/Example.ogg /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-mp3 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-mp3 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video/audio file(s) to MP3 (Audio) Format" + echo "" + echo "+ Config:" + echo "" + echo " - Audio codec: ${acodec}" + echo " - Bitrate audio: ${b_acodec}" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp3 " + echo "" + echo " + Example: ks-mp3 /data/songs/Example.ogg /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 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=$(${p_ffmpeg} "${1}" 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 + + # Ask for METADATA + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + 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: Album-Disc) Type name of album: " ; read album_file + if [ -z "${album_file}" ] ; then + album_title="Album-Disc" + else + album_title="${album_file}" + fi + echo -n "* (Default: 01) Type number of track: " ; read track_file + if [ -z "${track_file}" ] ; then + track_num="01" + else + track_num="${track_file}" + fi + echo -n "* (Default: Artist) Type name of artist: " ; read artist_file + if [ -z "${artist_file}" ] ; then + artist_title="Artist" + else + artist_title="${artist_file}" + fi + echo -n "* (Default: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_num="${current_date}" + else + year_num="${year_file}" + fi + echo -n "* (Default: Pop) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Pop" + else + genre_file="${genre_file}" + fi + + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + echo " # Convert the file '${1}' to MP3 Audio" + echo " ${p_ffmpeg} \"${1}\" -map ${audio_track} -c:a ${acodec} -b:a ${b_acodec} -metadata title=\"${name_title}\" -metadata date=\"${year_num}\" -metadata genre=\"${genre_file}\" -metadata album=\"${album_title}\" -metadata artist=\"${artist_title}\" -metadata track=\"${track_num}\" \"${2}.${v_ext}\"" + + # 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 + ${p_ffmpeg} "${1}" -map ${audio_track} -c:a ${acodec} -b:a ${b_acodec} -metadata title="${name_title}" -metadata date="${year_num}" -metadata genre="${genre_file}" -metadata album="${album_title}" -metadata artist="${artist_title}" -metadata track="${track_num}" "${2}.${v_ext}" + fi +fi diff --git a/src/ks-mp3-album b/src/ks-mp3-album new file mode 100755 index 0000000..c3aa6b3 --- /dev/null +++ b/src/ks-mp3-album @@ -0,0 +1,294 @@ +#!/bin/bash + +###################################################################### +# ks-mp3-album (ks-tools) - Convert folder album to MP3 Audio Format # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +###################################################################### +VERSION="6.6" +M_DATE="100221" + +# Variables +tempFile="/tmp/ks-mp4s-folder.txt" +tempFileTest="/tmp/ks-mp4s-folder-test.txt" +formatFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob ogg oga mp3 mp2 aac ac3" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +acodec="libmp3lame" +b_acodec="128k" +v_ext="mp3" +track_init="1" +artist_init="Artist" +default_lang_audio="spa" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-mp3-album (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + 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} +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Sintax: ks-mp3-album " + echo "" + echo " + Example: ks-mp3 /data/albums/matrix-soundtrack" + echo "" + exit +fi + +# Check if folder exist +if [ -d "${1}" ] ; then + echo null > /dev/null +else + echo "" + echo "* ks-mp3-album (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The folder '${1}' does not exist!" + echo "" + exit +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 + 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 +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 + album_title="Album-Disc" +else + album_title="${album_file}" +fi +echo -n "* (Default: ${current_date}) Type the year of album: " ; read year_file +if [ -z "${year_file}" ] ; then + year_num="${current_date}" +else + year_num="${year_file}" +fi +echo -n "* (Default: Pop) Type the genre of album: " ; read genre_file +if [ -z "${genre_file}" ] ; then + genre_file="Pop" +else + genre_file="${genre_file}" +fi + +# Init the conversion files +convert_files=1 +num_files=$(cat ${tempFile} | wc -l) +if [ ${num_files} -eq 0 ] ; then + echo "" + echo "+ No video/audio file(s) found in folder '${1}" + echo "" + exit +else + 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-oga'" + # 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-oga && rm -rf to-oga/* + 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 + ffmpeg -i "${show_file}" -map ${audio_track} -c:a ${acodec} -b:a ${b_acodec} -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-oga/${fname}.${v_ext}" + # Increment variables + convert_files=$(expr ${convert_files} + 1) + track_init=$(expr ${track_num} + 1) + done + fi +fi diff --git a/src/ks-mp4 b/src/ks-mp4 new file mode 100755 index 0000000..6a2fd13 --- /dev/null +++ b/src/ks-mp4 @@ -0,0 +1,379 @@ +#!/bin/bash + +############################################################### +# ks-mp4 (ks-tools) - Convert video to MP4 format # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +############################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +rel_size="1280x534" +vcodec="libx264" +b_vcodec="2400k" +# Presets: ultrafast, superfast, veryfast, faster, +# fast, medium (default), slow, slower, veryslow +v_preset="medium" +acodec="aac" +b_acodec="256k" +default_lang_audio="spa" +default_lang_subt="spa" +v_ext="mp4" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-mp4 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + echo "${1}" > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Sintax: ks-mp4 +if [ -z "${1}" ] ; then + echo "" + echo "* ks-mp4 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient MP4" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Preset: ${v_preset}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp4 " + echo "" + echo " + Example: ks-mp4 /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-mp4 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" +p_ffmpeg_patched="-max_muxing_queue_size 9999" +f_conversion="-vsync 1 -async 1" +# Check if video input uses H265 (HEVC) +codec_h265=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "h265") +codec_hevc=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "hevc") +yuv420p10le=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "yuv420p10le") +codec_h265_hevc="${codec_h265}${codec_hevc}${yuv420p10le}" +if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" +else + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -pix_fmt yuv420p -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" +fi + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-mp4 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient MP4" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Preset: ${v_preset}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp4 " + echo "" + echo " + Example: ks-mp4 /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Video Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + echo "+ Subtitle Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 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=$(${p_ffmpeg} "${1}" 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 + # Check the subtitle track by default + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | grep "(forced)" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default="0:3" + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + # Ask for subtitle + echo -n "* (Default: n) Do you want include subtitles? (y/n): " ; read subtitles_y_n + if [ "${subtitles_y_n}" == "y" ] ; then + echo -n "* (Default: ${subtitle_default}) Type the number of subtitle track: " ; read subtitle_track + if [ -z "${subtitle_track}" ] ; then + subtitle_track="${subtitle_default}" + else + subtitle_track="${subtitle_track}" + fi + fi + echo -n "* (Default: ${rel_size}) Type the resolution: " ; read resolution + if [ -z "${resolution}" ] ; then + resolution="${rel_size}" + else + rel_size="${resolution}" + if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" + else + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -pix_fmt yuv420p -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" + fi + fi + echo -n "* (Default: n) Do you want apply '-max_muxing_queue_size 9999' patch? (y/n): " ; read patch_thread + if [ "${patch_thread}" == "y" ] ; then + patch_thread="y" + else + patch_thread="n" + fi + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + 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: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_file="${current_date}" + else + year_file="${year_file}" + fi + echo -n "* (Default: Unknown) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Unknown" + else + genre_file="${genre_file}" + fi + + # Check audio 5.1 or 7.1 + audio_5_7_1=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1") + audio_5_7_1_2=$(${p_ffmpeg} "${1}" 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 + + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + if [ "${subtitles_y_n}" == "y" ] ; then + echo " # Extract subtitles from file ${1}" + echo " ${p_ffmpeg} \"${1}\" -map ${subtitle_track} \"${2}.srt\"" + echo "" + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + else + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + # 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 + if [ "${subtitles_y_n}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${subtitle_track} "${2}.srt" + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + else + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + fi +fi diff --git a/src/ks-mp4k b/src/ks-mp4k new file mode 100755 index 0000000..572d87e --- /dev/null +++ b/src/ks-mp4k @@ -0,0 +1,379 @@ +#!/bin/bash + +############################################################### +# ks-mp4k (ks-tools) - Convert video to MP4 format (4K) # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +############################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +rel_size="3840x2160" +vcodec="libx264" +b_vcodec="6000k" +# Presets: ultrafast, superfast, veryfast, faster, +# fast, medium (default), slow, slower, veryslow +v_preset="medium" +acodec="aac" +b_acodec="320k" +default_lang_audio="spa" +default_lang_subt="spa" +v_ext="mp4" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-mp4k (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + echo "${1}" > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Sintax: ks-mp4k +if [ -z "${1}" ] ; then + echo "" + echo "* ks-mp4k (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient MP4 (4K)" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Preset: ${v_preset}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp4k " + echo "" + echo " + Example: ks-mp4k /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-mp4k (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" +p_ffmpeg_patched="-max_muxing_queue_size 9999" +f_conversion="-vsync 1 -async 1" +# Check if video input uses H265 (HEVC) +codec_h265=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "h265") +codec_hevc=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "hevc") +yuv420p10le=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "yuv420p10le") +codec_h265_hevc="${codec_h265}${codec_hevc}${yuv420p10le}" +if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" +else + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -pix_fmt yuv420p -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" +fi + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-mp4k (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient MP4 (4K)" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Preset: ${v_preset}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp4k " + echo "" + echo " + Example: ks-mp4k /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Video Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + echo "+ Subtitle Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 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=$(${p_ffmpeg} "${1}" 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 + # Check the subtitle track by default + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | grep "(forced)" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default="0:3" + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + # Ask for subtitle + echo -n "* (Default: n) Do you want include subtitles? (y/n): " ; read subtitles_y_n + if [ "${subtitles_y_n}" == "y" ] ; then + echo -n "* (Default: ${subtitle_default}) Type the number of subtitle track: " ; read subtitle_track + if [ -z "${subtitle_track}" ] ; then + subtitle_track="${subtitle_default}" + else + subtitle_track="${subtitle_track}" + fi + fi + echo -n "* (Default: ${rel_size}) Type the resolution: " ; read resolution + if [ -z "${resolution}" ] ; then + resolution="${rel_size}" + else + rel_size="${resolution}" + if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" + else + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -pix_fmt yuv420p -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" + fi + fi + echo -n "* (Default: n) Do you want apply '-max_muxing_queue_size 9999' patch? (y/n): " ; read patch_thread + if [ "${patch_thread}" == "y" ] ; then + patch_thread="y" + else + patch_thread="n" + fi + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + 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: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_file="${current_date}" + else + year_file="${year_file}" + fi + echo -n "* (Default: Unknown) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Unknown" + else + genre_file="${genre_file}" + fi + + # Check audio 5.1 or 7.1 + audio_5_7_1=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1") + audio_5_7_1_2=$(${p_ffmpeg} "${1}" 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 + + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + if [ "${subtitles_y_n}" == "y" ] ; then + echo " # Extract subtitles from file ${1}" + echo " ${p_ffmpeg} \"${1}\" -map ${subtitle_track} \"${2}.srt\"" + echo "" + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + else + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + # 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 + if [ "${subtitles_y_n}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${subtitle_track} "${2}.srt" + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + else + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + fi +fi diff --git a/src/ks-mp4s b/src/ks-mp4s new file mode 100755 index 0000000..512d65a --- /dev/null +++ b/src/ks-mp4s @@ -0,0 +1,379 @@ +#!/bin/bash + +############################################################### +# ks-mp4s (ks-tools) - Convert video to MP4 format (Series) # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +############################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +rel_size="720x480" +vcodec="libx264" +b_vcodec="950k" +# Presets: ultrafast, superfast, veryfast, faster, +# fast, medium (default), slow, slower, veryslow +v_preset="medium" +acodec="aac" +b_acodec="128k" +default_lang_audio="spa" +default_lang_subt="spa" +v_ext="mp4" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-mp4s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + echo "${1}" > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Sintax: ks-mp4s +if [ -z "${1}" ] ; then + echo "" + echo "* ks-mp4s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient MP4 (Series)" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Preset: ${v_preset}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp4s " + echo "" + echo " + Example: ks-mp4s /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-mp4s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" +p_ffmpeg_patched="-max_muxing_queue_size 9999" +f_conversion="-vsync 1 -async 1" +# Check if video input uses H265 (HEVC) +codec_h265=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "h265") +codec_hevc=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "hevc") +yuv420p10le=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "yuv420p10le") +codec_h265_hevc="${codec_h265}${codec_hevc}${yuv420p10le}" +if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" +else + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -pix_fmt yuv420p -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" +fi + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-mp4s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient MP4 (Series)" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Preset: ${v_preset}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp4s " + echo "" + echo " + Example: ks-mp4s /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Video Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + echo "+ Subtitle Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 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=$(${p_ffmpeg} "${1}" 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 + # Check the subtitle track by default + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | grep "(forced)" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default="0:3" + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + # Ask for subtitle + echo -n "* (Default: n) Do you want include subtitles? (y/n): " ; read subtitles_y_n + if [ "${subtitles_y_n}" == "y" ] ; then + echo -n "* (Default: ${subtitle_default}) Type the number of subtitle track: " ; read subtitle_track + if [ -z "${subtitle_track}" ] ; then + subtitle_track="${subtitle_default}" + else + subtitle_track="${subtitle_track}" + fi + fi + echo -n "* (Default: ${rel_size}) Type the resolution: " ; read resolution + if [ -z "${resolution}" ] ; then + resolution="${rel_size}" + else + rel_size="${resolution}" + if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" + else + p_conversion="-s ${rel_size} -c:v ${vcodec} -profile:v high -pix_fmt yuv420p -b:v ${b_vcodec} -preset ${v_preset} -c:a ${acodec} -b:a ${b_acodec}" + fi + fi + echo -n "* (Default: n) Do you want apply '-max_muxing_queue_size 9999' patch? (y/n): " ; read patch_thread + if [ "${patch_thread}" == "y" ] ; then + patch_thread="y" + else + patch_thread="n" + fi + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + 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: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_file="${current_date}" + else + year_file="${year_file}" + fi + echo -n "* (Default: Unknown) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Unknown" + else + genre_file="${genre_file}" + fi + + # Check audio 5.1 or 7.1 + audio_5_7_1=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1") + audio_5_7_1_2=$(${p_ffmpeg} "${1}" 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 + + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + if [ "${subtitles_y_n}" == "y" ] ; then + echo " # Extract subtitles from file ${1}" + echo " ${p_ffmpeg} \"${1}\" -map ${subtitle_track} \"${2}.srt\"" + echo "" + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + else + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to MP4" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + # 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 + if [ "${subtitles_y_n}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${subtitle_track} "${2}.srt" + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + else + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + fi +fi diff --git a/src/ks-mp4s-folder b/src/ks-mp4s-folder new file mode 100755 index 0000000..1e91a74 --- /dev/null +++ b/src/ks-mp4s-folder @@ -0,0 +1,118 @@ +#!/bin/bash + +################################################################################# +# ks-mp4s-folder (ks-tools) - Convert videos from folder to MP4 format (Series) # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +################################################################################# +VERSION="6.6" +M_DATE="100221" + +# Variables +tempFile="/tmp/ks-mp4s-folder.txt" +tempFileTest="/tmp/ks-mp4s-folder-test.txt" +formatFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob" + +# Show help when folder is empty +if [ -z "${1}" ] ; then + echo "" + echo "* ks-mp4s-folder (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert videos from folder to MP4 format (Series)" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-mp4s-folder [subs]" + echo "" + echo " + Examples: " + echo " $ ks-mp4s-folder /data/Westworld" + echo " $ ks-mp4s-folder /data/Daredevil subs" + echo "" + echo "* Notes:" + echo "" + echo " + The option 'subs' apply detection & rendering the forced subtitles." + echo " + You must not use spaces in folders and video files." + echo "" + exit +fi + +# Check if folder exist +if [ -d "${1}" ] ; then + echo null > /dev/null +else + echo "" + echo "* ks-mp4s-folder (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The folder '${1}' does not exist!" + echo "" + exit +fi + +# Scan videos files format +clear +echo "" +echo "* ks-mp4s-folder (ks-tools) v${VERSION} (${M_DATE})" +echo "" +echo "- Convert videos from folder to MP4 format (Series)" +echo "" +echo -n "* Scanning ${1} " && sleep 4 +echo "" +rm -rf ${tempFile} && touch ${tempFile} +for format in ${formatFiles} ; do + 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 +done + +# Init the conversion files +num_files=$(cat ${tempFile} | wc -l) +if [ ${num_files} -eq 0 ] ; then + echo "" + echo "+ No video file(s) found in folder '${1}" + echo "" + exit +else + echo "" + echo "* Files to convert (${1}):" + convert_files=1 + if [ "${2}" == "subs" ] ; then + subtitles_enabled=1 + subtitles_show="with subtitles" + else + subtitles_enabled=0 + subtitles_show="without subtitles" + fi + while [ ${convert_files} -le ${num_files} ] ; do + show_file=$(cat ${tempFile} | head -${convert_files} | tail -1) + echo " + ${show_file} (${subtitles_show})" + convert_files=$(expr ${convert_files} + 1) + done + echo "" + echo "* The output folder will be '${1}/to-mp4'" + # 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-mp4 && rm -rf to-mp4/* + while [ ${convert_files} -le ${num_files} ] ; do + show_file=$(cat ${tempFile} | head -${convert_files} | tail -1) + if [ ${subtitles_enabled} -eq 1 ] ; then + fname=$(echo "${show_file}" | cut -d "." -f 1) + ks-mp4s-wrapper --conv-with-sub "${show_file}" "to-mp4/${fname}" + else + fname=$(echo "${show_file}" | cut -d "." -f 1) + ks-mp4s-wrapper --conv "${show_file}" "to-mp4/${fname}" + fi + convert_files=$(expr ${convert_files} + 1) + done + fi +fi diff --git a/src/ks-oga b/src/ks-oga new file mode 100755 index 0000000..44c256c --- /dev/null +++ b/src/ks-oga @@ -0,0 +1,265 @@ +#!/bin/bash + +############################################################################# +# ks-oga (ks-tools) - Convert video/audio file(s) to OGA (OGG Audio) Format # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +############################################################################# +VERSION="6.6" +M_DATE="100221" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +acodec="libvorbis" +b_acodec="130k" +v_ext="oga" +default_lang_audio="spa" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-oga (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + 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} +} + +# Sintax: ks-oga +if [ -z "${1}" ] ; then + echo "" + echo "* ks-oga (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video/audio file(s) to OGA (OGG Audio) Format" + echo "" + echo "+ Config:" + echo "" + echo " - Audio codec: ${acodec}" + echo " - Bitrate audio: ${b_acodec}" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-oga " + echo "" + echo " + Example: ks-oga /data/songs/Example.mp3 /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-oga (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-oga (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video/audio file(s) to OGA (OGG Audio) Format" + echo "" + echo "+ Config:" + echo "" + echo " - Audio codec: ${acodec}" + echo " - Bitrate audio: ${b_acodec}" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-oga " + echo "" + echo " + Example: ks-oga /data/songs/Example.mp3 /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 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=$(${p_ffmpeg} "${1}" 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 + + # Ask for METADATA + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + 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: Album-Disc) Type name of album: " ; read album_file + if [ -z "${album_file}" ] ; then + album_title="Album-Disc" + else + album_title="${album_file}" + fi + echo -n "* (Default: 01) Type number of track: " ; read track_file + if [ -z "${track_file}" ] ; then + track_num="01" + else + track_num="${track_file}" + fi + echo -n "* (Default: Artist) Type name of artist: " ; read artist_file + if [ -z "${artist_file}" ] ; then + artist_title="Artist" + else + artist_title="${artist_file}" + fi + echo -n "* (Default: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_num="${current_date}" + else + year_num="${year_file}" + fi + echo -n "* (Default: Pop) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Pop" + else + genre_file="${genre_file}" + fi + + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + echo " # Convert the file '${1}' to OGG Audio" + echo " ${p_ffmpeg} \"${1}\" -map ${audio_track} -c:a ${acodec} -b:a ${b_acodec} -metadata title=\"${name_title}\" -metadata date=\"${year_num}\" -metadata genre=\"${genre_file}\" -metadata album=\"${album_title}\" -metadata artist=\"${artist_title}\" -metadata track=\"${track_num}\" \"${2}.${v_ext}\"" + + # 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 + ${p_ffmpeg} "${1}" -map ${audio_track} -c:a ${acodec} -b:a ${b_acodec} -metadata title="${name_title}" -metadata date="${year_num}" -metadata genre="${genre_file}" -metadata album="${album_title}" -metadata artist="${artist_title}" -metadata track="${track_num}" "${2}.${v_ext}" + fi +fi diff --git a/src/ks-oga-album b/src/ks-oga-album new file mode 100755 index 0000000..2e29e1f --- /dev/null +++ b/src/ks-oga-album @@ -0,0 +1,294 @@ +#!/bin/bash + +############################################################################ +# ks-oga-album (ks-tools) - Convert folder album to OGA (OGG Audio) Format # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +############################################################################ +VERSION="6.6" +M_DATE="100221" + +# Variables +tempFile="/tmp/ks-mp4s-folder.txt" +tempFileTest="/tmp/ks-mp4s-folder-test.txt" +formatFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob ogg oga mp3 mp2 aac ac3" + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" + +# Basic parameters +acodec="libvorbis" +b_acodec="130k" +v_ext="oga" +track_init="1" +artist_init="Artist" +default_lang_audio="spa" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-oga-album (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Sintax: extractFolderOrFile +function extractFolderOrFile() { + 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}" +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + 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} +} + +# Function to remove extension from file +# Sintax: removeExtension "" +function removeExtension() { + 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 + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Sintax: ks-oga +if [ -z "${1}" ] ; then + echo "" + echo "* ks-oga-album (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert folder album to OGA (OGG Audio) Format" + echo "" + echo "+ Config:" + echo "" + echo " - Audio codec: ${acodec}" + echo " - Bitrate audio: ${b_acodec}" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-oga " + echo "" + echo " + Example: ks-oga /data/albums/matrix-soundtrack" + echo "" + exit +fi + +# Check if folder exist +if [ -d "${1}" ] ; then + echo null > /dev/null +else + echo "" + echo "* ks-oga-album (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The folder '${1}' does not exist!" + echo "" + exit +fi + +# Scan videos/audio files format +clear +echo "" +echo "* ks-oga-album (ks-tools) v${VERSION} (${M_DATE})" +echo "" +echo "- Convert folder album to OGA (OGG Audio) Format" +echo "" +echo -n "* Scanning ${1} " && sleep 4 +echo "" +rm -rf ${tempFile} && touch ${tempFile} +for format in ${formatFiles} ; do + 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 +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 + album_title="Album-Disc" +else + album_title="${album_file}" +fi +echo -n "* (Default: ${current_date}) Type the year of album: " ; read year_file +if [ -z "${year_file}" ] ; then + year_num="${current_date}" +else + year_num="${year_file}" +fi +echo -n "* (Default: Pop) Type the genre of album: " ; read genre_file +if [ -z "${genre_file}" ] ; then + genre_file="Pop" +else + genre_file="${genre_file}" +fi + +# Init the conversion files +convert_files=1 +num_files=$(cat ${tempFile} | wc -l) +if [ ${num_files} -eq 0 ] ; then + echo "" + echo "+ No video/audio file(s) found in folder '${1}" + echo "" + exit +else + 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-oga'" + # 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-oga && rm -rf to-oga/* + 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 + ffmpeg -i "${show_file}" -map ${audio_track} -c:a ${acodec} -b:a ${b_acodec} -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-oga/${fname}.${v_ext}" + # Increment variables + convert_files=$(expr ${convert_files} + 1) + track_init=$(expr ${track_num} + 1) + done + fi +fi diff --git a/src/ks-upa b/src/ks-upa new file mode 100755 index 0000000..afe714b --- /dev/null +++ b/src/ks-upa @@ -0,0 +1,444 @@ +#!/bin/bash + +##################################################################### +# ks-upa (ks-tools) - Upload audio file(s) to server with rsync+ssh # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +##################################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters. +dirTemp="/tmp" +listCompTemp="ks-tools.list-full" +listCompTempTest="ks-tools.list-full.test" +listTemp="ks-tools.list" +dirConfig="$HOME/.ks-tools" +ksToolsTempFolder="/tmp/ks-tools" + +# Function to remove spaces and symbols +# Sintaxis: convertText "" +function convertText() { + wordToConvert=${1} + ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt" + mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null + echo "${wordToConvert}" > ${ksToolsSedFile} + # Borrar espacios + sed -i 's/ /_/g' "${ksToolsSedFile}" &> /dev/null + # Borrar simbolos + symbolsList="[ ] @ { } | \ / ~ # $ % & ? ¿ = ( ) < > ! ¡" + for findSymbol in ${symbolsList} ; do + sed -i "s/${findSymbol}//g" "${ksToolsSedFile}" &> /dev/null + done + # Borrar el resto de simbolos + sed -i 's/*//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/"//g' "${ksToolsSedFile}" &> /dev/null + sed -i "s/^//g" "${ksToolsSedFile}" &> /dev/null + # Cambiar algunos simbolos + sed -i 's/+/_/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/:/-/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/;/-/g' "${ksToolsSedFile}" &> /dev/null + # Mostrar texto convertido + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to list the files of a directory. +# Sintax: listArchives +function listArchives() { + fail=1 + formatFiles="mp3 wma ogg wav flac midi acc oga opus mp2 ra m4a mka" + # Rename files + cd "${1}" + mkdir -p ${ksToolsTempFolder} + ls -1 > ${ksToolsTempFolder}/rename-files.txt + count=1 + sizeFile=$(cat ${ksToolsTempFolder}/rename-files.txt | wc -l) + while [ ${count} -le ${sizeFile} ] ; do + fileToRename=$(cat ${ksToolsTempFolder}/rename-files.txt | head -${count} | tail -1) + fileRenamed=$(convertText "${fileToRename}") + if [ "${fileRenamed}" == "${fileToRename}" ] ; then + echo "null" > /dev/null + else + mv "${fileToRename}" "${fileRenamed}" + fi + count=$(expr $count + 1) + done + # Scan audio files + rm -rf ${dirTemp}/${listTemp} + rm -rf ${dirTemp}/${listCompTemp} + for format in ${formatFiles} ; do + find "${1}"/*.${format} &> ${dirTemp}/${listCompTempTest} + if [ $? -ne 0 ] ; then + echo "null" > /dev/null + else + find "${1}"/*.${format} &>> ${dirTemp}/${listCompTemp} + echo "Audio file(s) in .${format} found!" + cd "${1}" && ls -1 *.${format} &>> ${dirTemp}/${listTemp} + fail=0 + fi + done + if [ ${fail} -eq 1 ] ; then + echo "No audio file(s) found!" + echo "" + exit + else + echo "" + fi +} + +# Function to count the found files. +function countArchives() { + totalArchives=$(cat ${dirTemp}/${listCompTemp} | wc -l) + echo ${totalArchives} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + FileName=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + FileNoExtension=$(echo $FileName | cut -d "." -f 1) + echo $FileNoExtension > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Function to show the name of the file. +# Sintax: showFile +function showFile() { + archive=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + echo ${archive} +} + +# Function to show full file path. +# Sintax: showPathFile +function showPathFile() { + pathFile=$(cat ${dirTemp}/${listCompTemp} | head -${1} | tail -1) + echo ${pathFile} +} + +# Function to send file to server. +# Sintax: sendFile [file name] +function sendFile() { + correct=0 + countSend=0 + while [ ${correct} -eq 0 ] ; do + #sshpass -p ${1} scp ${2} ${3}@${4}:${5} &> /dev/null + comandOne="sshpass -p ${1} rsync -azL -e" + comandTwo="--progress ${2} ${3}@${4}:${5}" + ${comandOne} "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" ${comandTwo} 2> /dev/null + sshpass -p ${1} ssh ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "Failure to send ${2}" + echo "Retrying..." + sshpass -p ${1} ssh ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null + sleep 5 + countSend=$(expr ${countSend} + 1) + if [ ${countSend} -eq 5 ] ; then + correct=1 + fi + else + correct=1 + fi + done +} + +# Function to check local and server checksum. +# Sintax: checkChecksum +function checkChecksum() { + correct=0 + countChecksum=0 + echo -n "Checking checksum... " && sleep 4 + echo "" + while [ ${correct} -eq 0 ] ; do + checksumServer=$(sshpass -p ${1} ssh ${3}@${4} md5sum ${5} 2> /dev/null) + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "Failed to get checksum for ${5}" + echo "Retrying..." + sleep 5 + countChecksum=$(expr ${countChecksum} + 1) + if [ ${countChecksum} -eq 5 ] ; then + correct=1 + fi + else + checksumLocal=$(md5sum ${2}) + echo "Local checksum: $checksumLocal" + echo "Server checksum: $checksumServer" + correct=1 + fi + done +} + +# Function to show config +function showConfig() { + configAvailable=0 + echo "" + echo "ks-upa (ks-tools) v${VERSION} (${M_DATE})" + echo "" + if [ -f ${dirConfig}/USER ] ; then + showUser=$(cat ${dirConfig}/USER) + echo "Server User: ${showUser}" + configAvailable=1 + fi + if [ -f ${dirConfig}/PASS ] ; then + showPassword=$(cat ${dirConfig}/PASS) + echo "Server Password: ${showPassword}" + configAvailable=1 + fi + if [ -f ${dirConfig}/SERVER ] ; then + showServer=$(cat ${dirConfig}/SERVER) + echo "URL (or IP) Server: ${showServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR_SERVER ] ; then + showDirServer=$(cat ${dirConfig}/DIR_SERVER) + echo "Destination Path (Server): ${showDirServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR ] ; then + showDirLocal=$(cat ${dirConfig}/DIR) + echo "Scan Path (Local): ${showDirLocal}" + configAvailable=1 + fi + if [ ${configAvailable} -eq 0 ] ; then + echo "The configuration file does not exist!" + fi + echo "" + exit +} + +# Function to edit the configuration file +function editConfig() { + editConfig=0 + while [ ${editConfig} -eq 0 ] ; do + clear + editUser=$(cat ${dirConfig}/USER 2> /dev/null) + editPassword=$(cat ${dirConfig}/PASS 2> /dev/null) + editServer=$(cat ${dirConfig}/SERVER 2> /dev/null) + editDirServer=$(cat ${dirConfig}/DIR_SERVER 2> /dev/null) + editDirLocal=$(cat ${dirConfig}/DIR 2> /dev/null) + echo "" + echo "ks-upa (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "1 - Edit User (${editUser})" + echo "2 - Edit Password (${editPassword})" + echo "3 - Edit Server (${editServer})" + echo "4 - Edit Dest. Path (${editDirServer})" + echo "5 - Edit Local Path (${editDirLocal})" + echo "" + echo "6 - Exit" + echo "" + echo -n "Choose an option: " ; read EDIT + echo "" + if [ "${EDIT}" == "1" ] ; then + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + elif [ "${EDIT}" == "2" ] ; then + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + elif [ "${EDIT}" == "3" ] ; then + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + elif [ "${EDIT}" == "4" ] ; then + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + elif [ "${EDIT}" == "5" ] ; then + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + elif [ "${EDIT}" == "6" ] ; then + editConfig=1 + else + echo "Invalid option!" + echo -n "Press ENTER to continue " ; read CONTINUE + fi + done + exit +} + +# Function to show version +function showVersion() { + echo "" + echo "ks-upa (ks-tools) v${VERSION} (${M_DATE})" + echo "" + exit +} + +# Function to show help +function showHelp() { + echo "" + echo "ks-upa (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "Upload audio file(s) to server with rsync+ssh" + echo "" + echo "Sintax:" + echo "" + echo "ks-upa -i - Start upload" + echo "ks-upa -r - Remove configuration" + echo "ks-upa -c - Show configuration" + echo "ks-upa -e - Edit configuration" + echo "ks-upa -v - Show version" + echo "ks-upa -h - Show help" + echo "" + exit +} + +# Function to check if all the necessary tools +# for the execution are installed. +function checkDependencies() { + dependence=0 + echo -n "Checking necessary tools... " + sleep 3 && echo "" + sshpass -h &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'sshpass' tool is not installed!" + dependence=1 + fi + md5sum --help &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'md5sum' tool is not installed!" + dependence=1 + fi + rsync --version &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'rsync' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/scp ] ; then + echo "OK" > /dev/null + else + echo "The 'scp' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/ssh ] ; then + echo "OK" > /dev/null + else + echo "The 'ssh' tool is not installed!" + dependence=1 + fi + if [ ${dependence} -eq 0 ] ; then + echo "Necessary tools installed!" + echo "" + else + echo "" + exit + fi +} + +# Check if the configuration directory exists. +if [ -d ${dirConfig} ] ; then + echo ${dirConfig} > /dev/null +else + mkdir -p ${dirConfig} +fi + +# Delete the existing configuration. +if [ "$1" == "-r" ] ; then + rm -rf ${dirConfig}/USER + rm -rf ${dirConfig}/PASS + rm -rf ${dirConfig}/SERVER + rm -rf ${dirConfig}/DIR_SERVER + rm -rf ${dirConfig}/DIR + exit +fi + +# Show configuration file +if [ "$1" == "-c" ] ; then + showConfig +# Show configuration file +elif [ "$1" == "-e" ] ; then + editConfig +# Show the version +elif [ "$1" == "-v" ] ; then + showVersion +# Show the help +elif [ "$1" == "-h" ] ; then + showHelp +# Init +elif [ "$1" == "-i" ] ; then + # Start script + clear + echo "" + echo "ks-upa (ks-tools) v${VERSION} (${M_DATE})" + echo "" + checkDependencies + # Ask the user and if it exists, read it from the config. + if [ -f ${dirConfig}/USER ] ; then + user=$(cat ${dirConfig}/USER) + else + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + fi + # Ask the password and if it exists, read it from the config. + if [ -f ${dirConfig}/PASS ] ; then + password=$(cat ${dirConfig}/PASS) + else + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + fi + # Ask the server URL and if it exists, read it from the config. + if [ -f ${dirConfig}/SERVER ] ; then + server=$(cat ${dirConfig}/SERVER) + else + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + fi + # Ask the path on the server and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR_SERVER ] ; then + dirServer=$(cat ${dirConfig}/DIR_SERVER) + else + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + fi + # Ask the local path and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR ] ; then + dirLocal=$(cat ${dirConfig}/DIR) + else + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + fi + + # Call the functions to perform the whole process. + echo -n "Scanning ${dirLocal} " && sleep 4 + echo "" + if [ -d ${dirLocal} ] ; then + listArchives "${dirLocal}" + totalFiles=$(countArchives) + count=1 + echo "${totalFiles} audio file(s) found!" + echo "" + while [ ${count} -le ${totalFiles} ] ; do + fullNameFile=$(showFile ${count}) + fullPathFile=$(showPathFile ${count}) + echo "Uploading '${fullNameFile}' " + sendFile ${password} ${fullPathFile} ${user} ${server} ${dirServer} ${fullNameFile} + checkChecksum ${password} ${fullPathFile} ${user} ${server} ${dirServer}/${fullNameFile} + echo "" + count=$(expr ${count} + 1) + done + else + echo "Directory ${dirLocal} does not exist!" + echo "" + exit + fi +# Show help +else + showHelp +fi diff --git a/src/ks-upf b/src/ks-upf new file mode 100755 index 0000000..d3a03d9 --- /dev/null +++ b/src/ks-upf @@ -0,0 +1,462 @@ +#!/bin/bash + +###################################################################### +# ks-upf (ks-tools) - Upload common file(s) to server with rsync+ssh # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +###################################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters. +dirTemp="/tmp" +listCompTemp="ks-tools.list-full" +listCompTempTest="ks-tools.list-full.test" +listTemp="ks-tools.list" +dirConfig="$HOME/.ks-tools" +ksToolsTempFolder="/tmp/ks-tools" + +# Function to remove spaces and symbols +# Sintaxis: convertText "" +function convertText() { + wordToConvert=${1} + ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt" + mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null + echo "${wordToConvert}" > ${ksToolsSedFile} + # Borrar espacios + sed -i 's/ /_/g' "${ksToolsSedFile}" &> /dev/null + # Borrar simbolos + symbolsList="[ ] @ { } | \ / ~ # $ % & ? ¿ = ( ) < > ! ¡" + for findSymbol in ${symbolsList} ; do + sed -i "s/${findSymbol}//g" "${ksToolsSedFile}" &> /dev/null + done + # Borrar el resto de simbolos + sed -i 's/*//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/"//g' "${ksToolsSedFile}" &> /dev/null + sed -i "s/^//g" "${ksToolsSedFile}" &> /dev/null + # Cambiar algunos simbolos + sed -i 's/+/_/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/:/-/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/;/-/g' "${ksToolsSedFile}" &> /dev/null + # Mostrar texto convertido + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to list the files of a directory. +# Sintax: listArchives +function listArchives() { + fail=1 + formatFiles="" + videoFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob" + formatFiles="${formatFiles} ${videoFiles}" + audioFiles="mp3 wma ogg wav flac midi acc oga opus mp2 ra m4a mka" + formatFiles="${formatFiles} ${audioFiles}" + compressFiles="zip rar 7z bz2 gz xz tar bzip2 gzip" + formatFiles="${formatFiles} ${compressFiles}" + open_libreOfficeFiles="odt ods odp odb odg" + formatFiles="${formatFiles} ${open_libreOfficeFiles}" + OfficeFiles="doc docx dotx dot docm dic thmx xlsx ppt pps pst mbd accdb msg" + formatFiles="${formatFiles} ${OfficeFiles}" + documentFiles="txt pdf rtf html php xml json css" + formatFiles="${formatFiles} ${documentFiles}" + pictureFiles="png jpg jpeg gif bmp raw xcf ppm pnm tiff ico tga pbm pgm svg psd" + formatFiles="${formatFiles} ${pictureFiles}" + executableFiles="exe msi run bin elf dmg deb rpm tgz ebuild flatpakref snap sh bash cmd bat AppImage jar" + formatFiles="${formatFiles} ${executableFiles}" + otherFiles="desktop lnk blend db c cpp cxx cc py rb dll so link network service ko list java" + formatFiles="${formatFiles} ${otherFiles}" + # Rename files + cd "${1}" + mkdir -p ${ksToolsTempFolder} + ls -1 > ${ksToolsTempFolder}/rename-files.txt + count=1 + sizeFile=$(cat ${ksToolsTempFolder}/rename-files.txt | wc -l) + while [ ${count} -le ${sizeFile} ] ; do + fileToRename=$(cat ${ksToolsTempFolder}/rename-files.txt | head -${count} | tail -1) + fileRenamed=$(convertText "${fileToRename}") + if [ "${fileRenamed}" == "${fileToRename}" ] ; then + echo "null" > /dev/null + else + mv "${fileToRename}" "${fileRenamed}" + fi + count=$(expr $count + 1) + done + # Scan audio files + rm -rf ${dirTemp}/${listTemp} + rm -rf ${dirTemp}/${listCompTemp} + for format in ${formatFiles} ; do + find "${1}"/*.${format} &> ${dirTemp}/${listCompTempTest} + if [ $? -ne 0 ] ; then + echo "null" > /dev/null + else + find "${1}"/*.${format} &>> ${dirTemp}/${listCompTemp} + echo "File(s) in .${format} format found!" + cd "${1}" && ls -1 *.${format} &>> ${dirTemp}/${listTemp} + fail=0 + fi + done + if [ ${fail} -eq 1 ] ; then + echo "No common file(s) found!" + echo "" + exit + else + echo "" + fi +} + +# Function to count the found files. +function countArchives() { + totalArchives=$(cat ${dirTemp}/${listCompTemp} | wc -l) + echo ${totalArchives} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + FileName=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + FileNoExtension=$(echo $FileName | cut -d "." -f 1) + echo $FileNoExtension > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Function to show the name of the file. +# Sintax: showFile +function showFile() { + archive=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + echo ${archive} +} + +# Function to show full file path. +# Sintax: showPathFile +function showPathFile() { + pathFile=$(cat ${dirTemp}/${listCompTemp} | head -${1} | tail -1) + echo ${pathFile} +} + +# Function to send file to server. +# Sintax: sendFile [file name] +function sendFile() { + correct=0 + countSend=0 + while [ ${correct} -eq 0 ] ; do + #sshpass -p ${1} scp ${2} ${3}@${4}:${5} &> /dev/null + comandOne="sshpass -p ${1} rsync -azL -e" + comandTwo="--progress ${2} ${3}@${4}:${5}" + ${comandOne} "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" ${comandTwo} 2> /dev/null + sshpass -p ${1} ssh ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "Failure to send ${2}" + echo "Retrying..." + sshpass -p ${1} ssh ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null + sleep 5 + countSend=$(expr ${countSend} + 1) + if [ ${countSend} -eq 5 ] ; then + correct=1 + fi + else + correct=1 + fi + done +} + +# Function to check local and server checksum. +# Sintax: checkChecksum +function checkChecksum() { + correct=0 + countChecksum=0 + echo -n "Checking checksum... " && sleep 4 + echo "" + while [ ${correct} -eq 0 ] ; do + checksumServer=$(sshpass -p ${1} ssh ${3}@${4} md5sum ${5} 2> /dev/null) + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "Failed to get checksum for ${5}" + echo "Retrying..." + sleep 5 + countChecksum=$(expr ${countChecksum} + 1) + if [ ${countChecksum} -eq 5 ] ; then + correct=1 + fi + else + checksumLocal=$(md5sum ${2}) + echo "Local checksum: $checksumLocal" + echo "Server checksum: $checksumServer" + correct=1 + fi + done +} + +# Function to show config +function showConfig() { + configAvailable=0 + echo "" + echo "ks-upf (ks-tools) v${VERSION} (${M_DATE})" + echo "" + if [ -f ${dirConfig}/USER ] ; then + showUser=$(cat ${dirConfig}/USER) + echo "Server User: ${showUser}" + configAvailable=1 + fi + if [ -f ${dirConfig}/PASS ] ; then + showPassword=$(cat ${dirConfig}/PASS) + echo "Server Password: ${showPassword}" + configAvailable=1 + fi + if [ -f ${dirConfig}/SERVER ] ; then + showServer=$(cat ${dirConfig}/SERVER) + echo "URL (or IP) Server: ${showServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR_SERVER ] ; then + showDirServer=$(cat ${dirConfig}/DIR_SERVER) + echo "Destination Path (Server): ${showDirServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR ] ; then + showDirLocal=$(cat ${dirConfig}/DIR) + echo "Scan Path (Local): ${showDirLocal}" + configAvailable=1 + fi + if [ ${configAvailable} -eq 0 ] ; then + echo "The configuration file does not exist!" + fi + echo "" + exit +} + +# Function to edit the configuration file +function editConfig() { + editConfig=0 + while [ ${editConfig} -eq 0 ] ; do + clear + editUser=$(cat ${dirConfig}/USER 2> /dev/null) + editPassword=$(cat ${dirConfig}/PASS 2> /dev/null) + editServer=$(cat ${dirConfig}/SERVER 2> /dev/null) + editDirServer=$(cat ${dirConfig}/DIR_SERVER 2> /dev/null) + editDirLocal=$(cat ${dirConfig}/DIR 2> /dev/null) + echo "" + echo "ks-upf (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "1 - Edit User (${editUser})" + echo "2 - Edit Password (${editPassword})" + echo "3 - Edit Server (${editServer})" + echo "4 - Edit Dest. Path (${editDirServer})" + echo "5 - Edit Local Path (${editDirLocal})" + echo "" + echo "6 - Exit" + echo "" + echo -n "Choose an option: " ; read EDIT + echo "" + if [ "${EDIT}" == "1" ] ; then + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + elif [ "${EDIT}" == "2" ] ; then + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + elif [ "${EDIT}" == "3" ] ; then + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + elif [ "${EDIT}" == "4" ] ; then + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + elif [ "${EDIT}" == "5" ] ; then + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + elif [ "${EDIT}" == "6" ] ; then + editConfig=1 + else + echo "Invalid option!" + echo -n "Press ENTER to continue " ; read CONTINUE + fi + done + exit +} + +# Function to show version +function showVersion() { + echo "" + echo "ks-upf (ks-tools) v${VERSION} (${M_DATE})" + echo "" + exit +} + +# Function to show help +function showHelp() { + echo "" + echo "ks-upf (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "Upload common file(s) to server with rsync+ssh" + echo "" + echo "Sintax:" + echo "" + echo "ks-upf -i - Start upload" + echo "ks-upf -r - Remove configuration" + echo "ks-upf -c - Show configuration" + echo "ks-upf -e - Edit configuration" + echo "ks-upf -v - Show version" + echo "ks-upf -h - Show help" + echo "" + exit +} + +# Function to check if all the necessary tools +# for the execution are installed. +function checkDependencies() { + dependence=0 + echo -n "Checking necessary tools... " + sleep 3 && echo "" + sshpass -h &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'sshpass' tool is not installed!" + dependence=1 + fi + md5sum --help &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'md5sum' tool is not installed!" + dependence=1 + fi + rsync --version &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'rsync' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/scp ] ; then + echo "OK" > /dev/null + else + echo "The 'scp' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/ssh ] ; then + echo "OK" > /dev/null + else + echo "The 'ssh' tool is not installed!" + dependence=1 + fi + if [ ${dependence} -eq 0 ] ; then + echo "Necessary tools installed!" + echo "" + else + echo "" + exit + fi +} + +# Check if the configuration directory exists. +if [ -d ${dirConfig} ] ; then + echo ${dirConfig} > /dev/null +else + mkdir -p ${dirConfig} +fi + +# Delete the existing configuration. +if [ "$1" == "-r" ] ; then + rm -rf ${dirConfig}/USER + rm -rf ${dirConfig}/PASS + rm -rf ${dirConfig}/SERVER + rm -rf ${dirConfig}/DIR_SERVER + rm -rf ${dirConfig}/DIR + exit +fi + +# Show configuration file +if [ "$1" == "-c" ] ; then + showConfig +# Show configuration file +elif [ "$1" == "-e" ] ; then + editConfig +# Show the version +elif [ "$1" == "-v" ] ; then + showVersion +# Show the help +elif [ "$1" == "-h" ] ; then + showHelp +# Init +elif [ "$1" == "-i" ] ; then + # Start script + clear + echo "" + echo "ks-upf (ks-tools) v${VERSION} (${M_DATE})" + echo "" + checkDependencies + # Ask the user and if it exists, read it from the config. + if [ -f ${dirConfig}/USER ] ; then + user=$(cat ${dirConfig}/USER) + else + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + fi + # Ask the password and if it exists, read it from the config. + if [ -f ${dirConfig}/PASS ] ; then + password=$(cat ${dirConfig}/PASS) + else + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + fi + # Ask the server URL and if it exists, read it from the config. + if [ -f ${dirConfig}/SERVER ] ; then + server=$(cat ${dirConfig}/SERVER) + else + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + fi + # Ask the path on the server and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR_SERVER ] ; then + dirServer=$(cat ${dirConfig}/DIR_SERVER) + else + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + fi + # Ask the local path and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR ] ; then + dirLocal=$(cat ${dirConfig}/DIR) + else + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + fi + + # Call the functions to perform the whole process. + echo -n "Scanning ${dirLocal} " && sleep 4 + echo "" + if [ -d ${dirLocal} ] ; then + listArchives "${dirLocal}" + totalFiles=$(countArchives) + count=1 + echo "${totalFiles} common file(s) found!" + echo "" + while [ ${count} -le ${totalFiles} ] ; do + fullNameFile=$(showFile ${count}) + fullPathFile=$(showPathFile ${count}) + echo "Uploading '${fullNameFile}' " + sendFile ${password} ${fullPathFile} ${user} ${server} ${dirServer} ${fullNameFile} + checkChecksum ${password} ${fullPathFile} ${user} ${server} ${dirServer}/${fullNameFile} + echo "" + count=$(expr ${count} + 1) + done + else + echo "Directory ${dirLocal} does not exist!" + echo "" + exit + fi +# Show help +else + showHelp +fi diff --git a/src/ks-upr b/src/ks-upr new file mode 100755 index 0000000..c2fea0f --- /dev/null +++ b/src/ks-upr @@ -0,0 +1,407 @@ +#!/bin/bash + +########################################################################### +# ks-upr (ks-tools) - Upload recursively file(s) to server with rsync+ssh # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +########################################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters. +dirTemp="/tmp" +listCompTemp="ks-tools.list-full" +listCompTempTest="ks-tools.list-full.test" +listTemp="ks-tools.list" +dirConfig="$HOME/.ks-tools" +ksToolsTempFolder="/tmp/ks-tools" + +# Function to remove spaces and symbols +# Sintaxis: convertText "" +function convertText() { + wordToConvert=${1} + ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt" + mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null + echo "${wordToConvert}" > ${ksToolsSedFile} + # Borrar espacios + sed -i 's/ /_/g' "${ksToolsSedFile}" &> /dev/null + # Borrar simbolos + symbolsList="[ ] @ { } | \ / ~ # $ % & ? ¿ = ( ) < > ! ¡" + for findSymbol in ${symbolsList} ; do + sed -i "s/${findSymbol}//g" "${ksToolsSedFile}" &> /dev/null + done + # Borrar el resto de simbolos + sed -i 's/*//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/"//g' "${ksToolsSedFile}" &> /dev/null + sed -i "s/^//g" "${ksToolsSedFile}" &> /dev/null + # Cambiar algunos simbolos + sed -i 's/+/_/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/:/-/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/;/-/g' "${ksToolsSedFile}" &> /dev/null + # Mostrar texto convertido + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to list the files of a directory. +# Sintax: listArchives +function listArchives() { + fail=1 + formatFiles="" + videoFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob" + formatFiles="${formatFiles} ${videoFiles}" + audioFiles="mp3 wma ogg wav flac midi acc oga opus mp2 ra m4a mka" + formatFiles="${formatFiles} ${audioFiles}" + compressFiles="zip rar 7z bz2 gz xz tar bzip2 gzip" + formatFiles="${formatFiles} ${compressFiles}" + open_libreOfficeFiles="odt ods odp odb odg" + formatFiles="${formatFiles} ${open_libreOfficeFiles}" + OfficeFiles="doc docx dotx dot docm dic thmx xlsx ppt pps pst mbd accdb msg" + formatFiles="${formatFiles} ${OfficeFiles}" + documentFiles="txt pdf rtf html php xml json css" + formatFiles="${formatFiles} ${documentFiles}" + pictureFiles="png jpg jpeg gif bmp raw xcf ppm pnm tiff ico tga pbm pgm svg psd" + formatFiles="${formatFiles} ${pictureFiles}" + executableFiles="exe msi run bin elf dmg deb rpm tgz ebuild flatpakref snap sh bash cmd bat AppImage jar" + formatFiles="${formatFiles} ${executableFiles}" + otherFiles="desktop lnk blend db c cpp cxx cc py rb dll so link network service ko list java" + formatFiles="${formatFiles} ${otherFiles}" + # Rename files + cd "${1}" + mkdir -p ${ksToolsTempFolder} + ls -1 > ${ksToolsTempFolder}/rename-files.txt + count=1 + sizeFile=$(cat ${ksToolsTempFolder}/rename-files.txt | wc -l) + while [ ${count} -le ${sizeFile} ] ; do + fileToRename=$(cat ${ksToolsTempFolder}/rename-files.txt | head -${count} | tail -1) + fileRenamed=$(convertText "${fileToRename}") + if [ "${fileRenamed}" == "${fileToRename}" ] ; then + echo "null" > /dev/null + else + mv "${fileToRename}" "${fileRenamed}" + fi + count=$(expr $count + 1) + done + # Scan audio files + rm -rf ${dirTemp}/${listTemp} + rm -rf ${dirTemp}/${listCompTemp} + for format in ${formatFiles} ; do + find "${1}"/*.${format} &> ${dirTemp}/${listCompTempTest} + if [ $? -ne 0 ] ; then + echo "null" > /dev/null + else + find "${1}"/*.${format} &>> ${dirTemp}/${listCompTemp} + echo "File(s) in .${format} format found!" + cd "${1}" && ls -1 *.${format} &>> ${dirTemp}/${listTemp} + fail=0 + fi + done + if [ ${fail} -eq 1 ] ; then + echo "No common file(s) found!" + echo "" + exit + else + echo "" + fi +} + +# Function to count the found files. +function countArchives() { + totalArchives=$(cat ${dirTemp}/${listCompTemp} | wc -l) + echo ${totalArchives} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + FileName=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + FileNoExtension=$(echo $FileName | cut -d "." -f 1) + echo $FileNoExtension > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Function to show the name of the file. +# Sintax: showFile +function showFile() { + archive=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + echo ${archive} +} + +# Function to show full file path. +# Sintax: showPathFile +function showPathFile() { + pathFile=$(cat ${dirTemp}/${listCompTemp} | head -${1} | tail -1) + echo ${pathFile} +} + +# Function to send file to server. +# Sintax: sendFile [file name] +function sendFile() { + #sshpass -p ${1} scp ${2} ${3}@${4}:${5} &> /dev/null + comandOne="sshpass -p ${1} rsync -azlr -e" + comandTwo="--progress ${2}/ ${3}@${4}:${5}/" + ${comandOne} "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" ${comandTwo} 2> /dev/null + sshpass -p ${1} ssh ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null +} + +# Function to show config +function showConfig() { + configAvailable=0 + echo "" + echo "ks-upr (ks-tools) v${VERSION} (${M_DATE})" + echo "" + if [ -f ${dirConfig}/USER ] ; then + showUser=$(cat ${dirConfig}/USER) + echo "Server User: ${showUser}" + configAvailable=1 + fi + if [ -f ${dirConfig}/PASS ] ; then + showPassword=$(cat ${dirConfig}/PASS) + echo "Server Password: ${showPassword}" + configAvailable=1 + fi + if [ -f ${dirConfig}/SERVER ] ; then + showServer=$(cat ${dirConfig}/SERVER) + echo "URL (or IP) Server: ${showServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR_SERVER ] ; then + showDirServer=$(cat ${dirConfig}/DIR_SERVER) + echo "Destination Path (Server): ${showDirServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR ] ; then + showDirLocal=$(cat ${dirConfig}/DIR) + echo "Scan Path (Local): ${showDirLocal}" + configAvailable=1 + fi + if [ ${configAvailable} -eq 0 ] ; then + echo "The configuration file does not exist!" + fi + echo "" + exit +} + +# Function to edit the configuration file +function editConfig() { + editConfig=0 + while [ ${editConfig} -eq 0 ] ; do + clear + editUser=$(cat ${dirConfig}/USER 2> /dev/null) + editPassword=$(cat ${dirConfig}/PASS 2> /dev/null) + editServer=$(cat ${dirConfig}/SERVER 2> /dev/null) + editDirServer=$(cat ${dirConfig}/DIR_SERVER 2> /dev/null) + editDirLocal=$(cat ${dirConfig}/DIR 2> /dev/null) + echo "" + echo "ks-upr (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "1 - Edit User (${editUser})" + echo "2 - Edit Password (${editPassword})" + echo "3 - Edit Server (${editServer})" + echo "4 - Edit Dest. Path (${editDirServer})" + echo "5 - Edit Local Path (${editDirLocal})" + echo "" + echo "6 - Exit" + echo "" + echo -n "Choose an option: " ; read EDIT + echo "" + if [ "${EDIT}" == "1" ] ; then + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + elif [ "${EDIT}" == "2" ] ; then + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + elif [ "${EDIT}" == "3" ] ; then + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + elif [ "${EDIT}" == "4" ] ; then + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + elif [ "${EDIT}" == "5" ] ; then + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + elif [ "${EDIT}" == "6" ] ; then + editConfig=1 + else + echo "Invalid option!" + echo -n "Press ENTER to continue " ; read CONTINUE + fi + done + exit +} + +# Function to show version +function showVersion() { + echo "" + echo "ks-upr (ks-tools) v${VERSION} (${M_DATE})" + echo "" + exit +} + +# Function to show help +function showHelp() { + echo "" + echo "ks-upr (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "Upload recursively file(s) to server with rsync+ssh" + echo "" + echo "Sintax:" + echo "" + echo "ks-upf -i - Start upload" + echo "ks-upf -r - Remove configuration" + echo "ks-upf -c - Show configuration" + echo "ks-upf -e - Edit configuration" + echo "ks-upf -v - Show version" + echo "ks-upf -h - Show help" + echo "" + exit +} + +# Function to check if all the necessary tools +# for the execution are installed. +function checkDependencies() { + dependence=0 + echo -n "Checking necessary tools... " + sleep 3 && echo "" + sshpass -h &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'sshpass' tool is not installed!" + dependence=1 + fi + md5sum --help &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'md5sum' tool is not installed!" + dependence=1 + fi + rsync --version &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'rsync' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/scp ] ; then + echo "OK" > /dev/null + else + echo "The 'scp' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/ssh ] ; then + echo "OK" > /dev/null + else + echo "The 'ssh' tool is not installed!" + dependence=1 + fi + if [ ${dependence} -eq 0 ] ; then + echo "Necessary tools installed!" + echo "" + else + echo "" + exit + fi +} + +# Check if the configuration directory exists. +if [ -d ${dirConfig} ] ; then + echo ${dirConfig} > /dev/null +else + mkdir -p ${dirConfig} +fi + +# Delete the existing configuration. +if [ "$1" == "-r" ] ; then + rm -rf ${dirConfig}/USER + rm -rf ${dirConfig}/PASS + rm -rf ${dirConfig}/SERVER + rm -rf ${dirConfig}/DIR_SERVER + rm -rf ${dirConfig}/DIR + exit +fi + +# Show configuration file +if [ "$1" == "-c" ] ; then + showConfig + # Show configuration file +elif [ "$1" == "-e" ] ; then + editConfig + # Show the version +elif [ "$1" == "-v" ] ; then + showVersion + # Show the help +elif [ "$1" == "-h" ] ; then + showHelp + # Init +elif [ "$1" == "-i" ] ; then + # Start script + clear + echo "" + echo "ks-upr (ks-tools) v${VERSION} (${M_DATE})" + echo "" + checkDependencies + # Ask the user and if it exists, read it from the config. + if [ -f ${dirConfig}/USER ] ; then + user=$(cat ${dirConfig}/USER) + else + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + fi + # Ask the password and if it exists, read it from the config. + if [ -f ${dirConfig}/PASS ] ; then + password=$(cat ${dirConfig}/PASS) + else + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + fi + # Ask the server URL and if it exists, read it from the config. + if [ -f ${dirConfig}/SERVER ] ; then + server=$(cat ${dirConfig}/SERVER) + else + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + fi + # Ask the path on the server and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR_SERVER ] ; then + dirServer=$(cat ${dirConfig}/DIR_SERVER) + else + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + fi + # Ask the local path and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR ] ; then + dirLocal=$(cat ${dirConfig}/DIR) + else + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + fi + + # Call the functions to perform the whole process. + echo -n "Scanning ${dirLocal} " && sleep 4 + echo "" + if [ -d ${dirLocal} ] ; then + sendFile ${password} "${dirLocal}" ${user} ${server} ${dirServer} + echo "" + count=$(expr ${count} + 1) + else + echo "Directory ${dirLocal} does not exist!" + echo "" + exit + fi + # Show help +else + showHelp +fi diff --git a/src/ks-upv b/src/ks-upv new file mode 100755 index 0000000..96fa11c --- /dev/null +++ b/src/ks-upv @@ -0,0 +1,444 @@ +#!/bin/bash + +###################################################################### +# ks-upv (ks-tools) - Upload videos file(s) to server with rynsc+ssh # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +###################################################################### +VERSION="6.6" +M_DATE="100221" + +# Global parameters. +dirTemp="/tmp" +listCompTemp="ks-tools.list-full" +listCompTempTest="ks-tools.list-full.test" +listTemp="ks-tools.list" +dirConfig="$HOME/.ks-tools" +ksToolsTempFolder="/tmp/ks-tools" + +# Function to remove spaces and symbols +# Sintaxis: convertText "" +function convertText() { + wordToConvert=${1} + ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt" + mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null + echo "${wordToConvert}" > ${ksToolsSedFile} + # Borrar espacios + sed -i 's/ /_/g' "${ksToolsSedFile}" &> /dev/null + # Borrar simbolos + symbolsList="[ ] @ { } | \ / ~ # $ % & ? ¿ = ( ) < > ! ¡" + for findSymbol in ${symbolsList} ; do + sed -i "s/${findSymbol}//g" "${ksToolsSedFile}" &> /dev/null + done + # Borrar el resto de simbolos + sed -i 's/*//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/"//g' "${ksToolsSedFile}" &> /dev/null + sed -i "s/^//g" "${ksToolsSedFile}" &> /dev/null + # Cambiar algunos simbolos + sed -i 's/+/_/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/:/-/g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/;/-/g' "${ksToolsSedFile}" &> /dev/null + # Mostrar texto convertido + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to list the files of a directory. +# Sintax: listArchives +function listArchives() { + fail=1 + formatFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob" + # Rename files + cd "${1}" + mkdir -p ${ksToolsTempFolder} + ls -1 > ${ksToolsTempFolder}/rename-files.txt + count=1 + sizeFile=$(cat ${ksToolsTempFolder}/rename-files.txt | wc -l) + while [ ${count} -le ${sizeFile} ] ; do + fileToRename=$(cat ${ksToolsTempFolder}/rename-files.txt | head -${count} | tail -1) + fileRenamed=$(convertText "${fileToRename}") + if [ "${fileRenamed}" == "${fileToRename}" ] ; then + echo "null" > /dev/null + else + mv "${fileToRename}" "${fileRenamed}" + fi + count=$(expr $count + 1) + done + # Scan video files + rm -rf ${dirTemp}/${listTemp} + rm -rf ${dirTemp}/${listCompTemp} + for format in ${formatFiles} ; do + find "${1}"/*.${format} &> ${dirTemp}/${listCompTempTest} + if [ $? -ne 0 ] ; then + echo "null" > /dev/null + else + find "${1}"/*.${format} &>> ${dirTemp}/${listCompTemp} + echo "Video file(s) in .${format} found!" + cd "${1}" && ls -1 *.${format} &>> ${dirTemp}/${listTemp} + fail=0 + fi + done + if [ ${fail} -eq 1 ] ; then + echo "No video file(s) found!" + echo "" + exit + else + echo "" + fi +} + +# Function to count the found files. +function countArchives() { + totalArchives=$(cat ${dirTemp}/${listCompTemp} | wc -l) + echo ${totalArchives} +} + +# Function to show files with spaces. +# Sintax: showFileWithSpace +function showFileWithSpace() { + FileName=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + FileNoExtension=$(echo $FileName | cut -d "." -f 1) + echo $FileNoExtension > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Function to show the name of the file. +# Sintax: showFile +function showFile() { + archive=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1) + echo ${archive} +} + +# Function to show full file path. +# Sintax: showPathFile +function showPathFile() { + pathFile=$(cat ${dirTemp}/${listCompTemp} | head -${1} | tail -1) + echo ${pathFile} +} + +# Function to send file to server. +# Sintax: sendFile [file name] +function sendFile() { + correct=0 + countSend=0 + while [ ${correct} -eq 0 ] ; do + #sshpass -p ${1} scp ${2} ${3}@${4}:${5} &> /dev/null + comandOne="sshpass -p ${1} rsync -azL -e" + comandTwo="--progress ${2} ${3}@${4}:${5}" + ${comandOne} "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" ${comandTwo} 2> /dev/null + sshpass -p ${1} ssh ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "Failure to send ${2}" + echo "Retrying..." + sshpass -p ${1} ssh ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null + sleep 5 + countSend=$(expr ${countSend} + 1) + if [ ${countSend} -eq 5 ] ; then + correct=1 + fi + else + correct=1 + fi + done +} + +# Function to check local and server checksum. +# Sintax: checkChecksum +function checkChecksum() { + correct=0 + countChecksum=0 + echo -n "Checking checksum... " && sleep 4 + echo "" + while [ ${correct} -eq 0 ] ; do + checksumServer=$(sshpass -p ${1} ssh ${3}@${4} md5sum ${5} 2> /dev/null) + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "Failed to get checksum for ${5}" + echo "Retrying..." + sleep 5 + countChecksum=$(expr ${countChecksum} + 1) + if [ ${countChecksum} -eq 5 ] ; then + correct=1 + fi + else + checksumLocal=$(md5sum ${2}) + echo "Local checksum: $checksumLocal" + echo "Server checksum: $checksumServer" + correct=1 + fi + done +} + +# Function to show config +function showConfig() { + configAvailable=0 + echo "" + echo "ks-upv (ks-tools) v${VERSION} (${M_DATE})" + echo "" + if [ -f ${dirConfig}/USER ] ; then + showUser=$(cat ${dirConfig}/USER) + echo "Server User: ${showUser}" + configAvailable=1 + fi + if [ -f ${dirConfig}/PASS ] ; then + showPassword=$(cat ${dirConfig}/PASS) + echo "Server Password: ${showPassword}" + configAvailable=1 + fi + if [ -f ${dirConfig}/SERVER ] ; then + showServer=$(cat ${dirConfig}/SERVER) + echo "URL (or IP) Server: ${showServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR_SERVER ] ; then + showDirServer=$(cat ${dirConfig}/DIR_SERVER) + echo "Destination Path (Server): ${showDirServer}" + configAvailable=1 + fi + if [ -f ${dirConfig}/DIR ] ; then + showDirLocal=$(cat ${dirConfig}/DIR) + echo "Scan Path (Local): ${showDirLocal}" + configAvailable=1 + fi + if [ ${configAvailable} -eq 0 ] ; then + echo "The configuration file does not exist!" + fi + echo "" + exit +} + +# Function to edit the configuration file +function editConfig() { + editConfig=0 + while [ ${editConfig} -eq 0 ] ; do + clear + editUser=$(cat ${dirConfig}/USER 2> /dev/null) + editPassword=$(cat ${dirConfig}/PASS 2> /dev/null) + editServer=$(cat ${dirConfig}/SERVER 2> /dev/null) + editDirServer=$(cat ${dirConfig}/DIR_SERVER 2> /dev/null) + editDirLocal=$(cat ${dirConfig}/DIR 2> /dev/null) + echo "" + echo "ks-upv (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "1 - Edit User (${editUser})" + echo "2 - Edit Password (${editPassword})" + echo "3 - Edit Server (${editServer})" + echo "4 - Edit Dest. Path (${editDirServer})" + echo "5 - Edit Local Path (${editDirLocal})" + echo "" + echo "6 - Exit" + echo "" + echo -n "Choose an option: " ; read EDIT + echo "" + if [ "${EDIT}" == "1" ] ; then + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + elif [ "${EDIT}" == "2" ] ; then + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + elif [ "${EDIT}" == "3" ] ; then + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + elif [ "${EDIT}" == "4" ] ; then + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + elif [ "${EDIT}" == "5" ] ; then + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + elif [ "${EDIT}" == "6" ] ; then + editConfig=1 + else + echo "Invalid option!" + echo -n "Press ENTER to continue " ; read CONTINUE + fi + done + exit +} + +# Function to show version +function showVersion() { + echo "" + echo "ks-upv (ks-tools) v${VERSION} (${M_DATE})" + echo "" + exit +} + +# Function to show help +function showHelp() { + echo "" + echo "ks-upv (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "Upload videos file(s) to server with rynsc+ssh" + echo "" + echo "Sintax:" + echo "" + echo "ks-upv -i - Start upload" + echo "ks-upv -r - Remove configuration" + echo "ks-upv -c - Show configuration" + echo "ks-upv -e - Edit configuration" + echo "ks-upv -v - Show version" + echo "ks-upv -h - Show help" + echo "" + exit +} + +# Function to check if all the necessary tools +# for the execution are installed. +function checkDependencies() { + dependence=0 + echo -n "Checking necessary tools... " + sleep 3 && echo "" + sshpass -h &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'sshpass' tool is not installed!" + dependence=1 + fi + md5sum --help &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'md5sum' tool is not installed!" + dependence=1 + fi + rsync --version &> /dev/null + OUTPUT=$? + if [ ${OUTPUT} -ne 0 ] ; then + echo "The 'rsync' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/scp ] ; then + echo "OK" > /dev/null + else + echo "The 'scp' tool is not installed!" + dependence=1 + fi + if [ -f /usr/bin/ssh ] ; then + echo "OK" > /dev/null + else + echo "The 'ssh' tool is not installed!" + dependence=1 + fi + if [ ${dependence} -eq 0 ] ; then + echo "Necessary tools installed!" + echo "" + else + echo "" + exit + fi +} + +# Check if the configuration directory exists. +if [ -d ${dirConfig} ] ; then + echo ${dirConfig} > /dev/null +else + mkdir -p ${dirConfig} +fi + +# Delete the existing configuration. +if [ "$1" == "-r" ] ; then + rm -rf ${dirConfig}/USER + rm -rf ${dirConfig}/PASS + rm -rf ${dirConfig}/SERVER + rm -rf ${dirConfig}/DIR_SERVER + rm -rf ${dirConfig}/DIR + exit +fi + +# Show configuration file +if [ "$1" == "-c" ] ; then + showConfig + # Show configuration file +elif [ "$1" == "-e" ] ; then + editConfig + # Show the version +elif [ "$1" == "-v" ] ; then + showVersion + # Show the help +elif [ "$1" == "-h" ] ; then + showHelp + # Init +elif [ "$1" == "-i" ] ; then + # Start script + clear + echo "" + echo "ks-upv (ks-tools) v${VERSION} (${M_DATE})" + echo "" + checkDependencies + # Ask the user and if it exists, read it from the config. + if [ -f ${dirConfig}/USER ] ; then + user=$(cat ${dirConfig}/USER) + else + echo -n "Enter the server user: " ; read USER + user=${USER} + echo ${user} > ${dirConfig}/USER + fi + # Ask the password and if it exists, read it from the config. + if [ -f ${dirConfig}/PASS ] ; then + password=$(cat ${dirConfig}/PASS) + else + echo -n "Enter the server key: " ; read PASS + password=${PASS} + echo ${password} > ${dirConfig}/PASS + fi + # Ask the server URL and if it exists, read it from the config. + if [ -f ${dirConfig}/SERVER ] ; then + server=$(cat ${dirConfig}/SERVER) + else + echo -n "Enter the server URL: " ; read SERVER + server=${SERVER} + echo ${server} > ${dirConfig}/SERVER + fi + # Ask the path on the server and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR_SERVER ] ; then + dirServer=$(cat ${dirConfig}/DIR_SERVER) + else + echo -n "Enter the path on the server: " ; read DIR_SERVER + dirServer=${DIR_SERVER} + echo ${dirServer} > ${dirConfig}/DIR_SERVER + fi + # Ask the local path and if it exists, read it from the config. + if [ -f ${dirConfig}/DIR ] ; then + dirLocal=$(cat ${dirConfig}/DIR) + else + echo -n "Enter the local path to scan: " ; read DIR + dirLocal=${DIR} + echo ${dirLocal} > ${dirConfig}/DIR + fi + + # Call the functions to perform the whole process. + echo -n "Scanning ${dirLocal} " && sleep 4 + echo "" + if [ -d ${dirLocal} ] ; then + listArchives "${dirLocal}" + totalFiles=$(countArchives) + count=1 + echo "${totalFiles} video file(s) found!" + echo "" + while [ ${count} -le ${totalFiles} ] ; do + fullNameFile=$(showFile ${count}) + fullPathFile=$(showPathFile ${count}) + echo "Uploading '${fullNameFile}' " + sendFile ${password} ${fullPathFile} ${user} ${server} ${dirServer} ${fullNameFile} + checkChecksum ${password} ${fullPathFile} ${user} ${server} ${dirServer}/${fullNameFile} + echo "" + count=$(expr ${count} + 1) + done + else + echo "Directory ${dirLocal} does not exist!" + echo "" + exit + fi + # Show help +else + showHelp +fi diff --git a/src/ks-vob b/src/ks-vob new file mode 100755 index 0000000..c9ded45 --- /dev/null +++ b/src/ks-vob @@ -0,0 +1,234 @@ +#!/bin/bash + +############################################################### +# ks-avi (ks-tools) - Convert video to VOB DVD Format # +# Date: 10-02-2021 # +# Author: q3aql # +# Contact: q3aql@protonmail.ch # +############################################################### +VERSION="6.6" +M_DATE="100221" + +# Basic parameters +rel_size="720x480" +vcodec="mpeg2video" +b_vcodec="3500k" +acodec="libtwolame" +b_acodec="192k" +default_lang_audio="spa" +v_ext="vob" + +# Check if ffmpeg is installed +ffmpeg_test=$(ffmpeg --help 2>&1) +error_ffmpeg=$? +if [ ${error_ffmpeg} -ne 0 ] ; then + echo "" + echo "* ks-vob (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "+ The 'ffmpeg' tool is not installed!" + echo "" + exit +fi + +# Sintax: ks-vob -f +if [ -z "${1}" ] ; then + echo "" + echo "* ks-vob (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient VOB" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Split: 30 minutes" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-vob " + echo + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-vob (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi +# Parametros de conversion +p_ffmpeg="ffmpeg -i" +p_conversion="-s ${rel_size} -c:v ${vcodec} -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" +p_duracion=$(${p_ffmpeg} ${1} 2>&1 | grep Duration | tr -s " " | cut -f 3 -d ' ' | cut -f 1 -d ":") +p_duracion_min=$(${p_ffmpeg} ${1} 2>&1 | grep Duration | tr -s " " | cut -f 3 -d ' ' | cut -f 2 -d ":") + +# Realizar conversion en base a la duracion del video +if [ -z "${2}" ] ; then + echo "" + echo "* ks-vob (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient VOB" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Split: 30 minutes" + echo " - Container: ${v_ext}" + echo "" + echo "+ Sintax: " + echo "" + echo " $ ks-vob " + echo + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Video Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 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=$(${p_ffmpeg} "${1}" 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 + # Define the resolution + echo -n "* (Default: ${rel_size}) Type the resolution: " ; read resolution + if [ -z "${resolution}" ] ; then + resolution="${rel_size}" + else + rel_size="${resolution}" + p_conversion="-s ${rel_size} -c:v ${vcodec} -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + fi + echo -n "* (Default: n) Do you want apply '-max_muxing_queue_size 9999' patch? (y/n): " ; read patch_thread + if [ "${patch_thread}" == "y" ] ; then + patch_thread="-max_muxing_queue_size 9999" + else + patch_thread="" + fi + # Check audio 5.1 or 7.1 + audio_5_7_1=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1") + audio_5_7_1_2=$(${p_ffmpeg} "${1}" 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 + # Execute commands for conversion + echo "" + echo "* CONFIGURATION THAT WILL BE APPLIED:" + echo "" + echo " + Source file: ${1}" + echo " + Destination: ${2}/VTS_01_*" + echo " + Parameters: -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} ${patch_thread}" + 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 + if [ "${p_duracion}" == "00" ] ; then + mkdir -p "${2}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:00:00 -t 00:30:00 ${patch_thread} "${2}/VTS_01_1.${v_ext}" + if [ "${p_duracion_min}" -gt "30" ] ; then + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:30:01 ${patch_thread} "${2}/VTS_01_2.${v_ext}" + fi + elif [ "${p_duracion}" == "01" ] ; then + mkdir -p "${2}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:00:00 -t 00:30:00 ${patch_thread} "${2}/VTS_01_1.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:30:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_2.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 01:00:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_3.${v_ext}" + if [ "${p_duracion_min}" -gt "30" ] ; then + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 01:30:01 ${patch_thread} "${2}/VTS_01_4.${v_ext}" + fi + elif [ "${p_duracion}" == "02" ] ; then + mkdir -p "${2}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:00:00 -t 00:30:00 ${patch_thread} "${2}/VTS_01_1.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:30:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_2.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 01:00:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_3.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 01:30:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_4.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 02:00:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_5.${v_ext}" + if [ "${p_duracion_min}" -gt "30" ] ; then + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 02:30:01 ${patch_thread} "${2}/VTS_01_6.${v_ext}" + fi + elif [ "${p_duracion}" == "03" ] ; then + mkdir -p "${2}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:00:00 -t 00:30:00 ${patch_thread} "${2}/VTS_01_1.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 00:30:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_2.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 01:00:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_3.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 01:30:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_4.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 02:00:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_5.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 02:30:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_6.${v_ext}" + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 03:00:01 -t 00:29:59 ${patch_thread} "${2}/VTS_01_7.${v_ext}" + if [ "${p_duracion_min}" -gt "30" ] ; then + ${p_ffmpeg} "${1}" -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -ss 03:30:01 ${patch_thread} "${2}/VTS_01_8.${v_ext}" + fi + else + echo "" + echo "* ks-vob (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The video must be less than 4h." + echo "" + exit + fi + fi +fi