audio-split 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/sh
  2. # This is a quick and simple solution for downloading and splitting youtube mixes
  3. # Requirements:
  4. # awk
  5. # exiftool
  6. # ffmpeg
  7. # youtube-dl
  8. # Usage:
  9. # youtube-dl --add-metadata -icxf bestaudio/best URL
  10. # eval $(audio-spilt FILE OUTDIR)
  11. # This is it! Tracks should be now in OUTDIR
  12. # This code is under CC0 http://creativecommons.org/publicdomain/zero/1.0/
  13. # by Andrzej <dreadknight@firemail.cc> Bogdanowicz 2019
  14. outdir="$2"
  15. if [ -z "$outdir" ]; then outdir=$(echo "$1.dir" | tr " " "_");fi
  16. mkdir -p "$outdir"
  17. exiftool -s -s -s -b -Description "$1" | awk -v dir="$outdir" -v filename="$1" \
  18. 'BEGIN{fileformat=filename; gsub(".*\\.", "", fileformat); timestamps[1]=0}
  19. /[0-9]:[0-9][0-9]/{
  20. gsub("([A-Za-z\\(\\) ])+$","",$0)
  21. time=$NF;
  22. gsub("\"","",$0);
  23. split($0, album, " (-|–) ");
  24. gsub(" ([0-9]+:)?[0-9]+:[0-9]+","",album[2]);
  25. gsub(":", " ", time);
  26. gsub("^ ","",album[2]);
  27. tnum=length(timestamps);
  28. saveto=dir"/"tnum"_"tolower(album[1]"-"album[2]"."fileformat);
  29. $0=time;
  30. if (NF == 3) {timestamp = $1*3600+$2*60+$3;}
  31. else {timestamp = $1*60+$2};
  32. if (timestamp == 0) {games[1]=album[1]; titles[1]=album[2]; savetos[1]=saveto; next };
  33. titles[tnum+1] = album[2];
  34. games[tnum+1] = album[1];
  35. timestamps[tnum+1]=timestamp
  36. prevtime = timestamp-timestamps[tnum];
  37. saveto=dir"/"tnum+1"_"tolower(album[1]"-"album[2]"."fileformat);
  38. gsub(" ", "_", savetos[tnum]);
  39. savetos[tnum+1] = saveto;
  40. command="ffmpeg -i \"%s\" -acodec copy -ss %s -t %s -metadata:s:a:0 title=\"%s\" -metadata:s:a:0 artist=\"%s\" -metadata title=\"%s\" -metadata artist=\"%s\" -metadata track=\"%s\" \"%s\";\n";
  41. printf(command,filename,timestamps[tnum],prevtime,titles[tnum],games[tnum],titles[tnum],games[tnum],tnum,savetos[tnum]);
  42. }
  43. END{
  44. tnum++;
  45. gsub(" ", "_", savetos[tnum]);
  46. gsub("-t %s ","",command);
  47. printf(command,filename,timestamps[tnum],titles[tnum],games[tnum],titles[tnum],games[tnum],tnum,savetos[tnum]);
  48. }'