Bellagio
Posts: 19
Joined: Tue Jun 12, 2012 3:23 pm

[player youtube]

Tue Jun 12, 2012 3:41 pm

Hi,

I propose a simple way to watch (or to listen) youtube's videos without flash plugin.

Code: Select all

#! /usr/bin/env python2.7

import sys
import subprocess
import getopt
import gdata.youtube
import gdata.youtube.service

class bcolors:

    pink = '\033[95m'
    blue = '\033[94m'
    green = '\033[92m'
    yellow = '\033[93m'
    red = '\033[91m'
    endc = '\033[0m'

class Options:
    
    def __init__(self):
        pass
    
    def __del__(self):
        pass
    
    def set_options(self, novideo, author, start_index, orderby, 
                    max_results, query, tags, verbose, fullscreen,
                    quality, hd):
        self.novideo = novideo
        self.author = author
        self.fullscreen = fullscreen
        self.start_index = start_index
        self.order_by = orderby
        self.max_results = max_results
        self.query = query
        self.tags = tags
        self.verbose = verbose
        self.quality = quality
        self.hd = hd

def print_entry_details(entry):

    print bcolors.blue + '\nVideo title' + bcolors.endc + ': %s' % entry.media.title.text
    print bcolors.blue + 'Video published on' + bcolors.endc + ': %s ' % entry.published.text
    print bcolors.blue + 'Video description' + bcolors.endc + ': %s' % entry.media.description.text
    print bcolors.blue + 'Video category' + bcolors.endc + ': %s' % entry.media.category[0].text
    print bcolors.blue + 'Video tags' + bcolors.endc + ': %s' % entry.media.keywords.text
    print bcolors.blue + 'Video watch page' + bcolors.endc + ': %s' % entry.media.player.url
    print bcolors.blue + 'Video flash player URL' + bcolors.endc + ': %s' % entry.GetSwfUrl()
    print bcolors.blue + 'Video duration' + bcolors.endc + ': %s' % entry.media.duration.seconds
    print bcolors.blue + 'Video view count' + bcolors.endc + ': %s' % entry.statistics.view_count
    print bcolors.blue + 'Video rating' + bcolors.endc + ': %s\n' % entry.rating.average

def parse_video_feed(feed, opts):

    for entry in feed.entry:
        if (opts.verbose):
            print_entry_details(entry)
        play_video(entry.media.player.url, opts.novideo, opts.fullscreen, opts.quality)

def play_video(url, novideo, fullscreen, quality):

    cookie_file = '/var/tmp/youtube-dl-cookies.txt'
    mplayer = 'mplayer '

    if (novideo):
        mplayer += '-novideo '
    if (fullscreen):
        mplayer += '-fs '

    mplayer += '-cookies -cookies-file "%s" $(youtube-dl --max-quality "%s" -g --cookies "%s" "%s") &>/dev/null'\
        %(cookie_file,quality,cookie_file,url)

    mplayer=["bash", "-c", mplayer]
    subprocess.call(mplayer)

def search_videos_by_keywords(opts):

    yt_service = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()
    query.orderby = opts.order_by
    query.racy = 'include'
    query.start_index = opts.start_index
    query.max_results = opts.max_results
    query.hd = opts.hd

    if (opts.author != ''):
        query.author = opts.author
    if (opts.query != ''):
        query.vq = opts.query
    if (opts.tags != ''):
        new_term = opts.tags.lower()
        query.categories.append('/%s' % new_term)

    feed = yt_service.YouTubeQuery(query)
    parse_video_feed(feed, opts)

def usage(av):

    print '\nusage: %s  '%(av[0]) + '[' + bcolors.green +\
    'OPTIONS' + bcolors.endc + ' | ' + bcolors.green + 'URL' + bcolors.endc + ']\n'
    print bcolors.green + 'options:' + bcolors.endc
    print bcolors.red + '\t -a' + bcolors.endc + '\t author (YouTube username)'
    print bcolors.red + '\t -f' + bcolors.endc + '\t fullscreen'
    print bcolors.red + '\t -i' + bcolors.endc + '\t start_index'
    print bcolors.red + '\t -o' + bcolors.endc + '\t order by (relevance, viewCount, published, or rating)'
    print bcolors.red + '\t -m'+ bcolors.endc +'\t max_results'
    print bcolors.red + '\t -n'+ bcolors.endc +'\t no video'
    print bcolors.red + '\t -q'+ bcolors.endc +'\t search query term'
    print bcolors.red + '\t -t'+ bcolors.endc + '\t tags'
    print bcolors.red + '\t -v'+ bcolors.endc +'\t verbose'
    print bcolors.red + '\t -x'+ bcolors.endc +'\t max quality format(5 6 34 35 18 22 37 38 83 82 85 84)\n'

def main(ac, av):

    novideo = False
    author = ''
    start_index = 1
    orderby = 'relevance' 
    max_results = 50
    quality = 34
    query = ''
    tags = ''
    verbose = False
    fullscreen = False
    hd = False

    if (ac < 2) or ((ac == 2) and ((av[1] == '-h') or (av[1] == '--help'))):
        usage(av)
        sys.exit(1)

    try:
        opts, args = getopt.getopt(av[1:], "a:fi:o:m:nq:t:vx:",
                                   ["author=","index=", 
                                    "orderby=", "max=", 
                                    "query=", "tags=",
                                    "quality="])
    except getopt.GetoptError:
        usage(av)
        sys.exit(2)

    if not opts:
        play_video(av[1], False, True, quality)
        sys.exit(0)

    for opt, arg in opts:
        if opt in ("-a", "--author"):
            author = arg
        elif opt in ("-i", "--index"):
            start_index = int(arg)
        elif opt in ("-o", "--orderby"):
            orderby = arg
        elif opt in ("-m", "--max"):
            max_results = int(arg)
        elif opt in ("-n"):
            novideo = True
        elif opt in ("-f"):
            fullscreen = True
        elif opt in ("-q", "--query"):
            query = arg
        elif opt in ("-t", "--tags"):
            tags = arg
        elif opt == '-v':
            verbose = True
        elif opt in ("-x", "--quality"):
            quality = int(arg)
            if (quality > 36):
                hd = True

    opt = Options()
    opt.set_options(novideo, author, start_index, orderby, 
                    max_results, query, tags, verbose, fullscreen, 
                    quality, hd);
    
    search_videos_by_keywords(opt)

if __name__== '__main__':
    main(len(sys.argv), sys.argv)
Dependencies:
- mplayer2
- youtube-dl
- python2.7
- python-gdata

ironzorg
Posts: 1
Joined: Wed Jun 13, 2012 6:58 am

Re: [player youtube]

Wed Jun 13, 2012 6:59 am

Incredibly useful when sharing links on IRC (and other IMs). Thanks.

Bellagio
Posts: 19
Joined: Tue Jun 12, 2012 3:23 pm

Re: [player youtube]

Wed Jun 13, 2012 11:14 am

ironzorg wrote:Incredibly useful when sharing links on IRC (and other IMs). Thanks.
Thanks. The next version will be with PyQt :)

Return to “Python”