2

I want to play youtube videos on a loop from my terminal using mplayer, and for that I've written the following code taking pieces from here and there:

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter the video ID: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

video=$(youtube-dl -g https://youtube.com/v/$searchterm)

while true; 
do 
    mplayer $video
    sleep 2; 
done

To play the video you need to feed the video ID to the script. For example if this is the video you're watching: https://www.youtube.com/watch?v=daQhI1JFXn4 Then the video ID is the part after v= which is daQhI1JFXn4

Everything works fine, except it still downloads the video every time that it goes trough the loop. I am wondering if there is any way that this could be changed so that it downloads the file only once and play it over and over.

3

I found avplay (you can replace with ffplay if you prefer) worked better for this, but I've left it as mplayer. tee-ing it to a file and piping to play the first time works. I've added a check to see if it's previously been downloaded.

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter the video ID: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

video=$(youtube-dl -g https://youtube.com/v/$searchterm)

[[ -f $searchterm.mp4 ]] || wget -O - $video 2>/dev/null | tee $searchterm.mp4 | mplayer -

while true; 
do
    sleep 2;
    mplayer $searchterm.mp4
done
  • Great. that wget is what I needed. But I avoided avplay or ffplay, since I can't find them on fedora repositories, and I still used mplayer. Is there any way that I can stop wget from showing all those contents that it's downloading? – Keivan Oct 5 '15 at 2:56
  • 1
    You can still use mplayer , but you might need the -cache option to get it to work right. wgethas the output on stderr (just realised it's not weird, it's so we can do exactly what we're doing), so adding 2> /dev/null to the command should do the trick for hiding the output. I'll put that in the answer. – seumasmac Oct 5 '15 at 3:02
  • perfect. thanks. mplayer works fine for me. But I keep that in mind if something goes wrong. Thanks. now only if I can search in the folder so that I don't download the same file if I have downloaded it before, I'll be set for life. – Keivan Oct 5 '15 at 3:11
  • 2
    You could name the file $searchterm.mp4, instead of current.mp4, and check for its existence before running wget. I'll update the script with to match what you've asked for. – seumasmac Oct 5 '15 at 3:13
  • 1
    Updated to add a check. The || syntax is an OR. It means either there's a file called $searchterm.mp4, OR run the wget command. – seumasmac Oct 5 '15 at 3:20
1

Store the output of YouTube-dl in a file by redirecting. Instead of saving the command itself in the video variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.