Advertisement
Guest User

audio bookmark manager

a guest
Mar 12th, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.02 KB | Software | 0 0
  1. hardware/software used:
  2.  
  3. Satellite Pro ET10-G-106
  4. A4tech TK-5 Mini Numeric Keypad
  5. Lubuntu 18.04.6 LTS
  6. GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
  7.  
  8. usage, every laptop / 2 in 1 laptop / or just a tablet
  9.  
  10. 1. setup Audacious (4.3)
  11.  
  12. sudo add-apt-repository ppa:ubuntuhandbook1/apps
  13. sudo apt update
  14. sudo apt -y install audacious
  15.  
  16. File/Settings/Appearance/Restart in GTK (legacy) mode
  17.  
  18. 2. setup xbindkeys
  19.  
  20. sudo apt -y install xbindkeys
  21.  
  22. 3. setup xev
  23.  
  24. sudo apt -y install xautomation
  25.  
  26. 4. (optional) check keycodes in terminal via xev (A4Tech TK-5):
  27.  
  28. keycode 87 - KP_End ( rewind )
  29. keycode 88 - KP_Down ( fast forward )
  30. keycode 89 - KP_Next ( play/pause )
  31. keycode 104 - KP_Enter ( bookmark audio )
  32. keycode 83 - KP_Left ( split audio )
  33. keycode 84 - KP_Begin ( join & play audio )
  34.  
  35. 5. setup speech-dispatcher
  36.  
  37. sudo apt -y install speech-dispatcher
  38.  
  39. 6. setup scripts:
  40.  
  41. rewind, terminal:
  42.  
  43. leafpad ~/Music/rewind.bash
  44.  
  45. paste code:
  46.  
  47. #!/bin/bash
  48.  
  49. audtool playback-seek-relative -10
  50. spd-say -t female2 p
  51.  
  52. save & close text editor, make it executable:
  53.  
  54. chmod +x ~/Music/rewind.bash
  55. _________________________________
  56.  
  57. fast forward, terminal:
  58.  
  59. leafpad ~/Music/fforward.bash
  60.  
  61. paste code:
  62.  
  63. #!/bin/bash
  64.  
  65. audtool playback-seek-relative 10
  66. spd-say -t female2 n
  67.  
  68. save & close text editor, make it executable:
  69.  
  70. chmod +x ~/Music/fforward.bash
  71.  
  72. _________________________________
  73.  
  74. play/pause, terminal:
  75.  
  76. leafpad ~/Music/play_pause.bash
  77.  
  78. paste code:
  79.  
  80. #!/bin/bash
  81.  
  82. function play_pause() {
  83.     status=$(audtool playback-status)
  84.     if grep 'paused\|stopped' <<< $status ; then
  85.         audtool playback-play
  86.     else
  87.         audtool playback-pause
  88.     fi
  89. }
  90.  
  91. play_pause
  92. spd-say -t female2 s
  93.  
  94. save & close text editor, make it executable:
  95.  
  96. chmod +x ~/Music/play_pause.bash
  97. _________________________________
  98.  
  99. bookmark audio, terminal:
  100.  
  101. leafpad ~/Music/mark_in_out.bash
  102.  
  103. paste code:
  104.  
  105. #!/bin/bash
  106.  
  107. function mark_audio() {
  108.     out_loc=$(echo ~/Music/marks.txt)
  109.     status=$(audtool playback-status)
  110.     current_song_pos=$(audtool --current-song-output-length-seconds)
  111.     minutes=$((current_song_pos / 60))
  112.     if [[ "$minutes" -lt 10 ]]; then
  113.         minutes=$(printf "%02d" $minutes)
  114.     fi
  115.     seconds=$((current_song_pos % 60))
  116.     if [[ "$seconds" -lt 10 ]]; then
  117.         seconds=$(printf "%02d" $seconds)
  118.     fi
  119.     write_marker=$(echo $minutes":"$seconds)
  120.     if [[ -f "$out_loc" ]]; then
  121.         echo -n " $write_marker" >> $out_loc
  122.     else
  123.         echo -n "$write_marker" >> $out_loc
  124.     fi
  125. }
  126.  
  127. mark_audio
  128. spd-say -t female2 m
  129.  
  130. save & close text editor, make it executable:
  131.  
  132. chmod +x ~/Music/mark_in_out.bash
  133. _________________________________
  134.  
  135. 7. make audio clips:
  136.  
  137. install dependencies:
  138.  
  139. sudo apt -y install ffmpeg
  140.  
  141. split audio & backup marks.txt before its deletion, terminal:
  142.  
  143. leafpad ~/Music/split_audio.bash
  144.  
  145. paste code:
  146.  
  147. #!/bin/bash
  148.  
  149. function check_out_loc() {
  150.   w_dir="$(pwd)/clips"
  151.   echo $w_dir
  152.   if [ -d "$w_dir" ]; then
  153.     # re-creatates subdir clips in the current folder
  154.     rm -rf $w_dir
  155.     mkdir "$(pwd)/clips"
  156.     echo direcotry re-created
  157.   else
  158.     mkdir "$(pwd)/clips"
  159.     echo direcotry created
  160.   fi
  161. }
  162.  
  163. function cut_audio() {
  164.   readarray -t mp3_files < <(find ./ -maxdepth 1 -type f -regex '.*\.\(mp3\|flac\)$' | sed "s/.*\///")
  165.   if [ ${#mp3_files[@]} -eq 1 ]; then
  166.     j=0
  167.     for i in "$@"; do
  168.       ((j++))
  169.       if [ $((j%2)) -eq 0 ]; then
  170.         end_time=$i
  171.         MPHR=60
  172.         IFS=':' read -ra start_array <<< "$start_time"
  173.         IFS=':' read -ra end_array <<< "$end_time"
  174.         len_start_min=${#start_array[0]}
  175.         len_start_sec=$(( $len_start_min + 1 ))
  176.         len_end_min=${#end_array[0]}
  177.         len_end_sec=$(( $len_end_min + 1 ))
  178.         start_time_sec=$(( ( 10#${start_time:0:$len_start_min} ) * MPHR + 10#${start_time:$len_start_sec} ))
  179.         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} ))
  180.         file_name=$(basename "${mp3_files[0]}" | cut -d. -f1)
  181.         file_extension=$(basename "${mp3_files[0]}" | cut -d. -f2)
  182.         escape=("[" "]" "~" "!" "{" "}" "(" ")" "$" "@" "<" ">" ":" ";" "," '`' "%" "+" "=" '"' "|" "?" '*' "&" " " "'")
  183.         for e in "${escape[@]}"
  184.         do
  185.           file_name=${file_name//[$e]/\\$e}
  186.         done
  187.         file_name=${file_name//"\\'"/"'\"\'\"'"}
  188.         w_dir="$(pwd)/clips/"
  189.         out_name=$w_dir$(echo $file_name"_"$start_time"-"$end_time"."$file_extension | sed s/:/_/g)
  190.         # removes metadata
  191.         cmd=$(echo ffmpeg -y -ss $start_time_sec -t $duration -i "$file_name"."$file_extension" -map_metadata -1 -c:a copy "$out_name")
  192.         # no metadata removal
  193.         # cmd=$(echo ffmpeg -y -ss $start_time_sec -t $duration -i "$file_name"."$file_extension" -c:a copy "$out_name")
  194.         # echo $cmd
  195.         bash -c "$cmd"
  196.       else
  197.         start_time=$i
  198.       fi
  199.     done
  200.     # backup marks.txt file
  201.     if [ -f "$(pwd)/marks.txt" ]; then
  202.       marks_out_loc=$(echo $(pwd)/marks_$(date +%s).txt)
  203.       cp "$(pwd)/marks.txt" "$marks_out_loc"
  204.       echo -n " $file_name"."$file_extension" >> $marks_out_loc
  205.       echo marks.txt backup done
  206.       rm marks.txt
  207.     fi
  208.   else
  209.     echo "error, only 1 mp3/flac file allowed in the current working directory"
  210.   fi
  211. }
  212.  
  213. check_out_loc
  214. cut_audio $(cat ~/Music/marks.txt)
  215. spd-say -t female2 "split audio"
  216.  
  217. save & close text editor, make it executable:
  218.  
  219. chmod +x ~/Music/split_audio.bash
  220.  
  221. _________________________________
  222.  
  223. 8. join & listen:
  224.  
  225. join clips & play, terminal:
  226.  
  227. leafpad ~/Music/join_play.bash
  228.  
  229. paste code:
  230.  
  231. #!/bin/bash
  232.  
  233. function check_out_loc() {
  234.   w_dir="$(pwd)/joins"
  235.   echo $w_dir
  236.   if [ -d "$w_dir" ]; then
  237.     echo direcotry exists
  238.   else
  239.     mkdir "$(pwd)/joins"
  240.     echo direcotry created
  241.   fi
  242. }
  243.  
  244. function join_clips() {
  245.     readarray -t files < <(find ./clips -maxdepth 1 -type f -regex '.*\.\(mp3\|flac\)$' | sed "s/.*\///")
  246.     file_extension=$(basename "${files[0]}" | cut -d. -f2)
  247.     out_loc=$(echo joins/$(date +%s).$file_extension)
  248.     cat ./clips/*.* > ./"$out_loc"
  249.     audacious "$(echo $(pwd)/$out_loc)"
  250. }
  251.  
  252. check_out_loc
  253. join_clips
  254. spd-say -t female2 "join & play"
  255.  
  256. save & close text editor, make it executable:
  257.  
  258. chmod +x ~/Music/join_play.bash
  259. _________________________________
  260.  
  261. 9. setup keyboard keybindings:
  262.  
  263. create & edit config file:
  264.  
  265. leafpad ~/.xbindkeysrc
  266.  
  267. paste code:
  268.  
  269. "/home/$(whoami)/Music/rewind.bash"
  270. c:87
  271.  
  272. "/home/$(whoami)/Music/fforward.bash"
  273. c:88
  274.  
  275. "/home/$(whoami)/Music/play_pause.bash"
  276. c:89
  277.  
  278. "/home/$(whoami)/Music/mark_in_out.bash"
  279. c:104
  280.  
  281. "/home/$(whoami)/Music/split_audio.bash"
  282. c:83
  283.  
  284. "/home/$(whoami)/Music/join_play.bash"
  285. c:84
  286.  
  287. save & close text editor, exectue following commands in terminal:
  288.  
  289. xbindkeys
  290. killall xbindkeys && xbindkeys
  291.  
  292. _________________________________
  293.  
  294. 10. power manager settings:
  295.  
  296. xfce power manager, terminal:
  297.  
  298. xfce4-power-manager-settings
  299.  
  300. Laptop lid - On battery / Plugged in - Switch off display
  301. _________________________________
  302.  
  303. further optimization is up to the user imagination
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement