Reply
Thread Tools
gogol's Avatar
Posts: 177 | Thanked: 57 times | Joined on Aug 2007 @ Washington State, USA
#1
Edit: Scroll down to the bottom to get the newest perl script. You simply bookmark videos you're interested in and then run the script. It scans your bookmarks, downloads and properly names them - storing em in 770's .videos folder. Only requirement is wget ( link )
-------
Hi all, I'm new here
I was really disappointed that I couldn't get youtube rtsp links from http://m.youtube.com to work with mplayer... so after a bit of hacking and googling I came up with this instead, which is actually a bit nicer since you aren't limited to teh mobile collection of vids:

Basically it involves a bash script that wgets (through the goofy youtube redirection that tries to hide the direct link to) the video and then plays it out, optionally deleting the file afterwards.

To do it I had to install bash and wget, available here:

bash
wget

..then make a script that goes like so:
Code:
#!/usr/bin/bash
baseurl=’http://youtube.com/get_video.php?’
pageurl=$1
wget -O - $baseurl`wget -qO - $pageurl | grep player2.swf | cut -d? -f2 | cut -d\” -f1` > /home/user/tmptube.flv
mplayer /home/user/tmptube.flv
(taken with very little change from here )

chmod a+x your script and mv it to /usr/bin to make things easier later.

Obviously you'll also need the osso-xterm package installed, I'm assuming if you got this far you have it already.

Then you just copy the name of the vid you want to watch, paste it in to the terminal after the name of your script and away you go!

Now, is there any way to expediate this? Copy/open terminal/paste... Can mplayer stream the unmungled http link directly? Any ideas?

Last edited by gogol; 2007-08-18 at 05:38. Reason: to point to updated script
 

The Following 2 Users Say Thank You to gogol For This Useful Post:
aflegg's Avatar
Posts: 1,463 | Thanked: 81 times | Joined on Oct 2005 @ UK
#2
This is basically what UKTube does, and arguably what VidConvert does (switch mplayer for 770-encode)
__________________
Andrew Flegg -- mailto:andrew@bleb.org | http://www.bleb.org
Now known as
Jaffa
 
gogol's Avatar
Posts: 177 | Thanked: 57 times | Joined on Aug 2007 @ Washington State, USA
#3
Like I said I'm new here. Lots of searching never led me to uktube. I see it is dependent upon python as well, this way is not. And vidconvert would just be waay too time consuming. (like you said, arguably)

What I'm thinking is a slicker bash script that scans your bookmarks for any direct links to .flv files and then prompts for which one to download/watch. This way you could right-click and bookmark the video and then call the script from a shortcut in the menu. A xterm would pop up and you'd select. A little cleaner..
 
gogol's Avatar
Posts: 177 | Thanked: 57 times | Joined on Aug 2007 @ Washington State, USA
#4
An alternate way to do this that doesn't require bash or wget:
Install links (.40 mb)
Use perl script below. It also works for ifilm.com. Found here (Note: Original script uses lynx instead of links)

Code:
#!/usr/bin/perl
# Filename:	myfilm
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License/
# Description:	Download iFilm/youtube movies given a film ID.
# Converts youtube films from flash to .mov if ffmpeg is available.
  my $VERSION=  '1.02';

use strict;

##################################################
# Setup the variables
##################################################
my $PROGNAME = $0; $PROGNAME =~ s|.*/||;
my ($BASENAME,$PROGNAME) = ($0 =~ m|(.*)/(.+)|) ? ($1?$1:'/',$2) : ('.',$0);

# This will probably change..
my $IFILM = "http://www.ifilm.com/player/mac.jsp?ifilmId=";
my $YOUTUBE = "http://youtube.com/get_video.php?l=165&video_id=";

# How to fetch web files - pick one
# Actual Safari agent example:
# Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/416.12 (KHTML, like Gecko) Safari/416.13
my $GET = "GET -H 'user-agent: Mozilla/5.0'";
my $LYNX = "links -source";

my $GET_URL = $LYNX;

my $POST = "mov";

# For converting youtube to .mov
my $FFMPEG = "ffmpeg";

##################################################
# Usage
##################################################
sub fatal {
	foreach my $msg (@_) { print STDERR "[$PROGNAME] ERROR:  $msg\n"; }
	exit(-1);
}

sub usage {
	foreach my $msg (@_) { print STDERR "ERROR:  $msg\n"; }
	print STDERR <<USAGE;

Usage:\t$PROGNAME [-d] <ifilm URL, youtube URL or ifilm id>
  Download an ifilm movie
  -o <file>         Save to file
  -bw <56,200,300>	Force a bandwidth other than your iFilm settings
  -d                Set debug mode
  -bg               Run ffmpeg in the background

USAGE
	exit -1;
}

sub parse_args {
	my $opt = {};
	while (my $arg=shift(@ARGV)) {
		if ($arg =~ /^-h$/) { usage(); }
		if ($arg =~ /^-d$/) { $MAIN::DEBUG=1; next; }
		if ($arg =~ /^-bg$/) { $opt->{bg} = 1; next; }
		if ($arg =~ /^-bw$/) { $opt->{bw} = shift @ARGV; next; }
		if ($arg =~ /^-o$/) { $opt->{save} = shift @ARGV; next; }
		if ($arg =~ /^-/) { usage("Unknown option: $arg"); }
		if ($arg =~ m|^http://([^/]+)/.*?([^/]+)$|) { ($opt->{url}, $opt->{dom}, $opt->{film}) = ($arg, $1, $2); next; }
		if ($arg =~ /^[0-9]+$/) { ($opt->{arg}, $opt->{dom},$opt->{film}) = ("ifilm.com", $arg); next; }
		usage("Unknown film id.  I need an iFilm or youtube URL");
	}

	usage("No film URL specified.") unless $opt->{film};
	$opt;
}

sub debug {
	return unless $MAIN::DEBUG;
	foreach my $msg (@_) { print STDERR "[$PROGNAME] $msg\n"; }
}

##################################################
# Utils
##################################################
sub title_embed {
	my ($opt, $url) = @_;

	debug("URL: $url");
	my $open = "$GET_URL $url";
	debug("GET: $open");
	open(FILM,"$open |") || usage("Couldn't get URL: $url\nMake sure [$GET_URL] is installed");
	
	# Find the film location
	my $dl;
	my $title;
	my $bw = $opt->{bw};
	my $embed;
	my $and_t;
	while(<FILM>) {
		$dl = $1 if /var\s+dl\s*=\s*"?(-?\d+)"?/;
		$bw = $1 if !$bw && /var\s+bw\s*=\s*"?(\d+)"?/;
		$embed = $1 if /<embed.*(http:[^>"]+)/i;
		$title = $1 if /span class="title">([^<]+)/;
		$title = $1 if /<title>(.*)<\/title>/;
		$and_t = $1 if /video_id=.*&t=([^&"]+)/;
	}
	close(FILM);
	($title,$embed,$dl,$bw,$and_t);
}

##################################################
# iFilm
##################################################
sub iFilm {
	my ($opt,$film) = @_;

	print STDERR "Fetching iFilm ID: $film\n";

	# Read the ifilm HTML
	my $url = $IFILM.$film;
	my ($title,$smil,$dl,$bw) = title_embed($opt, $url);
	$title = $title || $film;

	usage("Couldn't find 'dl' key in URL [$url]") unless $dl;
	usage("Couldn't find 'bw' key in URL [$url]") unless $bw;
	usage("Couldn't find 'embed' tag in URL [$url]") unless $smil;
	$smil =~ s/'\s+\+\s+dl\s+\+\s+'/$dl/g;
	$smil =~ s/'\s+\+\s+bw\s+\+\s+'/$bw/g;
	$smil =~ s/"$//;
	debug("SMIL: $smil");

	# Get the smil file
	my $open = "$GET_URL \Q$smil\E";
	open(SMIL,"$open |") || usage("Couldn't get SMIL: $smil");
	my $video;
	while (<SMIL>) {
		$video = $1 if /video src="(http[^"]+)"/;
	}
	usage("Couldn't find video source key in SMIL [$smil]") unless $video;
	close SMIL;

	debug("Found: $video");
	$title =~ s/[:\s]+/_/g;
	my $save = $opt->{save} || "$title.$POST";
	$save =~ s|/|_of_|g;
	print "Saving to: $save\n";
	debug("$GET_URL \Q$video\E > \Q$save\E\n");
	system("$GET_URL \Q$video\E > \Q$save\E");
}

##################################################
# youtube
##################################################
sub youtube {
	my ($opt, $film) = @_;
	print STDERR "Fetching youtube ID: $film\n";

	# Get name
	my ($save,undef,undef,undef,$and_t) = title_embed($opt, $opt->{url});
	fatal("No '&t=' code found for youtube [$opt->{url}]") unless $and_t;
	if ($opt->{save}) {
		$save = $opt->{save};
	} else {
		fatal("No title found for youtube [$opt->{url}]") unless $save;
		$save =~ s/youtube( - )?(.+)/$2/ig;
		print STDERR "Found film '$save'\n";
		$save =~ s/[:\s]+/_/g;
	}
	$save =~ s/(\.flv)?$/.flv/;
	$save =~ s|/|_of_|g;
	$save =~ s/[\*\?\'\"]//g;	# Get rid of troublesome characters.
	# ffmpeg gets confused easily

	# Get video
	#print "TITLE; $title -> $save\n";
	my $video = $YOUTUBE.$film."&t=$and_t";
	debug("Video: $video");
	print "Saving to: $save\n";
	system("$GET_URL \Q$video\E > \Q$save\E");

	# Attempt convert to .mov
	my $mov = $save;
	$mov =~ s/\.flv$/.mov/;
	print "Convert to: $mov\n";
	exec($FFMPEG,"-v",0,"-i",$save,$mov) unless fork;
	if ($opt->{bg}) {
		sleep 1;	# To let the intial ffmpeg output go through..
	} else {
		wait;
	}
}

##################################################
# Main code
##################################################
sub main {
	my $opt = parse_args();

	if ($opt->{dom} =~ /youtube/i) {
		$opt->{film} = "watch?v=$opt->{film}"
			if $opt->{url} =~ s|www.youtube.com/v/|www.youtube.com/watch?v=|;
		usage("Couldn't find youtube video (URL must contain 'v=[video_id]')")
			unless $opt->{film} =~ /v=([a-z0-9_-]+)/i;
		return youtube($opt, $1);
	}

	if ($opt->{dom} =~ /ifilm/i) {
		usage("Couldn't find iFilm video (URL must contain a number/film id)")
			unless $opt->{film} =~ /([0-9]+)/;
		return iFilm($opt, $1);
	}
}
main();
...although it's still not very streamlined...what I'd like is to get this clever javascript bookmark to work in the browser. Why doesn't that script work even with javascript enabled in the preferences?

Last edited by gogol; 2007-08-14 at 19:52.
 
Posts: 17 | Thanked: 0 times | Joined on Aug 2007
#5
Originally Posted by gogol View Post
...although it's still not very streamlined...what I'd like is to get this clever javascript bookmark to work in the browser. Why doesn't that script work even with javascript enabled in the preferences?
Have you tried an alternative browser, like minimo?
 
gogol's Avatar
Posts: 177 | Thanked: 57 times | Joined on Aug 2007 @ Washington State, USA
#6
Yeah I did try minimo but gave up rather quickly on it. I might have given up too soon, didn't look around too much...I put it off as something to try when I can afford to buy more memory.
 
gogol's Avatar
Posts: 177 | Thanked: 57 times | Joined on Aug 2007 @ Washington State, USA
#7
I hacked that script some more to get at what I wanted:
Now I can go around bookmarking interesting videos, then drop to the terminal and run the script to download them. It saves them to MyDocs/.videos folder so mplayer picks em right up. It also gives them their proper name. Screenshot below.

Code:
#!/usr/bin/perl
# Filename:	myfilm
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License/
# Description:	Download iFilm/youtube movies given a film ID.
# Converts youtube films from flash to .mov if ffmpeg is available.
  my $VERSION=  '1.02';
#
# Requires that ifilm/youtube's obfuscation doesn't change drastically.
#
# youtube: http://youtube.com/watch?v=[video_id]
#   video at:  http://youtube.com/get_video.php?l=165&video_id=[video_id]&t=[??]
#   Need to find &t=[c0de] in HTML
#
# ifilm:  http://www.ifilm.com/ifilmdetail/2667392
#   -> http://secure.ifilm.com/qt/portal/2667392_300.mov?e=1159050000&h=6c97297693c58d818e86aba0819abf10
#   Update:  Don't know how to look this up with iFilm's new Flash player
#
# Also see: http://javimoya.com/blog/youtube_en.php
##################################################
# CHANGELOG
##################################################
# 2007/8/16
# ----------------
# Haxxed for the 770 by gogol.  Somebody with 
# better perl skillz, please help
# NOTE: The bookmark parsing REQUIRES that myfilm.pl is in your PATH
#
# 1.02  2006/05/13
# ----------------
# + Updated for new youtube obfuscation (&t=[c0de])
#
# 1.01  2006/04/08
# ----------------
# + Released as myfilm
# + Now handles youtube as well as iFilm
#
# 1.00  2005/12/08
# ----------------
# + Released as diFilm
#
##################################################
#use strict;

##################################################
# Setup the variables
##################################################
my $PROGNAME = $0; $PROGNAME =~ s|.*/||;
my ($BASENAME,$PROGNAME) = ($0 =~ m|(.*)/(.+)|) ? ($1?$1:'/',$2) : ('.',$0);

# This will probably change..
my $IFILM = "http://www.ifilm.com/player/mac.jsp?ifilmId=";
my $YOUTUBE = "http://youtube.com/get_video.php?l=165&video_id=";

# How to fetch web files - pick one
# Actual Safari agent example:
# Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/416.12 (KHTML, like Gecko) Safari/416.13
my $GET = "GET -H 'user-agent: Mozilla/5.0'";
my $LYNX = "links -source";

my $GET_URL = $LYNX;

my $POST = "mov";

# For converting youtube to .mov
my $FFMPEG = "ffmpeg";

#default save dir for youtube
my $defdir = "/home/user/MyDocs/.videos"; #note no trailing slash

##################################################
# Usage
##################################################
sub fatal {
	foreach my $msg (@_) { print STDERR "[$PROGNAME] ERROR:  $msg\n"; }
	exit(-1);
}

sub usage {
	foreach my $msg (@_) { print STDERR "ERROR:  $msg\n"; }
	print STDERR <<USAGE;

Usage:\t$PROGNAME [-d] <ifilm URL, youtube URL or ifilm id>
  Download an ifilm movie
  -o <file>         Save to file
  -bw <56,200,300>	Force a bandwidth other than your iFilm settings
  -d                Set debug mode
  -bg               Run ffmpeg in the background
  -z		      Scan /home/user/.bookmarks/MyBookmarks.xml for youtube video links.
			It will then download any that don't already exist in ~/MyDocs/.videos
USAGE
	exit -1;
}

sub parse_args {
	my $opt = {};
	while (my $arg=shift(@ARGV)) {
		if ($arg =~ /^-z$/) {$opt->{bmfile} = 1; next;}
		if ($arg =~ /^-h$/) { usage(); }
		if ($arg =~ /^-d$/) { $MAIN::DEBUG=1; next; }
		if ($arg =~ /^-bg$/) { $opt->{bg} = 1; next; }
		if ($arg =~ /^-bw$/) { $opt->{bw} = shift @ARGV; next; }
		if ($arg =~ /^-o$/) { $opt->{save} = shift @ARGV; next; }
		if ($arg =~ /^-/) { usage("Unknown option: $arg"); }
		if ($arg =~ m|^http://([^/]+)/.*?([^/]+)$|) { ($opt->{url}, $opt->{dom}, $opt->{film}) = ($arg, $1, $2); next; }
		if ($arg =~ /^[0-9]+$/) { ($opt->{arg}, $opt->{dom},$opt->{film}) = ("ifilm.com", $arg); next; }
		usage("Unknown film id.  I need an iFilm or youtube URL");
	}

	usage("No film URL specified.") unless $opt->{film} || $opt->{bmfile};
	$opt;
}

sub debug {
	return unless $MAIN::DEBUG;
	foreach my $msg (@_) { print STDERR "[$PROGNAME] $msg\n"; }
}

##################################################
# Utils
##################################################
sub title_embed {
	my ($opt, $url) = @_;

	debug("URL: $url");
	my $open = "$GET_URL $url";
	debug("GET: $open");
	open(FILM,"$open |") || usage("Couldn't get URL: $url\nMake sure [$GET_URL] is installed");
	
	# Find the film location
	my $dl;
	my $title;
	my $bw = $opt->{bw};
	my $embed;
	my $and_t;
	while(<FILM>) {
		$dl = $1 if /var\s+dl\s*=\s*"?(-?\d+)"?/;
		$bw = $1 if !$bw && /var\s+bw\s*=\s*"?(\d+)"?/;
		$embed = $1 if /<embed.*(http:[^>"]+)/i;
		$title = $1 if /span class="title">([^<]+)/;
		$title = $1 if /<title>(.*)<\/title>/;
		$and_t = $1 if /video_id=.*&t=([^&"]+)/;
	}
	close(FILM);
	($title,$embed,$dl,$bw,$and_t);
}
###################################################
#Bookmark parse
###################################################
sub getbookmarks { 

my $BMARKS = "/home/user/.bookmarks/MyBookmarks.xml";
open BMARKS, "<$BMARKS" or die "Can't open bookmarks!: $!";
while (<BMARKS>) { chomp;
if ($_ =  m|(http://(?:www\.)?youtube\.com/watch\?v=[^"]+)|i){ push @youtube_vids, $1; }
}
print "Found ",($#youtube_vids + 1)," bookmarked videos:\n";
while ($ourcount < ($#youtube_vids+1) ) {
  print "$ourcount : $youtube_vids[$ourcount]\n";
  @args = ("myfilm.pl", $youtube_vids[$ourcount]);
  $ourcount++;
  system(@args) == 0 or die "failed: $?\n";
  }

}


##################################################
# iFilm
##################################################
sub iFilm {
	my ($opt,$film) = @_;

	print STDERR "Fetching iFilm ID: $film\n";

	# Read the ifilm HTML
	my $url = $IFILM.$film;
	my ($title,$smil,$dl,$bw) = title_embed($opt, $url);
	$title = $title || $film;

	usage("Couldn't find 'dl' key in URL [$url]") unless $dl;
	usage("Couldn't find 'bw' key in URL [$url]") unless $bw;
	usage("Couldn't find 'embed' tag in URL [$url]") unless $smil;
	$smil =~ s/'\s+\+\s+dl\s+\+\s+'/$dl/g;
	$smil =~ s/'\s+\+\s+bw\s+\+\s+'/$bw/g;
	$smil =~ s/"$//;
	debug("SMIL: $smil");

	# Get the smil file
	my $open = "$GET_URL \Q$smil\E";
	open(SMIL,"$open |") || usage("Couldn't get SMIL: $smil");
	my $video;
	while (<SMIL>) {
		$video = $1 if /video src="(http[^"]+)"/;
	}
	usage("Couldn't find video source key in SMIL [$smil]") unless $video;
	close SMIL;

	debug("Found: $video");
	$title =~ s/[:\s]+/_/g;
	my $save = $opt->{save} || "$title.$POST";
	$save =~ s|/|_of_|g;
	print "Saving to: $save\n";
	debug("$GET_URL \Q$video\E > \Q$save\E\n");
	system("$GET_URL \Q$video\E > \Q$save\E");
}

##################################################
# youtube
##################################################
sub youtube {
	my ($opt, $film) = @_;
	print STDERR "Fetching youtube ID: $film\n";

	# Get name
	my ($save,undef,undef,undef,$and_t) = title_embed($opt, $opt->{url});
	fatal("No '&t=' code found for youtube [$opt->{url}]") unless $and_t;
	if ($opt->{save}) {
		$save = $opt->{save};
	} else {
		fatal("No title found for youtube [$opt->{url}]") unless $save;
		$save =~ s/youtube( - )?(.+)/$2/ig;
		print STDERR "Found film '$save'\n";
		$save =~ s/[:\s]+/_/g;
	}
	$save =~ s/(\.flv)?$/.flv/;
	$save =~ s|/|_of_|g;
	$save =~ s/[\*\?\'\"]//g;	# Get rid of troublesome characters.
	# ffmpeg gets confused easily

	# Get video
	#print "TITLE; $title -> $save\n";
	my $video = $YOUTUBE.$film."&t=$and_t";
	debug("Video: $video");
	$filename = join "/", $defdir, $save;
	print "$filename is filename\n";
	if (-e $filename) { print "File $defdir/$save already exists, skipping.\n"; return; }
	print "Saving to: $defdir/$save\n";
       system("$GET_URL \Q$video\E > $filename");
	


	# Attempt convert to .mov
	#my $mov = $save;
	#$mov =~ s/\.flv$/.mov/;
	#print "Convert to: $mov\n";
	#exec($FFMPEG,"-v",0,"-i",$save,$mov) unless fork;
	#if ($opt->{bg}) {
	#	sleep 1;	# To let the intial ffmpeg output go through..
	#} else {
#		wait;
}

##################################################
# Main code
##################################################
sub main {
	my $opt = parse_args();

	if ($opt->{dom} =~ /youtube/i) {
		$opt->{film} = "watch?v=$opt->{film}"
			if $opt->{url} =~ s|www.youtube.com/v/|www.youtube.com/watch?v=|;
		usage("Couldn't find youtube video (URL must contain 'v=[video_id]')")
			unless $opt->{film} =~ /v=([a-z0-9_-]+)/i;
		return youtube($opt, $1);
	}

	if ($opt->{dom} =~ /ifilm/i) {
		usage("Couldn't find iFilm video (URL must contain a number/film id)")
			unless $opt->{film} =~ /([0-9]+)/;
		return iFilm($opt, $1);
	}
	if (defined $opt->{bmfile}) {&getbookmarks;} 
	

}
main();
This requires Links and Getopt/Std.pm. (Stick it in /usr/share/perl/5.8.3/Getopt/ next to Long.pm)



Now if somebody that has more than a few minutes experience with perl wanted to give a hand, this could be pretty freekin' cool. Thanks to #regex on freenode or I _never_ would have gotten line 155
 
gogol's Avatar
Posts: 177 | Thanked: 57 times | Joined on Aug 2007 @ Washington State, USA
#8
Another update.
Uses wget now, much faster.
Updated the handling of odd characters to fix stuff I was coming across.
-d debug switch actually works now.
removed all the iFilm and ffmpeg stuff.
You only need wget to use this script! perl is installed, no other dependencies..
Again, the basic premise is:
1 go to youtube and browse.
2 when you find a movie you are interested in, right-click and bookmark the link.
3 once you have all you want, drop to the shell and run the script.
4 open up mplayer on the train/bus/right now and watch!

Code:
#!/usr/bin/perl
# Filename:	myfilm
# Original Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License/
# Description:	For use with the Nokia 770.  The basic idea is to 
#			browse youtube and bookmark videos you are interested in.
#			Drop to osso-xterm and run the script to batch download!
my $VERSION=  '1.02';
#
# Requires that youtube's obfuscation doesn't change drastically.
#
# youtube: http://youtube.com/watch?v=[video_id]
#   video at:  http://youtube.com/get_video.php?l=165&video_id=[video_id]&t=[??]
#   Need to find &t=[c0de] in HTML
#
#
# Also see: http://javimoya.com/blog/youtube_en.php
##################################################
# CHANGELOG
##################################################
# 2007/8/16
# ----------------
# Haxxed for the 770 by gogol.  Somebody with 
# better perl skillz should really clean it up!
#
# 1.02  2006/05/13
# ----------------
# + Updated for new youtube obfuscation (&t=[c0de])
#
# 1.01  2006/04/08
# ----------------
# + Released as myfilm
# + Now handles youtube as well as iFilm
#
# 1.00  2005/12/08
# ----------------
# + Released as diFilm
#
##################################################
#use strict;
##################################################
# Setup the variables
##################################################
my $PROGNAME = $0; $PROGNAME =~ s|.*/||;
my ($BASENAME,$PROGNAME) = ($0 =~ m|(.*)/(.+)|) ? ($1?$1:'/',$2) : ('.',$0);

# This will probably change..
my $YOUTUBE = "http://youtube.com/get_video.php?l=165&video_id=";

# How to fetch web files - pick one
# Actual Safari agent example:
# Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/416.12 (KHTML, like Gecko) Safari/416.13
my $GET = "GET --header 'user agent: Mozilla/4.0 (compatible; MSIE 6.0; ; Linux armv5tejl; U) Opera 8.02 [en_US] Maemo browser 0.4.34 N770/SU-18' ";
my $wgetter = "wget -O -";
my $GET_URL = $wgetter;

#default save dir for youtube
my $defdir = "/home/user/MyDocs/.videos"; #note no trailing slash
#bookmarks are where?
my $BMARKS = "/home/user/.bookmarks/MyBookmarks.xml";

##################################################
# Usage
##################################################
sub fatal {
	foreach my $msg (@_) { print STDERR "[$PROGNAME] ERROR:  $msg\n"; }
	exit(-1);
}

sub usage {
	foreach my $msg (@_) { print STDERR "ERROR:  $msg\n"; }
	print STDERR <<USAGE;

Usage:\t$PROGNAME [-d] <youtube url>
  Download an ifilm movie
  -o <file>         Save to file
  -bw <56,200,300>	Force a bandwidth other than your iFilm settings
  -d                Set debug mode
  -s		      Scan /home/user/.bookmarks/MyBookmarks.xml for youtube video links.
			It will then download any that don't already exist in ~/MyDocs/.videos
USAGE
	exit -1;
}

sub parse_args {
	my $opt = {};
	while (my $arg=shift(@ARGV)) {
		if ($arg =~ /^-s$/) {$opt->{bmfile} = 1; next;}
		if ($arg =~ /^-h$/) { usage(); }
		if ($arg =~ /^-d$/) { $MAIN::DEBUG=3; next; }
		if ($arg =~ /^-bg$/) { $opt->{bg} = 1; next; }
		if ($arg =~ /^-bw$/) { $opt->{bw} = shift @ARGV; next; }
		if ($arg =~ /^-o$/) { $opt->{save} = shift @ARGV; next; }
		if ($arg =~ /^-/) { usage("Unknown option: $arg"); }
		if ($arg =~ m|^http://([^/]+)/.*?([^/]+)$|) { ($opt->{url}, $opt->{dom}, $opt->{film}) = ($arg, $1, $2); next; }
		usage("Unknown film id.  I need an iFilm or youtube URL");
	}

	usage("No film URL specified.") unless $opt->{film} || $opt->{bmfile};
	$opt;
}

sub debug {
	return unless $MAIN::DEBUG;
	foreach my $msg (@_) { print STDERR "[$PROGNAME] $msg\n"; }
}

##################################################
# Deobfuscation
##################################################
sub title_embed {
	my ($opt, $url) = @_;

	debug("URL: $url");
	my $open = "$GET_URL $url";
	debug("GET: $open");
	open(FILM,"$open |") || usage("Couldn't get URL: $url\nMake sure [$GET_URL] is installed");
	
	# Find the film location
	my $dl;
	my $title;
	my $bw = $opt->{bw};
	my $embed;
	my $and_t;
	while(<FILM>) {
		$dl = $1 if /var\s+dl\s*=\s*"?(-?\d+)"?/;
		$bw = $1 if !$bw && /var\s+bw\s*=\s*"?(\d+)"?/;
		$embed = $1 if /<embed.*(http:[^>"]+)/i;
		$title = $1 if /span class="title">([^<]+)/;
		$title = $1 if /<title>(.*)<\/title>/;
		$and_t = $1 if /video_id=.*&t=([^&"]+)/;
	}
	close(FILM);
	($title,$embed,$dl,$bw,$and_t);
}


#################################################
#Bookmark Parsing
#################################################
sub getbookmarks { 

	open BMARKS, "<$BMARKS" or die "Can't open bookmarks for reading: $!";
	while (<BMARKS>) { chomp;
	     if ($_ =  m|(http://(?:www\.)?youtube\.com/watch\?v=[^"]+)|i){ push @youtube_vids, $1; }
	}
	
	print "Found ",($#youtube_vids + 1)," bookmarked videos:\n";
	
	while ($ourcount < ($#youtube_vids+1) ) {
  		print "$ourcount : $youtube_vids[$ourcount]\n";
		if ($MAIN::DEBUG == "3") {
  			@args = ($PROGNAME, "-d", $youtube_vids[$ourcount]); 
		}
		else { @args = ($PROGNAME, $youtube_vids[$ourcount]); }
		print "$args[0] $args[1] $args[2]\n";
  		$ourcount++;
  		system(@args) == 0 or die "failed: $!\n";
  	}

}


##################################################
# youtube
##################################################
sub youtube {
	my ($opt, $film) = @_;
	print STDERR "Fetching youtube ID: $film\n";

	# Get name
	my ($save,undef,undef,undef,$and_t) = title_embed($opt, $opt->{url});
	fatal("No '&t=' code found for youtube [$opt->{url}]") unless $and_t;
	if ($opt->{save}) {
		$save = $opt->{save};
	} else {
		fatal("No title found for youtube [$opt->{url}]") unless $save;
		$save =~ s/youtube( - )?(.+)/$2/ig;
		print STDERR "Found film '$save'\n";
		$save =~ s/[:\s]+/_/g;
	}
	$save =~ s/(\.flv)?$/.flv/;
	$save =~ s|/|_of_|g;
	$save =~ s/[\*\?\'\"\&\,]//g;	# Get rid of troublesome characters..
	$save =~ s/\(//g; $save =~ s/\)//g;    #..including parentheses.
	debug("save: $save");
	# Get video
	my $video = $YOUTUBE.$film."&t=$and_t";
	debug("Video: $video");
	$filename = join "/", $defdir, $save;
	debug("filename: $filename");
	if (-e $filename) { 
		print "File $defdir/$save already exists, skipping.\n";

		#WOW the bookmarks xml SUCKS.  Eventually we'll delete old links...
		#open BMARKS, "<$BMARKS"; @all = <BMARKS>;
		#foreach (@all) {
		#if(m|$film|i) { print; $_ = ""; print "Erased bookmark to already downloaded file.\n";}
		#}
		#open BMARKS, ">$BMARKS"; 
		#foreach(@all) {
		#		#print;
		#			}
		#return;

}
	print "Saving to: $defdir/$save\n";
       system("$GET_URL \Q$video\E > $filename");

}

##################################################
# Main code
##################################################
sub main {
	my $opt = parse_args();

	if ($opt->{dom} =~ /youtube/i) {
		$opt->{film} = "watch?v=$opt->{film}"
			if $opt->{url} =~ s|www.youtube.com/v/|www.youtube.com/watch?v=|;
		usage("Couldn't find youtube video (URL must contain 'v=[video_id]')")
			unless $opt->{film} =~ /v=([a-z0-9_-]+)/i;
		return youtube($opt, $1);
	}

	if (defined $opt->{bmfile}) {&getbookmarks;} 
	

}
main();

Last edited by gogol; 2007-08-18 at 05:27.
 
Posts: 17 | Thanked: 0 times | Joined on Aug 2007
#9
Wow pretty clever solution there David. Too bad it's reliant on Opera's bookmarks

Just kidding.
 
Posts: 2 | Thanked: 0 times | Joined on Aug 2007 @ Arlington, TX
#10
gogol, this is a very intriguing script! Compared to uktube, it looks simpler and cleaner (i.e., much more friendly in terms of memory and dependencies, and therefore potential crashes) and there is no need to switch away from YouTube every single time you want to select a video or [later] viewing.

My problem is that I'm very new to the 770 and even newer to Linux, so I have to ask some stupid questions. I have installed osso-xterm and wget, but now I'm not sure how to get the script into the 770! Do I just paste it into a text editor and save to /usr/bin? If so, do I need to use a particular type of editor?

A few more [possibly also stupid] questions and/or assumptions on my part:

1. MPlayer seems to find videos on the mmc card without any trouble, so if I want the videos to be downloaded directly to the rs-mmc card, should I edit the script accordingly (how?), or should I mount MyDocs/.videos to a folder on the card (how?)

2. Just to confirm, there is no longer any need to install bash, links, or getopt/std.pm, correct?

3. It looks like you changed -z to -s.

4. This is really stupid, I'm sure: What is chmod a+x

5. You mentioned the possibility of calling the script from a shortcut in the menu. Still considering that? (or how could I do it myself?)

6. You mentioned "optionally deleting the file afterwards," but I assume all cleanup is manual at this point? (e.g. File Manager for deleting videos and Bookmark Manager for deleting bookmarks).

7. Can all of these YouTube bookmarks be kept in their own bookmark folder?

Thanks!
 
Reply


 
Forum Jump


All times are GMT. The time now is 08:54.