From 2a83767b810d348e86683a914e7bb1dcd3dbc37d Mon Sep 17 00:00:00 2001 From: q3aql Date: Wed, 24 Mar 2021 21:14:56 +0100 Subject: [PATCH] Add function for extract video + audio from specific hour-minute-second --- src/ks-mix | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 171 insertions(+), 10 deletions(-) diff --git a/src/ks-mix b/src/ks-mix index 6e7fe02..cb04177 100755 --- a/src/ks-mix +++ b/src/ks-mix @@ -80,8 +80,8 @@ function show_menu() { echo " $ ks-mix -evpf --> Extract video track & set fps (H264/5 & MPEG4)" echo " $ ks-mix -eaud --> Extract audio track" echo " $ ks-mix -esub --> Extract subtitle track (only for subrip/srt)" - echo " $ ks-mix -evti --> Extract video from a specific hour/minute /second" - echo " $ ks-mix -eati --> Extract video from a specific hour/minute /second" + echo " $ ks-mix -evti --> Extract video from a specific hour/minute/second" + echo " $ ks-mix -eati --> Extract video from a specific hour/minute/second" echo " $ ks-mix -evat --> Extract video + audio from specific time" echo " $ ks-mix -chec --> Analyze tracks of video file" echo " $ ks-mix -mixf --> Mix video and audio file" @@ -238,7 +238,7 @@ elif [ "${1}" == "-ev25" ] ; then exit fi elif [ "${1}" == "-evpf" ] ; then - # Run commands for -evfp parameter + # Run commands for -evpf parameter if [ -z "${2}" ] ; then show_menu elif [ -f "${2}" ] ; then @@ -455,9 +455,8 @@ elif [ "${1}" == "-esub" ] ; then echo "" exit fi - elif [ "${1}" == "-evti" ] ; then - # Run commands for -evid parameter + # Run commands for -evti parameter if [ -z "${2}" ] ; then show_menu elif [ -f "${2}" ] ; then @@ -592,9 +591,8 @@ elif [ "${1}" == "-evti" ] ; then echo "" exit fi - elif [ "${1}" == "-eati" ] ; then - # Run commands for -evid parameter + # Run commands for -eati parameter if [ -z "${2}" ] ; then show_menu elif [ -f "${2}" ] ; then @@ -669,7 +667,7 @@ elif [ "${1}" == "-eati" ] ; then echo "" echo -n "* Starting extract audio track (${2}) " sleep 3 - ffmpeg -y -i "${2}" -map ${audio_track} -c:v copy -ss ${hourCut}:${minuteCut}:${secondCut} -c:a copy "${audio_file_dir}" + ffmpeg -y -i "${2}" -map ${audio_track} -c:a copy -ss ${hourCut}:${minuteCut}:${secondCut} "${audio_file_dir}" echo "" echo -n "* Extracted audio into ${audio_file_dir}" sleep 2 @@ -721,7 +719,7 @@ elif [ "${1}" == "-eati" ] ; then echo "" echo -n "* Starting extract audio track (${2}) " sleep 3 - ffmpeg -y -i "${2}" -map ${audio_track} -c:v copy -ss ${hourCut}:${minuteCut}:${secondCut} -t ${hourCutAfter}:${minuteCutAfter}:${secondCutAfter} -c:a copy "${audio_file_dir}" + ffmpeg -y -i "${2}" -map ${audio_track} -c:a copy -ss ${hourCut}:${minuteCut}:${secondCut} -t ${hourCutAfter}:${minuteCutAfter}:${secondCutAfter} "${audio_file_dir}" echo "" echo -n "* Extracted audio into ${audio_file_dir}" sleep 2 @@ -735,7 +733,170 @@ elif [ "${1}" == "-eati" ] ; then echo "" exit fi - +elif [ "${1}" == "-evat" ] ; then + # Run commands for -evat parameter + if [ -z "${2}" ] ; then + show_menu + elif [ -f "${2}" ] ; then + hourCut=0 + minuteCut=1 + secondCut=0 + echo "" + echo "+ Video Tracks:" + ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ffmpeg -i "${2}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + echo "+ audio time:" + ffmpeg -i "${2}" 2>&1 | grep "Duration" | cut -d "," -f 1 | tr -s " " + 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 + # 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 + # Ask for hour to cut + echo -n "* (Default: ${hourCut}) Type the hour (0-99): " ; read hour_cut + if [ -z "${hour_cut}" ] ; then + hourZero=$(zero_number "${hourCut}") + hourCut="${hourZero}" + else + hourZero=$(zero_number "${hour_cut}") + hourCut="${hourZero}" + fi + # Ask for minute to cut + echo -n "* (Default: ${minuteCut}) Type the minute (0-59): " ; read minute_cut + if [ -z "${minute_cut}" ] ; then + minuteZero=$(zero_number "${minuteCut}") + minuteCut="${minuteZero}" + else + minuteZero=$(zero_number "${minute_cut}") + minuteCut="${minuteZero}" + fi + # Ask for second to cut + echo -n "* (Default: ${secondCut}) Type the second (0-59): " ; read second_cut + if [ -z "${second_cut}" ] ; then + secondZero=$(zero_number "${secondCut}") + secondCut="${secondZero}" + else + secondZero=$(zero_number "${second_cut}") + secondCut="${secondZero}" + fi + echo -n "* (Default: n) Extract audio from ${hourCut}:${minuteCut}:${secondCut} to end? (y/n): " ; read accept_extraction + if [ "${accept_extraction}" == "y" ] ; then + echo "" + echo -n "* Starting extract video and audio tracks (${2}) " + sleep 3 + ffmpeg -y -i "${2}" -map ${video_track} -map ${audio_track} -c:v copy -c:a copy -ss ${hourCut}:${minuteCut}:${secondCut} "${output_file_dir}" + echo "" + echo -n "* Extracted video and audio into ${output_file_dir}" + sleep 2 + echo "" && echo "" + exit + else + hourCutAfter=0 + minuteCutAfter=10 + secondCutAfter=0 + # Ask for hour to cut + echo "" + echo -n "* (Default: ${hourCutAfter}) Type how many hours from ${hourCut}:${minuteCut}:${secondCut} (0-99): " ; read hour_cutAfter + if [ -z "${hour_cutAfter}" ] ; then + hourZeroAfter=$(zero_number "${hourCutAfter}") + hourCutAfter="${hourZeroAfter}" + else + hourZeroAfter=$(zero_number "${hour_cutAfter}") + hourCutAfter="${hourZeroAfter}" + fi + # Ask for minute to cut + echo -n "* (Default: ${minuteCutAfter}) Type how many minutes from ${hourCut}:${minuteCut}:${secondCut} (0-59): " ; read minute_cutAfter + if [ -z "${minute_cutAfter}" ] ; then + minuteZeroAfter=$(zero_number "${minuteCutAfter}") + minuteCutAfter="${minuteZeroAfter}" + else + minuteZeroAfter=$(zero_number "${minute_cutAfter}") + minuteCutAfter="${minuteZeroAfter}" + fi + # Ask for second to cut + echo -n "* (Default: ${secondCutAfter}) Type how many minutes from ${hourCut}:${minuteCut}:${secondCut} (0-59): " ; read second_cutAfter + if [ -z "${second_cutAfter}" ] ; then + secondZeroAfter=$(zero_number "${secondCutAfter}") + secondCutAfter="${secondZeroAfter}" + else + secondZeroAfter=$(zero_number "${second_cutAfter}") + secondCutAfter="${secondZeroAfter}" + fi + hourPlusRaw=$(expr ${hourCut} + ${hourCutAfter}) + minutePlusRaw=$(expr ${minuteCut} + ${minuteCutAfter}) + secondPlusRaw=$(expr ${secondCut} + ${secondCutAfter}) + # Convert time total + hourPlus=$(zero_number "${hourPlusRaw}") + minutePlus=$(zero_number "${minutePlusRaw}") + secondPlus=$(zero_number "${secondPlusRaw}") + echo -n "* (Default: y) Extract audio from ${hourCut}:${minuteCut}:${secondCut} to ${hourPlus}:${minutePlus}:${secondPlus}? (y/n): " ; read accept_extraction_advanced + if [ "${accept_extraction_advanced}" == "n" ] ; then + exit + else + echo "" + echo -n "* Starting extract video and audio tracks (${2}) " + sleep 3 + ffmpeg -y -i "${2}" -map ${video_track} -map ${audio_track} -c:v copy -c:a copy -ss ${hourCut}:${minuteCut}:${secondCut} -t ${hourCutAfter}:${minuteCutAfter}:${secondCutAfter} "${output_file_dir}" + echo "" + echo -n "* Extracted video and audio into ${output_file_dir}" + sleep 2 + echo "" && echo "" + exit + fi + fi + 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