oki_one
Posts: 46
Joined: Thu Nov 28, 2013 2:39 pm

how to play youtube video with omxplayer

Tue Dec 17, 2013 10:45 pm

Hi There,
Anyone try to play youtube videos with omxplayer?
Thanks
Oki

gkreidl
Posts: 6053
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to play youtube video with omxplayer

Wed Dec 18, 2013 4:32 am

You need to extract the video URL from the website first. youtube-dl can do this for you.

sudo apt-get install youtube-dl

youtube-dl can update itself to the newest version (you should do this regularly):

sudo youtube-dl -U
sudo youtube-dl -U

That's not a mistake: if installed for the first time, it needs two updates.

Then run
youtube-dl -g url

where "url" is the url of the website that contains the video. youtube-dl will output the video url then. Play it with
omxplayer "vurl"

where vurl is the video url you got from youtube-dl. You must use single or double quotes.

Or:

Install Minimal Kiosk Browser. Navigate to the video website. Click the "Play" icon in the toolbar. After a few seconds, omxplayer will start to play the video full screen. (youtube-dl must be installed!).
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

oki_one
Posts: 46
Joined: Thu Nov 28, 2013 2:39 pm

Re: how to play youtube video with omxplayer

Wed Dec 18, 2013 6:18 pm

Hi First, thanks for answer.
I am using arch linux.
I installed youtube-dl with below command
pacman -S youtube-dl

then , i create yt.py for catch and play url.
-------yt.py--------------------------------------
import sys, os
url=os.system('youtube-dl -g https://www.youtube.com/watch?v=vw61gCe2oqI')
os.system('omxplayer -o hdmi '+url)
----------------------------------------------------
rasult is below. What's wrong this picture?

[[email protected] home]# python yt.py
WARNING: Assuming --restrict-filenames since file system encoding cannot encode all charactes. Set the LC_ALL environment variable to fix this.
http://r1---sn-o097zned.googlevideo.com ... 1AA944E44C
Traceback (most recent call last):
File "yt.py", line 4, in <module>
os.system('omxplayer -o hdmi '+url)
TypeError: Can't convert 'int' object to str implicitly
[[email protected] home]#

oki_one
Posts: 46
Joined: Thu Nov 28, 2013 2:39 pm

Re: how to play youtube video with omxplayer

Wed Dec 18, 2013 6:52 pm

I also try

import sys, os
url=os.system('youtube-dl -g https://www.youtube.com/watch?v=vw61gCe2oqI')
os.system('omxplayer -o hdmi '+str(url))

but still not working

gkreidl
Posts: 6053
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to play youtube video with omxplayer

Wed Dec 18, 2013 10:43 pm

os.system only returns the return code (an integer), not the output of the called program. Use something like

Code: Select all

import subprocess

yta = ['youtube-dl', '-g',url]
yt = subprocess.Popen(yta,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(res,err) = yt.communicate()
if res and not err:
the output is in res ....
Last edited by gkreidl on Sun Dec 22, 2013 10:48 pm, edited 1 time in total.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

oki_one
Posts: 46
Joined: Thu Nov 28, 2013 2:39 pm

Re: how to play youtube video with omxplayer

Sun Dec 22, 2013 6:52 pm

Thanks for answer ,

I try to run this code i get below error.

if res and not err:
^
IndentationError: unexpected indent

my url is
url = 'https://www.youtube.com/watch?v=_hK-gQb9pUk'

This code suppose to be run url youtube link with omxplayer right?
Oki

gkreidl
Posts: 6053
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to play youtube video with omxplayer

Sun Dec 22, 2013 10:47 pm

Sorry, don't know how the indentation got in here; "if" must be at the beginning of the line in this example. (I'll edit the code above)

in "res" is the resulting video stream url, that can be played with omxplayer.

And no, 'https://www.youtube.com/watch?v=_hK-gQb9pUk' is not the URL that can be played with omxplayer, it's the the URL of the video page. youtube-dl can extract the video URL for you from this link.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

oki_one
Posts: 46
Joined: Thu Nov 28, 2013 2:39 pm

Re: how to play youtube video with omxplayer

Mon Dec 23, 2013 3:12 pm

Ok here last status;
When i print res value with below code i got something like this,

b'http://r1---sn-p5qlsn7e.googlevideo.com ... 0409D69C\n'

Code: Select all

import subprocess

url='https://www.youtube.com/watch?v=vw61gCe2oqI'
yta = ['youtube-dl', '-g',url, '--restrict-filenames']
yt = subprocess.Popen(yta,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(res,err) = yt.communicate()
if res and not err:
   print(res)
i think youtube url is b'YOUTUBE_URL\n
i try to play this url with below code

Code: Select all

omxplayer -o hdmi YOUTUBE_URL
i got this;

[1] 645
[2] 646
[3] 647
[4] 648
[5] 649
[6] 650
-bash: 027b6a2a2: command not found
[7] 651
[8] 652
[9] 653
[10] 654
[11] 655
[12] 656
[13] 657
[14] 658
-bash: fexp: command not found
[15] 660
-bash: 78DC70C8E240D16.6AD4C89C0CD74A9280AACDF32386FE614AEBB427: command not found
[2] Done expire=1387836044
[3] Done sparams=id%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire
[4] Exit 127 id=bf0eb58 027b6a2a2
[5] Done sver=3
[6] Done upn=4TwE7O0enOY
[7] Done source=youtube
[8] Done ratebypass=yes
[9] Done mv=m
[10] Done key=yt5
[11] Exit 127 fexp =909717%2C932295%2C936912%2C936910%2C923305%2C936913%2C907231%2C907240%2C921090
[12] Done itag=22
[13] Done ms=au
[14]- Done ip=x.x.x.x
[15]+ Done mt=1387811411
[[email protected] home]#

I got correct url and try to play. But still nothing

gkreidl
Posts: 6053
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to play youtube video with omxplayer

Mon Dec 23, 2013 5:35 pm

you must strip the line feed (\n) from the result. And you must quote the video url.

Try from the command line first:
youtube-dl -g 'https://www.youtube.com/watch?v=vw61gCe2oqI'

that will give you the videourl

then

omxplayer 'videourl'

(in single or double quotes).
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

arti0504
Posts: 24
Joined: Wed Jan 30, 2013 8:40 am

Re: how to play youtube video with omxplayer

Thu Apr 24, 2014 7:30 am

I have one Raspberry Pi where I run Motion. I can see video from my web camera. I must write 192.168.0.18:8081 to web browser and it's works in standard PC.

I would like use second Raspberry Pi to see this see video from my web camera. Can I use omxplayer for do this? What write to command line to do it?

oozo
Posts: 15
Joined: Wed Apr 15, 2015 1:04 am

Re: how to play youtube video with omxplayer

Thu May 07, 2015 2:38 pm

When i use the Play button in the Kweb3 the youtube-dl find the video url, send to omxplayer and play it ?

This full screen from my sistem is running from an iframe. Can i integrate something to meke it run from omx ?

gkreidl
Posts: 6053
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to play youtube video with omxplayer

Thu May 07, 2015 8:13 pm

oozo wrote:When i use the Play button in the Kweb3 the youtube-dl find the video url, send to omxplayer and play it ?

This full screen from my sistem is running from an iframe. Can i integrate something to meke it run from omx ?
If you use local html files and include the web content within iframes you can use the command interface of kweb. Command links as src of iframes can be used to start any kind of program. You can use omxplayerGUI to play youtube video (not only in windows but also without any interface full screen or in a dedicated screen area). Have a look at the omxplayerGUI manual for details.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

oozo
Posts: 15
Joined: Wed Apr 15, 2015 1:04 am

Re: how to play youtube video with omxplayer

Thu May 07, 2015 8:47 pm

K, Thx

Will do it today

S0AndS0
Posts: 6
Joined: Thu Mar 12, 2015 6:47 am
Contact: Website

Re: how to play youtube video with omxplayer

Sun May 24, 2015 7:03 pm

In one line to play YouTube from command line using an SSH connection;

Code: Select all

omxplayer -o local --vol -2000 "$(youtube-dl -g '<YouTube_URL>')"
~ The -o can be set to "hdmi" or "both" for audio output
~ The --vol can be set between "-4000" to "-0" and controles the audio level
~ The "$()" is a trick for subsitution of output of one command's result into the input of another command; in this case the results of "youtube-dl -g '<>'"
~ The extra '' (single quotes) are to keep the "" (duble quotes) from interfearing with one another

This one command plus one line can likely be used in a script like so;

Code: Select all

#!/bin/bash
YT_URL="$1"
omxplayer -o local --vol -2000 "$(youtube-dl -g $YT_URL)"
~ and run with

Code: Select all

./script '<YouTube_URL>'
It'll take a few seconds to start but once started all the standard omx seem to work. Likely the above could be used to read a file as an array and then play videos as a playlist... but that's for another post...

~~ Edit ~~ after testing above suggestions I've updated commands and script example to match my working configs.
Additinal note; I start these commands within a session handler, screen or tmux, such that re-ataching after a disconnect over SSH isn't a problem ;-)

Happy hacking and modding.


~~ Edit - 2 ~~ ok so I couldn't help myself; here's a little script for playing YouTube links from a file, ie a playlist

Code: Select all

#!/bin/bash
FileToPlay="$1"
for LINK in $(cat $FileToPlay)
do
    echo "Playing : $LINK"
    omxplayer -o hdmi --vol -1750 "$(youtube-dl -g $LINK)"
done
~ Save a file with links sepperated by new line and then feed the above script that file like so

Code: Select all

./script '<file>'
~ I add to the file while playing with the following command

Code: Select all

echo "<URL>" | tee -a /<file>
~ The "| tee -a" part pipes the echo'ed URL into the bottom of "file" (-a is append ;-))

~~ Edit 3 ~~
So my thoughts of just adding to the exsisting file didn't work; here's how I corected for that with another for-do-done check for uniqueness

Code: Select all

#!/bin/bash
FileToPlay="$1"
for LINKs in $(cat $FileToPlay)
do
    omxplayer -o hdmi --vol -1500 $(youtube-dl -g $LINKs)
    echo "$LINKs" | tee -a $FileToPlay.played
done
for uLINK in $(sort $FileToPlay $FileToPlay.played | uniq -u)
do
    omxplayer -o hdmi --vol -1500 $(youtube-dl -g $uLINK)
done
echo "Removing : $FileToPlay.played"
rm $FileToPlay.played
echo "Exiting now"
~ The above will check once reaching the original end of $FileToPlay for anything that has been added, load those new entries as a new temp-list to play, then it cleans up after it's self without touching the original play-list file.

S0AndS0
Posts: 6
Joined: Thu Mar 12, 2015 6:47 am
Contact: Website

Re: how to play youtube video with omxplayer

Thu May 28, 2015 6:19 am

Here's a simple script for playing play-lists from YouTube via the raw play-list link from YouTube.

Dependancies; omxplayer youtube-dl lynx
Save in: /usr/local/sbin/yt_playList.sh
Grant permissions with: chmod +x /usr/local/sbin/typlayList.sh
Run with: yt_playList.sh "<Link_to_playList>"

Code: Select all

#!/bin/bash
if [ ${#@} = 0 ]
then
	echo "I need a YouTube play-list link. Exiting now..."
	exit 1
fi
PlayList_Link=$1
OUT="both"
VOL="1550"
PlayLinks(){
	for lines in $(lynx --source "$PlayList_Link" | grep -i "watch?v=" | uniq -u | grep -o "data-video-id.*" | awk '{gsub("\""," "); print "https://youtube.com/watch?v="$2}')
	do
		echo "Playing $lines"
		echo "omxplayer -o $OUT --vol -$VOL \"\$(youtube-dl -g $lines)\""
		omxplayer -o $OUT --vol -$VOL "$(youtube-dl -g $lines)"
		clear
	done
}
PlayLinks
Usage and script walk-through;
The above first checks that an argumet is passed with the script; thats the [ ${#@} = 0 ] if then check bit.
Then $1 is read; this should be a play-list link that is publicly visable on YouTube. Example link : https://www.youtube.com/playlist?list=P ... wBX0K8uU2R
~ this has the "/playlist?list" section in the URL and has so far worked with the above script for parcing and playing.
Variables you'll want to edit are;

Code: Select all

OUT="both"
VOL="1550"
~ the first selects output; hdmi and local and both are accepted arguments. The second selects volume and can range from 3000 to 0
Most of the heavy lifting is done with~

Code: Select all

lynx --source "$PlayList_Link" | grep -i "watch?v=" | uniq -u | grep -o "data-video-id.*" | awk '{gsub("\""," "); print "https://youtube.com/watch?v="$2}'
~ this first dumps the source of the given page link, then hunts for uniq "watch?v" portions of that page, then only outputs lines from those results that include "data-video-id" (the differance in -i vs -o ;-)), these lines are then cleaned up and formated with awk such that they can be fead into a file or input of another program
Finally the whole thing is wrapped up with

Code: Select all

omxplayer -o $OUT --vol -$VOL "$(youtube-dl -g $lines)"
~ the $lines variable is set with the 'for do done' loop and is what the previous line provided.

Likely I'll combine the two such that there is a history temp file that's made and looked for durring run-time such that premeture exits and then re-running against the same play-list link will result in resuming the play-list on the very next video that was not priviously played... perhaps a GitHub repo will appier with the varias tools I've been working on some day soon...

CartoonSitty
Posts: 10
Joined: Sat Jan 20, 2018 2:18 am

Re: how to play youtube video with omxplayer

Thu Feb 01, 2018 8:37 pm

I'm trying the steps in the top post, but when I try: youtube-dl -g url it says "failed to extract uploader nickname"
anyone know what gives??

gkreidl
Posts: 6053
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to play youtube video with omxplayer

Thu Feb 01, 2018 9:16 pm

CartoonSitty wrote:
Thu Feb 01, 2018 8:37 pm
I'm trying the steps in the top post, but when I try: youtube-dl -g url it says "failed to extract uploader nickname"
anyone know what gives??
perhaps you are using a very old version of youtube-dl.
If you are on Stretch, remove it and install a new version using pip
sudo pip install youtube-dl
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12125
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: how to play youtube video with omxplayer

Fri Feb 02, 2018 12:14 am

CartoonSitty wrote:
Thu Feb 01, 2018 8:37 pm
I'm trying the steps in the top post, but when I try: youtube-dl -g url it says "failed to extract uploader nickname"
anyone know what gives??
those steps are from 2015, so since several new versions of raspbian have come out, I would consider what is written as obsolete....

Return to “Python”