How to create browser integration of youtube-dl?

52 risposte [Ultimo contenuto]
pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

In order to use youtube-dl to download a video, play it, and then remove it, I created a very simple script called 'temptube' to run in a terminal:

cd ~/tmptube
youtube-dl -f 43 $1
totem *.webm
rm ~/tmptube/*.webm

Right now, I use ixquick to search for a video's URL and cut-and-paste it into a terminal after the script command. So: temptube http://www.somewebsite.com

How can I integrate this into a browser so that I can just toggle a switch and then click a link?

If anybody can point me in the right direction, I'd really appreciate it.

Thanks!

By the way, the script needs improvement. For instance, if I knew how to access the file name of the downloaded file (obvious to some I'm sure), this could work without the hack of downloading into a known empty directory in order for rm *.webm to work.

lloydsmart

I am a member!

Offline
Iscritto: 12/22/2012

I like your thinking, but I do have to wonder what you're trying to achieve here.

Youtube-dl is great for downloading YouTube videos from the command line, but if you're just browsing and want to watch a video without non-free software (including javascript), why not just use HTML5-Video-Everywhere? [1]

This addon will replace the default YouTube video player (either Flash or Javascript/HTML5) with the built-in native Firefox video player, which is 100% free software.

As a nice side-effect, it also makes it really easy to save videos if that's what you want. Just right-click and select save.

The addon is 100% free software, and I believe it is included in the Trisquel repositories.

[1] https://addons.mozilla.org/en-US/firefox/addon/html5-video-everywhere/

Magic Banana

I am a member!

I am a translator!

Offline
Iscritto: 07/24/2010
hack and hack
Offline
Iscritto: 04/02/2015

I'd says the main thing with youtube-dl is that it works without Javascript at all, which is nice when you often use NoScript.

lembas
Offline
Iscritto: 05/13/2010

This might come handy for browser to bash bridge. https://trisquel.info/en/browser/addons/launchy (nevermind that description, it does more)

Looks like the youtube command has a --get-name option. :)

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

Lol sorry about that. Those were both obvious tries before creating a post.

I use the repositories all the time, but for some reason I never looked for that. Probably because I just accepted using my youtube-dl hack. The addon looks great! I'll have to play with it some more, but this is definitely the kind of thing I wanted.

--get-name is indeed an option. When I wrote the script I was in a hurry, and the above script worked for me. Still, there is a right way and a wrong way to program which is why I suggested the improvement.

Thank you both!

hack and hack
Offline
Iscritto: 04/02/2015

I asked this myself a while ago, then forgot about it...
Here are some possibilities (I hope so) :

https://www.reddit.com/r/firefox/comments/3iob8v/openwith_addon_and_youtubedl_anyone_know_what_the/?ref=search_posts

http://bernaerts.dyndns.org/linux/74-ubuntu/307-ubuntu-firefox-gui-download-youtube-tvreplay-video-stream

EDIT:
http://gemal.dk/mozilla/launchy.html

For the name recognition in the script, maybe it's possible with regular expressions (I barely know what it is, so I won't be able to help much. Plus I'm not sure I understand what you're trying to accomplish).

hack and hack
Offline
Iscritto: 04/02/2015

Ok, so I put your script in a youtube-dl.sh file, chmoded it to 700 (and checked), though I replaced totem with vlc.

Then I created the lauchy.xml according to the link I've posted above. For "type", I've put 1 for browser, though I don't understand it fully yet.

I restarted my browser, and the link appears properly.
I went on a youtube page, then I tried it.
But nothing happens. And tmptube I created is empty, so I'm sure of it.

But I don't know whether the script or my launchy config is faulty. I should try to create an "open with gedit" entry and try.

EDIT :
I can't even make it work in the CLI, so there's that.
I named it tmptube.sh, chmoded it. I only replace totem by vlc.

Why doesn't it work?

lembas
Offline
Iscritto: 05/13/2010

How exactly are you trying to run it?

hack and hack
Offline
Iscritto: 04/02/2015

You nailed it, thanks for the hint lembas.

In the CLI, I do this:
tmptube https://www.youtube.com/watchwhatever

Now I tried using ./ before (and .sh at the end, though I've tried this by itself with no success).

According to this, I'd need to make launchy understand it better. For now, my path to EXE is /home/user/tmptube.sh

When I try this ($ /path/to/shell/script/tmptube.sh) in the CLI, it fails.
According to this (http://www.cyberciti.biz/faq/howto-run-a-script-in-linux/), I need to create a bin folder and put that script in. Maybe then it will work.

Assuming this already works, how lanchy is supposed to take the command and associate it with the url on the page?

lembas
Offline
Iscritto: 05/13/2010

No need to put it in a specific folder. If you're in the same folder as the script, you can run it with ./scriptname.ext

This should also work /path/to/shell/script/tmptube.sh

Do you get an error message?

hack and hack
Offline
Iscritto: 04/02/2015

Sorry, I didn't express myself properly.
I meant that executing with ./ worked, but not with the full path, for some reason.
It gives me the following message:
bash:home/user/tmptube.sh: no such file or folder (rough translation).

lembas
Offline
Iscritto: 05/13/2010

Are you forgetting the first slash?

/home/user/tmptube.sh

hack and hack
Offline
Iscritto: 04/02/2015

Bingo (https://www.youtube.com/watch?v=UMRo5XCKddQ), this worked! thanks lembas.

Now, in my .xml file for Launchy,
between the command brackets, I have:
/home/user/tmptube.sh

Actually it works just the way I wanted: I forgot to add the bin bash thing at the start of the script.

What's weird is that I don't even have the CLI opening anymore (showing the download progression), how come?

This is GREAT! Thanks a lot!

lembas
Offline
Iscritto: 05/13/2010

You're welcome! Don't you just love it when you really control your computer?

hack and hack
Offline
Iscritto: 04/02/2015

Yes, this is getting better and better!

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

Please try the following methodical method to clarify each step.

1.) Create a bin directory in your home directory with mkdir ~/bin
2.) Switch into your ~/bin directory and type: echo $PATH is /home/yourname/bin in the PATH?

3.) Download the video: youtube -dl -f 43 https://www.youtube.com/watchwhatever
Did it get there? Is it a .webm file?

4.) Play the .webm file with: vlc someFile.webm
Did vlc play the file?

5.) Create a script without #!/bin/bash and just name it temptube (not temptube.sh). Just put youtube-dl -f -43 $1 in the file. No other lines. Make the script executable with: chmod 755 temptube

6.) Remove the .webm file

7.) Now type: temptube https://www.youtube.com/watchwhatever [if /home/yourname/bin is in the path, no ./ is necessary]
Did it download the file?

8.) Remove the .webm file

9.) Now take the same script but add a second line: vlc *.webm

10.) Now type: temptube https://www.youtube.com/watchwhatever
Did it download and play the file with vlc?

If you get to step 10 successfully:
a.) create the directory tmptube: mkdir ~/tmptube
b.) add the line: cd ~/tmptube to the top of the script
c.) type temptube https://www.youtube.com/watchwhatever
d.) check the ~/tmptube directory and see if it downloaded the .webm file

Finally, if all of that works, you can add the last line to the script: rm ~/tmptube/*.webm
Run temptube https://www.youtube.com/watchwhatever again, then check the ~/tmptube directory to make sure it is empty.

Did it do all of that? If not, where was there a problem?

hack and hack
Offline
Iscritto: 04/02/2015

Thanks a lot pragmatist, I'll try each and every step carefully tomorrow.

I can tell you for now that when I execute the tmptube.sh with ./ in front of it (and being located in my home folder),
the video gets downloaded, and starts in VLC when finished.
The video is inside the tmptube folder (which I created in my home folder). But it's erased (not even in my trash can) when I close off VLC.

I'll read again tomorrow to see if it's the intended behavior.

On the other hand, when I try to do home/user/tmptube.sh with the URL after it, I get an error message:
bash:home/user/tmptube.sh: no such file or folder (rough translation).

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

If the difference was that in one case you typed:
./tmptube.sh and in another you typed: tmptube.sh

The response makes sense because your home directory is not in your PATH variable by default. Notice what I wrote in step 7.

Check your PATH variable with: echo $PATH
Before changing your PATH variable, back it up first with:
echo $PATH >> old_path

Then you simply append the new directory:
PATH=$PATH:/home/yourName/bin

now type: echo $PATH

It should be the original path with your home bin directory added at the end. If things get screwed up just restore the old path by cutting and pasting the contents of the old_path file after PATH=
or type PATH=`cat old_path` (just make sure you use the right quote symbol, it is the one next to the '1' key on most keyboards, so: ` NOT '

Or, just precede the command with ./ in the script

If you used the script I put in my OP then of course the video is deleted after you exit vlc. The commands are executed sequentially. When one ends, the next starts.

I don't know anything about the trash can.

hack and hack
Offline
Iscritto: 04/02/2015

The trash can thing is not much, it's just that not everything I delete ends up in it. And it's not necessarily big files. Anyway, not really important.

I just tried the /path/to/script/sh (with a / in first position this time), and it worked. What's weird is that my home folder isn't even listed in my path.

Anyway, it works. While while trying to make it work with launchy, I want to improve a thing or two, like closing the command line after download completion. and maybe hiding it or assigning it to another virtual desktop.I'll look into youtube-dl's options first.

EDIT:
Actually it works just the way I wanted: I forgot to add the bin bash thing at the start of the script.

What's weird is that I don't even have the CLI opening anymore (showing the download progression), do you know why?

Many thanks, it's been a while I wanted this to work!

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

It's totally normal that your home directory is NOT in your path. Your home directory is not where you are supposed to put binary files (or other executables). That is why you create a bin directory. When you type in a command to execute a file, all of the directories in your path are checked until the executable file is found. If you store lots of things in your home folder--like most people do--such as Videos and Music and Documents, those will be searched unnecessarily. Make sense?

As far as your terminal not opening, I'm not sure I understand exactly what you did. What is the script exactly? Where are you running the script? If you are using a launcher, I believe the program can be running in a shell but a terminal window does not open. I'm not in GNOME right now (I'm loving using i3WM), but if I'm remembering right, when you make a launcher there is a checkbox asking if you want the process to run in a shell.

hack and hack
Offline
Iscritto: 04/02/2015

It does make sense, but how come the script works even though it's in my home folder?

Here's the script (running from my home folder), without any launcher (I'm on i3wm as well, which is fantastic):

#!/bin/bash
cd ~/tmptube
youtube-dl -f 43 $1
vlc *.webm
rm ~/tmptube/*.webm

it's called tmptube.sh

And in Launchy's .xml file, I've put between the command brackets:
/home/user/tmptube.sh

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

If you precede the command with ./ then it will run if it is in the path or not.

If you are using i3wm check out a program called: i3blocks

From a couple of videos I just watched about launchy, there looks like there are settings under a 'general' tab that let you configure how/when the windows close/open

It is great to learn by experimenting, but it is also good to learn a small base of information to help you learn more:
http://www.freesoftwaremagazine.com/articles/command_line_intro
http://en.flossmanuals.net/command-line/index/

I'm not suggesting you are a beginner, just that you, like all of us, have some holes in your knowledge.

It is also really cool that you are hacking around with this script!

hack and hack
Offline
Iscritto: 04/02/2015

I'm a slightly advanced beginner, at best. My knowledge has definitely more holes than cheese, so thanks for the links :)

I can't configure a thing in the 'general' tab, though it displays the .xml file content.
I might try to contact the addon plugin creator, or *gasp* read the sources. Or at least try.

i3blocks looks nice, displaying scripts and clic interaction on the status bar can be fun indeed.
Right now, though I'm pleased with xfce4-appfinder, I want to try a drop down menu like this: https://github.com/blastrock/rlmenu
There's also menutray, but I don't use the tray at all.
Also I might replace i3lock with xscreensaver (the former has security issues), even if I LOVE the i3lock appearance.
Other than that, my i3wm is pretty standard (besides cosmetic improvements).

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

I use xscreensaver, here are my .i3/config entries:

exec --no-startup-id xscreensaver -nosplash &
exec --no-startup-id xautolock -time 5 -locker xscreensaver-command -lock &

bindsym $mod+Pause exec --no-startup-id xscreensaver-command -lock

The first two start the daemons in the background. The last is a keybinding to start xscreensaver on command. You may have to install xautolock, but I believe it is in the repositories so that is easy to do.

I really like this video series: https://www.youtube.com/watch?v=j1I63wGcvU4&list=PL5ze0DjYv5DbCv9vNEzFmP6sU7ZmkGzcf
The final video, part 3 'rice your desktop', talks about i3blocks. Bear in mind that not all suggestions in this series involve free software. For example, one of the fonts he recommends does not seem to be free. So make sure to double check.

hack and hack
Offline
Iscritto: 04/02/2015

Thanks again pragmatist,
the videos filled several gaps here and there, very interesting.

About the screenlocker, I've also read about physlock (though I think I've read about xscreensaver being super secure).
Source: https://bbs.archlinux.org/viewtopic.php?id=191339

Looks like this one (probably xscreensaver as well) goes deeper in the system than i3lock.

Of course, I can also keep i3lock and just turn the machine off when in an unsafe environment for too long. It's fast enough.

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

By the way, the plugin is great so my script is almost always unnecessary.

However, there are a few cases where something like it can be useful. For example, when I am learning from a screencast and following along in a program, or taking notes in a program, running totem can be better.

The reason is that with totem I can put it on half the screen and make the viewable area of the video use 100% of that half screen. In the other portion of the screen I can put an editor or whatever program I'm following along with.

Doing that with a video player embedded in the browser is not possible. To clear away everything but the player window requires going fullscreen which takes up the entire screen.

JadedCtrl
Offline
Iscritto: 08/11/2014

I'm not a huge fan of the HTML5 player (YT's version or the one from HTML Everywhere! extension), so I use mpsyt.
mps-youtube is a fantastic cli YouTube client. You can get it via pip3.
When mpsyt isn't available, your script will do nicely. :)

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

It looks powerful, I'll have to check it out.

Thank you! As far as I'm concerned, the more alternatives I have the better. If anybody has any other suggestions I'm also interested. I'm certainly not attached to the trivial script I wrote in < 5 min :-)

hack and hack
Offline
Iscritto: 04/02/2015

After a few attempts, Launchy isn't as good as unplug in detecting URLs sometimes. I have no idea how Launchy detects what to open, so I'm not even sure I can adapt the script to detect the page's URL.

It is still a huge improvement, but I'd like to be able to detect the page's URL every time. Maybe with curl or wget.

hack and hack
Offline
Iscritto: 04/02/2015

Looks like I was not alone (a comment from 2012):
"A further improvement would be if one could also submit the url of the current page to launchy as well. But I am already very satisfied with it as it is :)"
Source: https://addons.mozilla.org/en-US/firefox/addon/launchy/reviews/?page=2

Too bad :(

Reading the sources, I see some Js.
I just found that this can get the webpage's URL when associated to a variable: window.location.href

But then where to put it? What to do with it?
I think I'll just try to contact the author. I'd need to understand the whole thing before attempting anything, and that's beyond my super powers for now.
But with that feature, Launchy would be perfect. And youtube-dl would be fully accessible with a right clic.

EDIT:
I contacted him.
Also, here's the main file:
http://www.mozdev.org/source/browse/launchy/source/content/main.js?annotate=1.25

I suspect it's at line number 123.
I'll try to identify the related functions, but even then, it would make little sense to me.

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

If I were pursuing this, I would look into how to make a plugin or an extension (or at least find out the difference!). I wouldn't focus on launchy.

For myself, right now I'm happy with all the options I have. In most cases "HTML5 Video Everywhere!" is excellent. If I'm watching a screencast, I use my hack. I made an alias to temptube called 'tt' so I just highlight the URL, type tt in an open terminal, hit the middle-mouse-button to paste, then hit Enter.

I still have to experiment with mps-youtube but it looks really cool--I love cli applications.

hack and hack
Offline
Iscritto: 04/02/2015

I didn't think about this, thanks.
Though since I barely know Javascript basics, I have better chances with adding to or tweaking Launchy's sources than with working on a whole new idea (much harder to implement for me).

On the other hand, even in doing this, I would need to know the basics of extensions creation. So in the end, you're right anyway :)

Btw, it seems that for Mozilla, "addon" is a generic term encompassing themes, extensions and plugins (like a flash plugin, GNU forbid).

I'm specially interested in making this work because it would save a select-copy-open_CLI-write_you+TAB-paste-enter, and repeat on the next video.
But most of all because without Javascript (NoScript extension running for most of my browsing, so no html5), I have Unplug that works quite often, but youtube-dl works nearly all the time. So it's like the lowest, most basic tool that works in the worst conditions (no Js).

About cli apps, I wonder if they are as safe as a fully equipped browser.

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

lol GNU forbid!

Regarding safety, you need to compare specific programs. I don't think you can make a blanket statement like that about command-line programs.

hack and hack
Offline
Iscritto: 04/02/2015

Yeah, I assumed they shared a common ground, like maybe them being text-only (no Js) or something, but overall you're right once again. I will check them one by one (well, the one I want to use, of course).

hack and hack
Offline
Iscritto: 04/02/2015

I found a way to get the URL through scripting here:
https://raymii.org/s/snippets/Get_the_current_or_all_Firefox_tab_urls_in_Bash.html

Here's the adapted script since it's Abrowser (I used the one that gets the current tab):
#!/bin/bash
sed -n "$(python2 <<< $'import json\nf = open("/home/username/.mozilla/abrowser/randomlettersandnumbers.default/sessionstore-backups/recovery.js", "r")\njdata = json.loads(f.read())\nf.close()\nprint str(jdata["windows"][0]["selected"])')p" <(python2 <<< $'import json\nf = open("/home/username/.mozilla/abrowser/randomlettersandnumbers.default/sessionstore-backups/recovery.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")')

It has some weird behavior because it seems to get the current tab only if it's freshly opened. Getting back to a previously opened tab gets only the latest tab. And sometimes not. Weird, as I said. As you can see, the path in Firefox is different: "/home/username/.mozilla/firefox/RANDOM.profile/sessionstore.js".
The author says: "That file is updated almost instantly, however it will not always be the correct url."

This part, which prints the current tab number, is the issue. It doesn't update properly when I display another previously opened tab.
python2 <<< $'import json\nf = open("/home/username/.mozilla/firefox/RANDOM.profile/sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nprint str(jdata["windows"][0]["selected"])'

Refreshing the opened tab makes it work! Yay!

Now, what's the cleanest way to do that?
A simple F5 on tab selection is the desired behavior, but only when I launch the script. That means I need the script to start with something like alt+f5, and maybe give it a second or two.
But maybe there's a Python way to do this, and it's maybe more desirable, I don't know. Suggestions welcomed, as usual!

But I probably can put the output of this in a variable and make it work with the tmptube.sh.

hack and hack
Offline
Iscritto: 04/02/2015

The solution I found works great if used without Launchy (standalone script), but it would need a tab refreshing to be fully functional. But automating F5 from a script looks annoying (external program needed). So I guess I'll just F5 as needed for now. But I probably will only use the script with Launchy since it doesn't need a refresh, and also because the so-called standalone script still needs to have the proper path configured, which isn't automated enough to my taste (say for a fresh install). The best thing would be for youtube-dl to detect the current tab'sURL. But the way to get this would be different from a browser to another, so I doub't it's gonna ever happen.

Both the Launchy and the standalone script (using F5 when needed is acceptable for now) need to be able to launch the media player for different formats.

I'm stuck here:

#!/bin/bash
var=$(sed -n "$(python2 <<< $'import json\nf = open("/home/username/.mozilla/abrowser/RANDOM.default/sessionstore-backups/recovery.js", "r")\njdata = json.loads(f.read())\nf.close()\nprint str(jdata["windows"][0]["selected"])')p" <(python2 <<< $'import json\nf = open("/home/username/.mozilla/abrowser/RANDOM.default/sessionstore-backups/recovery.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")'))

cd ~/tmptube
youtube-dl $var
var2=$(path/to/tmptube/*)
if [ $var2 = *.webm ]
then cvlc 'path/to/*.webm'
rm ~/tmptube/*.webm

elif [ $var2 = *.mp4 ]
then cvlc '/path/to/*.mp4
rm ~/tmptube/*.mp4

else echo "add the file format to tmptube2.sh!"
fi

It picks the right format and downloads successfully, as far as I know. Probably because no format is specified. For some reason, webm is picked when available.
But I need to add the rm commands for each case, and more importantly, vlc doesn't play the downloaded video.
I need to check the syntax. Help welcomed, as usual!

hack and hack
Offline
Iscritto: 04/02/2015

Nevermind, I tried this and it worked :

#!/bin/bash
var=$(sed -n "$(python2 <<< $'import json\nf = open("/home/username/.mozilla/abrowser/RANDOM.default/sessionstore-backups/recovery.js", "r")\njdata = json.loads(f.read())\nf.close()\nprint str(jdata["windows"][0]["selected"])')p" <(python2 <<< $'import json\nf = open("/home/username/.mozilla/abrowser/RANDOM.default/sessionstore-backups/recovery.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")'))
cd ~/tmptube
youtube-dl $var
vlc *.*
rm ~/tmptube/*.*

A tab refresh (F5) might be needed, but that's all.
Little adaptation is needed for it to work with Launchy:
#!/bin/bash
cd ~/tmptube
youtube-dl $1
vlc *.*
rm ~/tmptube/*.*

There's a downside: I'm not sure it picks .webm by default when multiple format's are available.
If I force the format to .webm, all the other formats are blocked.

lembas
Offline
Iscritto: 05/13/2010

Sprinkle in some bash control operators.

You can use command1!!command2 which will run command2 if command1 returns an error, where command1 could be the download with force webm and command2 without it.

(Similarly && instead of !! would run the second command if the first succeeded. Note that these operators test for whether the exit status (of the preceeding command) is zero (great success) or not, i.e. complete failure or anything in between...)

hack and hack
Offline
Iscritto: 04/02/2015

Awesome, thanks lembas!

lembas
Offline
Iscritto: 05/13/2010

Sorry it was supposed to be || instead of !!...

andermetalsh
Offline
Iscritto: 01/04/2013

save this as a script:

cvlc $(youtube-dl -g $(xclip -o))

Then map a keybinding with your favourite desktop
environment as "win+s" or whichever you like.

Hightlight the URL, press "win+s", and enjoy.

lembas
Offline
Iscritto: 05/13/2010

Of course, you have to install xclip first. ;)

pragmatist

I am a member!

Offline
Iscritto: 03/03/2016

Alt+d or Ctrl+l (that is lower case L for location not a 1)

will automatically highlight the URL (a good way to go once you are already using keybindings ;)

hack and hack
Offline
Iscritto: 04/02/2015

Cool, thanks pragmatist :)
This is even better than the shortcut from vimfx, because there's no need for vimfx. I mean the less tweaks needed, the better.

hack and hack
Offline
Iscritto: 04/02/2015

Really nice, thank you andermetalsh!
Despite having to install xclip, I really like it because it's generic: there's no need to get a path inside to make it work.

Very elegant too.

I wonder if it's a good idea to add pragmatist's shortcut (select the URL) in the same script. Only one shortcut.
Ah but if it's the same thing as for F5, it needs xdotools and some configuration. Too bad.

EDIT:
I understood only now that vlc (or cvlc) opens a url, but youtube-dl only gets the url displayed nothing else. There's no download at all.
xdotool is totally worth it.
But since there's no download, it's not possible to keep a copy maybe. If there's a way to make it download the video as well, then my life is complete. Ok, maybe not. I tried a few things but failed miserably for now.
Wait a minute... Since it displays a url, then nesting that thing ($(youtube-dl -g $(xclip -o)) into youtube-dl should work! I need to try now.

The simple version works great for streaming.
I want to make a version that uses the same base to download the video.
But failed for now.

Here's my script (it also creates the temporary download folder if it's not created, and optionally removes it):

cd ~/
mkdir -p ~/tmptube
cd ~/tmptube
xdotool key ctrl+l
var=$(youtube-dl -g $(xclip -o))
youtube-dl -f 43 $var || youtube-dl $var
vlc *.webm || vlc *.*
rm ~/tmptube/*.*
rmdir ~/tmptube

The true beauty of such a code is that it's program-agnostic.
Also, since it goes to the user folder and creates a folder, it's not dependent on any folder pre installed, which might be in another language, thus not recognized.
But mainly, I gathered all the ideas you all shared. I need to test the output of each line in the CLI, patiently.

hack and hack
Offline
Iscritto: 04/02/2015

This one nearly works for downloading:
cd ~/
mkdir -p ~/tmptube
cd ~/tmptube
xdotool key ctrl+l
youtube-dl $(xclip -o)
vlc *.webm || vlc *.*
rm ~/tmptube/*.*
rmdir ~/tmptube

What changed is the -g option that is supposed to simulate and print the URL. with the -g option, it gets one URL (probably the video source), while xclip -o gets the URL displayed in the browser. Clearly the first (very long) URL fails to play in vlc. The other one works.
The two in a row work as well.

But I don't get why the "youtube-dl -g " is needed. Why did you make such a choice, andermetalsh?

Anyway, it just works when it wants. I noticed that it fails when the URL isn't highlighted, for some reason.
So I need to find out why it highlights it only once in a while. It doesn't seem to be a window focus issue.

The last thing I want to try is to make it prefer webm. the format forcing aside, I see youtube-dl has an option about prefering free formats. I'll try both.

EDIT:
Still the same script, fails at first, then worked several times in a row. Go figure. Even the highlighting is consistent.

hack and hack
Offline
Iscritto: 04/02/2015

Final script, thanks to pragmatist, lembas and andermetalsh!
This one temporarily downloads the video. Besides needing youtube-dl, xclip and xdotools installed, it's completely standalone and can be launched either from launchy, a keyboard shortcut etc.

The streaming version is a bit different and much simpler (discribed above by andermetalsh). I'll probably use both, but then I'd need to remove the 2 last lines in order to keep my downloads with this one. The 3rd one also if you don't want vlc playback.

#!/bin/bash
cd ~/
mkdir -p ~/tmptube
cd ~/tmptube
xdotool key ctrl+l
youtube-dl -f 43 $(xclip -o) || youtube-dl $(xclip -o)
vlc *.webm || vlc *.*
rm ~/tmptube/*.*
rmdir ~/tmptube

Forcing webm with an alternative works. Playing with youtube-dl options from there is just personal preference/details.

It seems I just need to let it fail once or twice, then it works all the time.

andermetalsh, please help me understand why did you use youtube-dl -g . I don't get it.
It simulates the download at the source. But then you have 2 URLs in a row. What's the point?
Wait: youtube-dl prints a url without acting on it, which is based on the url displayed in the browser.
Ok it makes more sense. So it's like getting directly to the source I suppose.

But why is it needed?

andermetalsh
Offline
Iscritto: 01/04/2013

"youtube-dl -g" prints the video's full URL, useful for non-youtube parsing video players like Mplayer, MPV and such.

hack and hack
Offline
Iscritto: 04/02/2015

If I understand, these other players can't parse a youtube URL (or video, whatever parsing exactly means). the -g options does that for them, is that it ?

I just found yet another option, sinc MPV integrates youtube-dl for streaming URLS: https://addons.mozilla.org/en-us/firefox/addon/watch-with-mpv/

Might be worth using for anyone who wants a simple way (and simple to install) to play videos without javascript/flash/html5. No scripts or anything.

I personally like the scripted way more, because it's also browser-agnostic.

Thanks again for your help everyone.

SuperTramp83

I am a translator!

Offline
Iscritto: 10/31/2014

I'd like to chime in and say that I just copy the youtube link and paste it in smplayer, the vid gets played and that is all.
Another great application by the same developer (Riccardo Villalba) of smplayer is smtube, with which you can browse, view and download YT videos. But you'll need to compile it and disable javascript and cookies (it takes 10 seconds).