Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

I can't be bothered going downstairs to watch the daily show

scraper.rb - scrapes metadata from thedailyshow.com.
Just run it - creates videos/index.yml
#!/usr/bin/ruby

require 'rubygems'
require 'leecher'
require 'open-uri'
require 'hpricot'

class Scraper
	SEARCH_PAGE="http://www.thedailyshow.com/tds_files/includes/search/search_results.jhtml"
	def scrape_day(site, date, force=false)
		return if site.skip.include?(date) and not force
		url = sprintf("#{SEARCH_PAGE}?searchterm=%02d-%02d-%04d",date.month,date.day,date.year)
		puts "Fetching #{url}" if $DEBUG

		data = open(url) {|f| f.read }
		h = Hpricot(data)

		# can't use a real xpath - they all use an _id_ istead of a class
		results = (h/"div").find_all {|x| x['id'] == "videoListItem_1" }
		results.each {|result|
			url = result.at("a[1]")['href']
			vid = if url =~ /videoId=(\d+)/
				$1.to_i
			else
				raise "Failed to parse link #{url}"
			end

			title = ((result/"a")[1]/"text()").to_s
			date = Date.parse((result/"a[@onclick][1]/text()").to_s)

			descr = result/"div.video_description"
			description = (descr/"div[1]/text()").to_s
			tags = (descr/".tags/a/text()").map {|x| unescape(x.to_s) }
			
			vid = Video.new(site, vid, date, unescape(title), unescape(description), tags)
			puts vid
		}
	
		results.length
	end

	def unescape(t)
		t.gsub(/&([^;]{1,5});/) {|ent|
			case $1
				when /^#(\d+)$/
					[$1.to_i].pack('C')
				when /^#x([0-9a-zA-Z]+)$/
					[$1].pack('H2')
				when 'amp'
					'&'
				when 'gt'
					'>'
				when 'quot'
					'"'
				when 'apos'
					"'"
				when 'lt'
					'<'
				else
					$stderr.puts "Unknown entity #{$1.inspect}"
					'?'
			end
		}
	end
end

if __FILE__ == $0
	Site.load
	tds = Site.sites.find {|s| s.short_name == "tds" }
	tds ||= Site.new("The Daily Show","tds")

	day = Date.today
	start = Date.new(1999)

	counter=0

	s = Scraper.new
	while day >= start
		results = s.scrape_day(tds, day)
		puts "#{results} results for #{day}" if results # else skipped

		# Mark days as done once we've scraped them a month after air
		if (Date.today - day) > 30 and not results.nil?
			tds.skip!(day)
		end

		day -= 1
		unless results.nil? or results.zero?
			Site.save if (counter += 1)%10 == 0
		end
	end
	Site.save
end


leecher.rb - downloads/searches/plays videos
./leecher.rb [download/list/play] [searchterm ... ]
Search terms can be
date: 2007 or 2007-10 or 2007-10-01
tag: interview
id: 31723
already downloaded?: downloaded or !downloaded
#!/usr/bin/ruby

MEDIA_PLAYER = %w{mplayer -fs}

require 'rubygems'
require 'open-uri'
require 'rexml/document'
require 'rexml/xpath'
require 'fileutils'
require 'yaml'
require 'rio'
require 'set'

class Site
	class << self
		attr_reader :base
		attr_reader :alternates
		attr_reader :sites
		
		def init
			unless self.base or self.sites
				@base = "./videos"
				@alternates = []
				@sites = []
			end
		end

		def load(stream=nil)
			if stream
				stuff = YAML::load(stream)
				@base = stuff['base'] || "./videos"
				@sites = stuff['sites'] || []
				@alternates = stuff['alternates'] || []
			else
				init
				begin
					File.open(File.join(base,'index.yml')) {|f|
						load(f)
					}
				rescue Errno::ENOENT
					$stderr.puts "Warning, no database found, starting a new one"
				end
			end
		end
		
		def save(stream=nil)
			if stream
				YAML::dump({'base' => base, 'sites' => sites, 'alternates' => 'alternates'},stream)
			else
				File.open(File.join(base,'index.yml_'),'w') {|f|
					save(f)
				}
				FileUtils.mv(File.join(base,'index.yml_'),File.join(base,'index.yml'))
			end
		end

		def each(&block)
			sites.each(&block)
		end
	end

	def initialize(name, short_name=name)
		Site.init
		@videos = {}
		@name, @short_name = name, short_name
		@skip = Set.new
		Site.sites << self
	end

	attr_reader :videos
	attr_reader :name
	attr_reader :short_name
	attr_reader :skip

	def directory
		File.join(Site.base, short_name)
	end
	def directory_alternates
		Site.alternates.map {|d| File.join(d, short_name) }
	end

	def ensure_dir_exists!
		FileUtils.mkpath(directory)
	end

	def <<(vid)
		self.videos[vid.id] = vid
	end

	def skip!(date)
		self.skip << date
	end

	def [](id)
		self.videos[id]
	end

	def to_s
		name
	end

	def each 
		self.videos.each {|k,v| yield v }	
	end
end

class Video
	attr_reader :tags
	attr_reader :site
	attr_reader :id
	attr_reader :date
	attr_reader :title
	attr_reader :description

	def initialize(site, id, date=nil, title = nil, description=nil, tags=[]) 
 		@site = site
		@id = id
		@title = title
		@tags = tags
		@date = date
		@description = description

		site << self
	end

	def filename
		site.directory_alternates.map{|x| 
			File.join(x,"#{id}.flv")
		}.find {|f| 
			File.exists?(f) 
		} || File.join(site.directory, "#{id}.flv")
	end

	def downloaded?
		File.exists?(filename)
	end

	def download
		download! unless downloaded?
	end

	SHARED_DATA = "http://www.comedycentral.com/sitewide/video_player/shared/data"
	def download!
		site.ensure_dir_exists!
		url = download_url()
		begin
			rio(url) > rio(filename)
			File.size(filename)
		rescue Exception => x
			begin
				File.delete(filename)
			rescue Exception
			end
			raise x
		end
	end

	def to_s 
		sprintf("[%1s %7d - %s - %s - %20s]",(downloaded?? 'D' : ' '), id, date, site, title) 
	end

	def download_url
		manifest = open("#{SHARED_DATA}/flv_xml_gen.jhtml?ml_video=#{id}&hiLoPref=hi") {|f| f.read }
		doc = REXML::Document.new(manifest)
		REXML::XPath.first(doc, "/package/video/item/src/text()").to_s
	end
end

class Filter
	class << self
		def method_missing(sym,*args,&block)
			if sym.to_s =~ /^by/
				new.send(sym,*args,&block)
			else
				super
			end
		end
	end

	def initialize(parent=nil,&block)
		@parent = parent
		@test = block
	end
	
	def [](video)
		case video
			when Video
				video if (@test.nil? or @test[video]) and (@parent.nil? or @parent[video])
			when Site
				if block_given?
					video.each {|v| yield v if self[v] }
				else
					video.find_all {|v| self[v] }
				end
			else
				raise ArgumentError
		end
	end

	def each(&block)
		Site.each {|show| self.send(:[], show, &block) }
	end

	def filter(&block)
		Filter.new(self,&block)
	end

	def by_downloaded(dl=true)
		filter {|v| v.downloaded? == !!dl }
	end

	def by_date(y, m=nil, d=nil)
		if m.nil? and d.nil?
			filter {|v| v.date and v.date.year == y }
		elsif d.nil?
			filter {|v| v.date and v.date.year == y and v.date.month == m }
		else
			filter {|v| v.date and v.date.year == y and v.date.month == m and v.date.day == d }
		end
	end

	def by_id(vid)
		filter {|v| v.id == vid }
	end

	def by_tag(tag)
		filter {|v| v.tags.include? tag }
	end

	def by_text(text) 
		filter {|v| 
			v.title && v.title.downcase.include?(text.downcase) or 
			v.description && v.description.downcase.include?(text.downcase) or
			v.tags.include? text
		}
	end

	def by(arg)
		arg = arg.to_s
		case arg
			when 'downloaded'
				by_downloaded(true)
			when '!downloaded'
				by_downloaded(false)
			when /^(\d{4})$/
				by_date($1.to_i)
			when /^(\d{4})-(\d{1,2})$/
				by_date($1.to_i, $2.to_i)
			when /^(\d{4})-(\d{1,2})-(\d{1,2})$/
				by_date($1.to_i, $2.to_i, $3.to_i)
			when /^\d{5,}$/
				by_id(arg.to_i)
			else
				by_text(arg)
		end
	end
