View Single Post
Posts: 187 | Thanked: 345 times | Joined on Nov 2009
#1
Hello everyone.

I've hacked together a small tool that lets you:
  • Search for videos
  • Download videos via a queue that runs in the background
  • Manipulate the download queue
  • Watch videos directly without the queued download

It's in extras-devel.

But why? There are plenty of GUI frontends for youtube already! True, but to be honest after trying all of them, I found that not a single one of them is usable for me. They all are slow to load and navigate (the N900 is to blame for this, not the authors of the GUI tools), searching is a pain (The Youtube API is responsible for this one) and all I really want is to quickly find and download and watch some videos that I remember having seen on youtube - this is my use case.

About the Youtube API problem (you can skip this): The search results the Youtube API gives are utter shite. For example, try doing a web search for "gremlinz aspect gavel" and you'll find a perfectly fine list of results. However supplying the Youtube API with this search string reveals no results at all. All of the GUI tools are using the API directly and hence I can't really find anything I previously found on a PC. With this CLI tool, you can just search for something really broad and then grep for whatever is important to you.

Demonstration:
Let's do a search for 'gremlinz'. The Youtube API provides 50 search results at a time at max. Run the same command repeatedly and it will show 50 more results each time.
Code:
user@Nokia-N900:~/ytcli$ ytcli -f "gremlinz"
1: GBP.Gremlinz Teaser.
2: GBP.Gremlinz.NORTHSTAR 2010. Caked
3: GBP Boreal Gremlinz 2010
4: Gremlinz & Stranjah - Blues VIP (Metalheadz Exclusive) [HD]
5: Gremlinz - Armshouse
6: GBP.Gremlinz.RIP KingVale.2010
7: Terror Danjah Gremlinz (The Instrumentals) Planet Mu 29/9/08 3LP/CD
8: Gremlinz - Annexed
--snip--
So I wanted to find a track by Gremlinz called 'The Gavel'. Note that the Youtube API provides zero results for "gremlinz gavel" or similar terms. This is why it sucks. But no problem:
Code:
user@Nokia-N900:~/ytcli$ ytcli -f "gremlinz" | grep -i "gavel"
28: Gremlinz featuring Aspect - The Gavel [HD]
There it is! I want to queue that track for download:
Code:
user@Nokia-N900:~/ytcli$ ytcli -q 28
Added: Gremlinz featuring Aspect - The Gavel [HD]
It will now be downloaded to /home/user/MyDocs/.videos/ytcli/. When it's done there will be a banner info and I can check what's in the queue and how far the download is using the -s trigger:
Code:
user@Nokia-N900:~/ytcli$ ytcli -s
>> 1 downloads in the queue:
0: Gremlinz featuring Aspect - The Gavel [HD]
>> Current or most recent download log:
[youtube] Setting language
[youtube] AnPDHRqLVEk: Downloading video webpage
[youtube] AnPDHRqLVEk: Downloading video info webpage
[youtube] AnPDHRqLVEk: Extracting video information
[download] Destination: Gremlinz_featuring_Aspect_The_Gavel_HD-AnPDHRqLVEk.flv
[download]   7.4% of 12.20M at  227.32k/s ETA 00:50
Let's search and queue some more stuff...
Code:
user@Nokia-N900:~/ytcli$ ytcli -f "technical itch" | grep -i rukus
1: Technical Itch - The Rukus
8: Tech itch 1337-The Rukus
23: Technical Itch - The Rukus
38: Technical Itch - The Rukus (D. Kay VIP)
user@Nokia-N900:~/ytcli$ ytcli -q 38
Added: Technical Itch - The Rukus (D. Kay VIP)
user@Nokia-N900:~/ytcli$ ytcli -s
>> 2 downloads in the queue:
0: Gremlinz featuring Aspect - The Gavel [HD]
1: Technical Itch - The Rukus (D. Kay VIP)
>> Current or most recent download log:
[youtube] Setting language
[youtube] AnPDHRqLVEk: Downloading video webpage
[youtube] AnPDHRqLVEk: Downloading video info webpage
[youtube] AnPDHRqLVEk: Extracting video information
[download] Destination: Gremlinz_featuring_Aspect_The_Gavel_HD-AnPDHRqLVEk.flv
[download]  21.3% of 12.20M at   79.78k/s ETA 02:03
user@Nokia-N900:~/ytcli$ ytcli -f "a-sides" | grep -i ruff
4: a sides - ruff it up
user@Nokia-N900:~/ytcli$ ytcli -q 4
Added: a sides - ruff it up
Now there are three items in the queue:
Code:
user@Nokia-N900:~/ytcli$ ytcli -s
>> 3 downloads in the queue:
0: Gremlinz featuring Aspect - The Gavel [HD]
1: Technical Itch - The Rukus (D. Kay VIP)
2: a sides - ruff it up
>> Current or most recent download log:
[youtube] Setting language
[youtube] AnPDHRqLVEk: Downloading video webpage
[youtube] AnPDHRqLVEk: Downloading video info webpage
[youtube] AnPDHRqLVEk: Extracting video information
[download] Destination: Gremlinz_featuring_Aspect_The_Gavel_HD-AnPDHRqLVEk.flv
[download]  39.5% of 12.20M at   56.83k/s ETA 02:12
Now I decide that I actually don't want the A-Sides video, so I remove it:
Code:
user@Nokia-N900:~/ytcli$ ytcli -r 2
Removed: a sides - ruff it up
user@Nokia-N900:~/ytcli$ ytcli -s
>> 2 downloads in the queue:
0: Gremlinz featuring Aspect - The Gavel [HD]
1: Technical Itch - The Rukus (D. Kay VIP)
>> Current or most recent download log:
[youtube] Setting language
[youtube] AnPDHRqLVEk: Downloading video webpage
[youtube] AnPDHRqLVEk: Downloading video info webpage
[youtube] AnPDHRqLVEk: Extracting video information
[download] Destination: Gremlinz_featuring_Aspect_The_Gavel_HD-AnPDHRqLVEk.flv
[download]  44.2% of 12.20M at   55.05k/s ETA 02:06
Of course you can also play videos right away without queing them:
Code:
user@Nokia-N900:~/ytcli$ ytcli -f "katamari damacy" | grep -i "lonely"
28: Katamari Damacy Soundtrack - 05 - Lonely Rolling Star
36: Best VGM 329 - Katamari Damacy - Lonely Rolling Star
user@Nokia-N900:~/ytcli$ ytcli -p 28
Fetching Katamari Damacy Soundtrack - 05 - Lonely Rolling Star
MPlayer SVN-r30099-4.2.1 (C) 2000-2009 MPlayer Team

Playing http://v8.lscache4.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0hQSFJSVF9FSkNOMF9KTFhF&fexp=907060%2C906717%2C904102&algorithm=throttle-factor&itag=34&ip=0.0.0.0&burst=40&sver=3&signature=3719BB98DF5B74187F2D43A4C74A52289A147CEB.D968A7D6FB7B0CA7113C38FAAD0B00CE086679A4&expire=1313276400&key=yt1&ipbits=0&factor=1.25&id=eff43274d5c8fe89.
Resolving v8.lscache4.c.youtube.com for AF_INET6...
Connecting to server v8.lscache4.c.youtube.com[42.0.20.80]: 80...
Failed to connect to server with AF_INET6
Resolving v8.lscache4.c.youtube.com for AF_INET...
Connecting to server v8.lscache4.c.youtube.com[193.192.226.165]: 80...
Cache size set to 320 KBytes
Cache fill: 17.50% (57344 bytes)   
libavformat file format detected.
--snip--
Note that complete or even partial downloads will never be deleted. Here's the help:

Code:
user@Nokia-N900:~/ytcli$ ytcli -h
Usage: ytcli.py [-a] {-f TERM|-r ID|-q ID|-p ID|-y YID|-s|-u|-k|-x REALLY} 

Options:
  -h, --help            show this help message and exit
  -f TERM, --find=TERM  do a search for TERM. Searching repeatedly for the
                        same TERM will load 50 more results
  -q ID, --queue=ID     queue the search result number ID for downloading
  -a, --authors         also show authors of videos
  -s, --status          show the status of the download queue
  -p ID, --play=ID      play the video number ID as a direct stream
  -y YID, --play-youtube=YID
                        play a video with youtube id YID
  -k, --kill            kill the queue downloader. Partially downloaded
                        videos will be continued in the future.
  -u, --resume          start the downloader and continue downloading
  -r ID, --remove=ID    Remove position ID from the download queue. Removing
                        ID 0 cancels the current download.
  -x REALLY, --reset=REALLY
                        kill the queue and remove all ytcli temp files.
                        Supply 'REALLY' as an argument to do this
So, the code has a couple of uncaught exceptions and it's not exactly a masterpiece of beauty (code-wise) but at least for me, it gets the job done a lot quicker than all the GUI tools.

I just -f | grep what I need, -q it and when the banner comes up I can play it in mplayer. Or I can play it immediately using -p.

By the way, the downloader runs as long as there is stuff in the queue. If you want to interrupt the download and pick it up at a later point, run it with -k to kill it, then later with -u to resume. It will continue where it stopped. ytcli depends on mplayer for playback and youtube-dl for downloading.



Enjoy!


known issues / room for improvement
- if it happens to you that a video only plays the first few frames (in mplayer), press SPACE twice. This is a problem with mplayer.
- sometimes webm videos get downloaded, not sure what to do with them yet since mplayer doesn't seem to be willing to play them
- fancy features like extracting sound or converting videos to a native codec/container
- trying to use maemo media player instead of mplayer to make use of the DSP
- all ytcli temp files get wiped upon a reboot
__________________
Author of:
  • The "Alarmed" scheduling and automation app. (Supports alarms, switching profiles, resetting the GPRS counter, setting custom commands to go off, cron syntax and much more. GUI and CLI).
  • "Reaction face-off", a two-player puzzle/knowledge/reaction game.
  • "ytcli", a simple but effective command line youtube client which allows you to play videos from the CLI instantly or download videos on a queue.

Last edited by shapeshifter; 2011-08-13 at 22:11.
 

The Following 8 Users Say Thank You to shapeshifter For This Useful Post: