Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function cut_audio_mp3() {
- readarray -t mp3_files < <(find ./ -maxdepth 1 -type f -regex '.*\.\(mp3\|flac\)$' | sed "s/.*\///")
- if [ ${#mp3_files[@]} -eq 1 ]; then
- j=0
- for i in "$@"; do
- ((j++))
- if [ $((j%2)) -eq 0 ]; then
- end_time=$i
- MPHR=60
- IFS=':' read -ra start_array <<< "$start_time"
- IFS=':' read -ra end_array <<< "$end_time"
- len_start_min=${#start_array[0]}
- len_start_sec=$(( $len_start_min + 1 ))
- len_end_min=${#end_array[0]}
- len_end_sec=$(( $len_end_min + 1 ))
- start_time_sec=$(( ( 10#${start_time:0:$len_start_min} ) * MPHR + 10#${start_time:$len_start_sec} ))
- duration=$(( ( 10#${end_time:0:$len_end_min} - 10#${start_time:0:$len_start_min} ) * MPHR + 10#${end_time:$len_end_sec} - 10#${start_time:$len_start_sec} ))
- file_name=$(basename "${mp3_files[0]}" | cut -d. -f1)
- file_extension=$(basename "${mp3_files[0]}" | cut -d. -f2)
- escape=("[" "]" "~" "!" "{" "}" "(" ")" "$" "@" "<" ">" ":" ";" "," '`' "%" "+" "=" '"' "|" "?" '*' "&" " " "'")
- for e in "${escape[@]}"
- do
- file_name=${file_name//[$e]/\\$e}
- done
- file_name=${file_name//"\\'"/"'\"\'\"'"}
- out_name=$(echo $file_name"_"$start_time"-"$end_time"."$file_extension | sed s/:/_/g)
- # removes metadata
- cmd=$(echo ffmpeg -y -ss $start_time_sec -t $duration -i "$file_name"."$file_extension" -map_metadata -1 -c:a copy "$out_name")
- # no metadata removal
- # cmd=$(echo ffmpeg -y -ss $start_time_sec -t $duration -i "$file_name"."$file_extension" -c:a copy "$out_name")
- # echo $cmd
- bash -c "$cmd" &
- else
- start_time=$i
- fi
- done
- else
- echo "error, 2 or more input files detected"
- fi
- }
- cut_audio_mp3 01:00 01:15
- # cut_audio_mp3 01:00 01:15 30:01 30:22 60:05 70:10
- # dependecies:
- # sudo apt install ffmpeg
- # function usage:
- # function name followed by start time, end time, time markers respectively
- # put only 1 mp3 or 1 flac file in the same folder as script file (chmod +x cut_audio_mp3.bash)
Add Comment
Please, Sign In to add comment