end

if __FILE__ == $0
	command = ARGV.shift or raise "Usage: leecher <download/list> [filters...]"
	action = case command.downcase
		when 'download'
			proc {|v| 
				puts "Fetching #{v}" unless v.downloaded?
				begin
					v.download
				rescue OpenURI::HTTPError => x
					$stderr.puts "Download failed, skipping: #{x}"
				rescue Errno::ENOENT => xm
					$stderr.puts "Download failed, skipping: #{x}"
				end
			}
		when 'list'
			proc {|v|
				puts "#{v} #{v.tags.join(', ')}"
			}
		when 'play'
			proc {|v|
				puts v
				system(*MEDIA_PLAYER, v.filename)
			}
		else
			raise "Unknown command #{command}"
	end

	filter = ARGV.inject(Filter.new) {|f, arg| f.by(arg)}

	Site.load
	filter.each(&action)
end


Convert a video file to an audio file (.mp4 to .mp3)

Convert an mp4 file to avi, then to mp3 (including mixing the stereo down to mono). This code was executed from the command-line on Ubuntu 7.04.

mencoder video.mp4 -ovc lavc -vf scale=123:100 -oac lavc -o video.avi
ffmpeg -i video.avi -ac 1 audio1.mp3 

Bash - 3gpToMpeg

mencoder video.3gp -o video.mpg -oac pcm -ovc lavc -lavcopts vcodec=mjpeg:mbd=1:vbitrate=1800

Java - JMF Simple Filter

import java.awt.Dimension;

import javax.media.Buffer;
import javax.media.Effect;
import javax.media.Format;
import javax.media.ResourceUnavailableException;
import javax.media.format.RGBFormat;

public class SimpleFilter implements Effect
{
	protected Format inputFormat = null;
	protected Format outputFormat = null;
	
	protected Format[] inputFormats = null;
	protected Format[] outputFormats = null;
	
	public AngelMotionCodec()
	{
		inputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
		outputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
	}
	
	/****** Codec ******/
	public Format[] getSupportedInputFormats()
	{
		return inputFormats;
	}

	public Format[] getSupportedOutputFormats(Format input)
	{
		if(input != null)
		{
			if(matches(input, inputFormats) != null)
				return new Format[]{ outputFormats[0].intersects(input) };
			else
				return new Format[0];
		}
		
		return outputFormats;
	}

	public int process(Buffer input, Buffer output)
	{
		// Swap tra input & output
		Object tmp = input.getData();
		
		input.setData(output.getData());
		output.setData(tmp);
		
		return BUFFER_PROCESSED_OK;
	}

	public Format setInputFormat(Format input)
	{
		inputFormat = input;
		
		return input;
	}

	public Format setOutputFormat(Format output)
	{
		if(output != null || matches(output, outputFormats) != null)
		{
			RGBFormat inRGB = (RGBFormat) output;
			
			Dimension size = inRGB.getSize();
			int maxDataLength = inRGB.getMaxDataLength();
			int lineStride = inRGB.getLineStride();
			int flipped = inRGB.getFlipped();
			
			if(size == null)
				return null;
			
			if(maxDataLength < size.width*size.height*3)
				maxDataLength = size.width*size.height*3;
			
			if(lineStride < size.width*3)
				lineStride = size.width*3;
			
			if(flipped != Format.FALSE)
				flipped = Format.FALSE;
			
			outputFormat = outputFormats[0].intersects(new RGBFormat(size, maxDataLength, inRGB.getDataType(), inRGB.getFrameRate(), inRGB.getBitsPerPixel(), inRGB.getRedMask(), inRGB.getGreenMask(), inRGB.getBlueMask(), inRGB.getPixelStride(), lineStride, flipped, inRGB.getEndian()));
			
			return outputFormat;
		}
		
		return null;
	}
	/****** Codec ******/

	/****** PlugIn ******/
	public void close()
	{

	}

	public String getName()
	{
		return "Simple-Filter";
	}

	public void open() throws ResourceUnavailableException
	{

	}

	public void reset()
	{

	}
	/****** PlugIn ******/

	/****** Controls ******/
	public Object getControl(String controlType)
	{
		return null;
	}

	public Object[] getControls()
	{
		return null;
	}
	/****** Controls ******/
	
	/****** Utility ******/
	private Format matches(Format in, Format[] out)
	{
		if(in != null && out != null)
		{
			for(int i=0, cnt=out.length; i<cnt; i++)
			{
				if(in.matches(out[i]))
					return out[i];
			}
		}
		
		return null;
	}
	/****** Utility ******/
}

Java - Example Very Simple Player (JMF)

// Main Class

package org.jmf.example;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class ExampleJMF
{
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true); 
		JDialog.setDefaultLookAndFeelDecorated(true);
		
		try
		{
			UIManager.setLookAndFeel(new MetalLookAndFeel());
		}
		catch(UnsupportedLookAndFeelException e)
		{
			e.printStackTrace();
		}
		
		new exampleFrame();
	}
}


// Frame Class

package org.jmf.example;

import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class exampleFrame extends JFrame
{
	private static final long serialVersionUID = 1L;
	
	public exampleFrame()
	{
		super("JMF - Example...");
		
		setSize(400, 300);
		setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight())/2);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				System.exit(0);
			}
		});
		
		setContentPane(new examplePanel());
		setVisible(true);
	}
}


// Panel Class

package org.jmf.example;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.JPanel;

public class examplePanel extends JPanel implements ActionListener, ControllerListener
{
	private static final long serialVersionUID = 1L;
	
	private Component visualComponent;
	private Player player;
	
	public examplePanel()
	{
		try
		{
			player = Manager.createPlayer(new URL("file:///tmp/a.mpg"));
			player.addControllerListener(this);
			
			player.start();
		}
		catch(NoPlayerException e)
		{
			e.printStackTrace();
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
	}

	public void actionPerformed(ActionEvent e)
	{

	}

	public void controllerUpdate(ControllerEvent c)
	{
		if(player == null)
			return;
		
		if(c instanceof RealizeCompleteEvent)
		{
			if((visualComponent = player.getVisualComponent()) != null)
				add(visualComponent);
		}
	}
}

Load a page after video finishes playing

You can do this using Windows Media Player, adding javascript to the page to handle the playStateChange event to detect when the video finishes playing. (Should work with any video type that Windows Media Player supports.)

http://www.mioplanet.com/rsc/embed_mediaplayer.htm
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmplay10/mmp_sdk/playerplaystatechange.asp

<SCRIPT LANGUAGE="JScript"  FOR="Player" EVENT="playStateChange(NewState)">

// Test for the player current state, display a message for each.
switch (NewState){
    case 1:
        window.href="xxx.htm";
        break;
}
</SCRIPT>

Mencoder settings

From Real Media to QuickTime compatible


mencoder ~/Desktop/wpa90_220k.rm -o ~/Desktop/wpa90_23.avi -ovc lavc -oac lavc -ss 00:41:10 -endpos 00:01:20


mencoder ~/Desktop/wpa90_23.avi -o ~/Desktop/wpa90_232.avi -ovc xvid -xvidencopts bitrate=400 -oac lavc -lavcopts acodec=mp3



« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS