CHALLENGE Muz/gyts script fix/alternatives Yad script

VastOne

Muz is my favorite alias and script that we have... It basically calls gyts and converts what you search for from youtube into an mp3

For the last several months the gyts script has been broken due to this line changing somehow at youtube

lynx -dump -listonly http://www.youtube.com/results?search_query=

This is the entire gyts script

gyts


#!/bin/bash
function isFileEmpty()
{
    if [ ! -s $1 ]
    then
            return 0
    else
            return 1
    fi
}

function fileExists()
{
    if [ -f $1 ]
    then
        return 1
    else
        return 0
    fi
}

function makeFileName()
{
    archive=$(echo "${artist} ${song}" | sed -e 's/ /_/g')
}

function downloadBest()
{
    youtube-dl ${1} -o ${2}.ytb
    fileExists ${2}.ytb
    return $?
}

function downloadWorst()
{
    youtube-dl -f worst ${1} -o ${2}.ytb
    fileExists ${2}.ytb
    return $?
}

function getTitle()
{
    youtube-dl --get-title $1
}

function extractAudio() {
    ffmpeg -i ${1}.ytb -ac 2 ${1}.wav
    rm ${1}.ytb
    fileExists ${1}.wav
    return $?
}

function toMp3() {
    lame -b ${1} ${2}.wav ${2}.mp3
    rm ${2}.wav
    fileExists ${2}.mp3
    return $?
}


function getList()
{
    lynx -dump -listonly http://www.youtube.com/results?search_query="${1}"+"${2}">>${3}.rl
    grep http://www.youtube.com/watch?v= ${3}.rl >> ${3}.rl1
    rm ${3}.rl
    cut -b 1-6 --complement ${3}.rl1 >> ${3}.rl2
    rm ${3}.rl1
    uniq ${3}.rl2 >> ${3}.rl3
    rm ${3}.rl2
    grep -v \& ${3}.rl3 >> ${3}.l
    rm ${3}.rl3
    isFileEmpty ${3}.l
    return $?
}

function checkProgram()
{
    if ! type $1 &>/dev/null;
    then
        echo "I require $1 to work.."
        echo "Do I try to install it? (y to install)"
        read -s -n1 char
        if [ "$char" == "y" ]
        then
            echo "trying to install (with apt)"
            sudo apt-get install $1
        else
            echo "see ya!"
            exit
        fi
    fi
}

function printHelp()
{
    echo -e "NAME"
    echo -e "\tgyts"
    echo -e "\tArtist, song and quality->Youtube->Audio->mp3, easy as that"
    echo -e "USAGE"
    echo -e "\tgyts"
    echo -e "\t\tInteractive Mode"
    echo -e "\tgyts \"artist\" \"song\" Q L|F"
    echo -e "\t\tSearch youtube for the artist and song (must be between \" \")"
    echo -e "\t\tQ can be 1, 2 or 3, 3 being the best quality"
    echo -e "\t\tL or l shows a list to choose from"
    echo -e "\t\tF or f shows no list, and download the first result"
    echo -e "\tgyts help"
    echo -e "\t\tPrint a usage message, this one"
    echo -e "\t\tcontact: superchompu@gmail.com"
    echo -e "\t\tLong Live CrunchBang Linux!!"
    echo -e "\t\tVisit http://crunchbanglinux.org/"
    exit
}

function interactiveMode()
{
    echo "Interactive Mode"
    read -p "Artist: " artist
    read -p "Song: " song
    echo "Quality: "
    echo "1 = worst but quick"
    echo "2 = medium quality"
    echo "3 = best quality"
    read quality
    while ! [[ "$quality" =~ ^[1|2|3]$ ]]
    do
        echo "Not a number or out of range (1-3)"
        read quality
    done
    echo "Choose from List or First find: "
    echo "L or l = List"
    echo "F or f = First Find" 
    read chooselist
    while ! [[ "$chooselist" =~ ^[L|l|F|f]$ ]]
    do
        echo "Only L for list or F for first find"
        read chooselist
    done
   
}

function singleSongLine()
{
    artist=$1
    song=$2
    quality=$3
    if ! [[ "$quality" =~ ^[1|2|3]$ ]]
    then
        echo "Bad argument Quality = $quality "
        printHelp
    fi
    chooselist=$4
    if ! [[ "$chooselist" =~ ^[L|l|F|f]$ ]]
    then
        echo "Bad argument List or First find= $chooselist"
        printHelp
    fi   
}

function downloadAndConvert()
{
    if [[ "$quality" -gt 1  ]]
    then
        downloadBest $1 $archive
    else
        downloadWorst $1 $archive
    fi
    if [[ $? -eq 0  ]]
    then
        echo "problem with link"
        return 0
    else
        extractAudio $archive
        if [[ $? -eq 0 ]]
        then
            echo "problem when extracting audio"
            return 0
        else
            if [[ "$quality" -eq 3 ]]
            then
                toMp3 320 $archive
            else
                toMp3 192 $archive
            fi
            if [[ $? -eq 0 ]]
            then
                echo "problem converting to mp3"
                return 0
            else
                return 1
            fi
        fi
    fi   
}

function chooseFromList()
{
        read -p "Choose: " index
        while ! [[ "$index" =~ ^[0-9]+$ ]]
        do
            echo "not a number"
            chooseFromList $1
        done
        while ! [[ "$index" -lt $1 ]]
        do
            echo "out of range"
            chooseFromList $1
        done
}
clear
checkProgram "lynx"
checkProgram "youtube-dl"
checkProgram "ffmpeg"
checkProgram "lame"

if [[ $# -gt 4 ]]
then
    echo "Too many arguments."
    printHelp
    exit
fi

if [ $# -eq 1 ] || [ $# -eq 3 ]
then
    printHelp
fi

if [[ $# -eq 2 ]]
then
    printHelp
else
    if [[ $# -eq 0 ]]
    then
        interactiveMode
    else
        if [[ $# -eq 4 ]]
        then
            singleSongLine "$1" "$2" "$3" "$4"
        fi
    fi
    makeFileName "$artist" "$song"
    getList "$artist" "$song" "$archive"
    if [[ $? -eq 0 ]]
    then
        echo "Error with connection"
        exit
    fi
    if  [ "$chooselist" == "L"  ] || [ "$chooselist" == "l" ]
    then
        iterator=0
        for link in $(cat ${archive}.l)
        do
            echo -n "[${iterator}]: "
            getTitle $link
            let iterator++
        done
        a=( $( cat ${archive}.l ) )
        alenght=$(( ${#a[@]} ))
        chooseFromList alenght
        downloadAndConvert ${a[$index]}
        if [[ $? -eq 0 ]]
        then
            echo "something went horribly wrong :( ..."
        else
            rm ${archive}.l
            exit
        fi
    else
        for link in $(cat ${archive}.l)
        do
            downloadAndConvert $link
            if [[ $? -eq 0 ]]
            then
                echo "trying to download another..."
                continue
            else
                rm ${archive}.l
                exit
            fi
        done
        rm ${archive}.l
        echo "ran out of links"
    fi
fi
exit


This is another script I have found that works perfectly using wget and whatever your search query is ...

youtube-dl-mp3

#!/bin/bash

url=$1
path=$2

TMP_FILE="youtube-mp3-$RANDOM.tmp"

youtube-dl --ignore-errors --get-title --get-url --get-filename "$url" > $TMP_FILE 2> "/tmp/$TMP_FILE.err"

exec 42< $TMP_FILE

while read video_title <&42 ; do
  read video_url <&42
  read video_filename <&42
  wget "$video_url" -O "$video_filename"
  ffmpeg -i "$video_filename" "$video_filename.wav"
  if [ -z "$path" ]; then
    lame -b 320 "$video_filename.wav" "$video_title.mp3"
  else
    lame -b 320 "$video_filename.wav" "$path/$video_title.mp3"
  fi
  rm -f "$video_filename" "$video_filename.wav"
done

exec 42<&-
rm -f $TMP_FILE


It works like this

youtube-dl-mp3 "the youtube url of the video you want to convert"

We have one of three options (as I see it anyway, please add more if you see fit)

1 - Fix the query string in the original gyts so that it is recognized again

2 - Change the gyts script to use wget instead of lynx ... we could even use the wget code from the second script ... need bash understanding help here (and wget) ... this is my favorite possible solution

3 - Just make a new Yad front end for the second script.. I like this idea but it eliminates a search function...  I would like to see this done anyway so if you Yad experts are willing ...

Call to arms for you that love this.. this is where I want to be as transparent as possible.. I could muddle along a figure this out eventually, but I do not want to!

VSIDO      VSIDO Change Blog    

    I dev VSIDO

Snap

Very interesting script. It can be used without the mp3 conversion part too. And it should be easily tweaked to download into other (better) audio formats than mp3 (or video formats) with just ffmpeg. So cool. Thanks for sharing, VastOne.

Wish I could help with the other part. I guess lynx is not needed at all. In the end the script is using youtube-dl,  ffmpeg and lame too. Only the browsing part needs to be modified to use anything else instead of lynx, IMO. My bash scripting abilities are almost null, but I'll give it a go anyway.

VastOne

^ We really do not need anything other than the new one.. I think I will just change that to muz on the next ISO build.. I would like to see it with a yad front end..

I have discovered this about the youtube-dl-mp3 script...

Most everything on YouTube now creates a playlist ... I played a single album of Liquid Mind and in turn it created a Liquid Mind playlist and when I clicked on it got this url that had 10 albums on it

https://www.youtube.com/watch?v=gXYu7k-O4iw&list=PLmmMygRTV3Nfbo7Q3BafTRUqPnrhsLQap

By running that url with youtube-dl-mp3 in " "  it downloaded all 10 albums and converted each to mp3...

Not a bad script at all
VSIDO      VSIDO Change Blog    

    I dev VSIDO