scripts

misc scripts and tools
git clone git://git.2f30.org/scripts
Log | Files | Refs

ytfetch (1288B)


      1 #!/bin/sh
      2 
      3 # fetch music videos and convert them to audio files
      4 # it also handles playlists
      5 # depends: youtube-dl ffmpeg
      6 
      7 if test -z "$1"; then
      8     echo "usage: $(basename $0) yturl ..." 1>&2
      9     exit 1
     10 fi
     11 
     12 # newline separated
     13 IFS='
     14 '
     15 
     16 for URL in "$@"; do
     17     echo "see $URL"
     18     # each --get comes on a line by its own in a predefined order
     19     # command line order does not matter
     20     LIST=$(youtube-dl --restrict-filenames --ignore-errors \
     21         --get-url --get-filename --get-title \
     22         -o "%(title)s.%(ext)s" "$URL")
     23     I=0 # keep state
     24     for L in $LIST; do
     25 	case $I in
     26         0)
     27             ARTIST=${L%% - *}
     28             TITLE=${L#* - }
     29         ;;
     30         1)
     31             DIRECT="$L"
     32         ;;
     33         2)
     34             F="${L%.*}.mp3"
     35             if test -f "$F"; then # already have this?
     36                 echo "hav $F"
     37             else
     38                 echo "get $L"
     39                 # encode to mp3 audio with tags and no video
     40                 ffmpeg -i "$DIRECT" -vn -acodec libmp3lame -ab 128k \
     41                     -metadata artist="$ARTIST" -metadata title="$TITLE" \
     42                     "$F" 2> /dev/null
     43             fi
     44         ;;
     45         esac
     46         # incr and mod with the number of --get used above
     47         I=$(expr $I + 1)
     48         I=$(expr $I % 3)
     49     done
     50 done