Never been to TextSnippets 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 (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Scrape YouTube thumbnails

// fun with mechanize
// more @ http://tramchase.com/scrape-myspace-youtube-torrents-for-fun-and-profit

agent = WWW::Mechanize.new
url = "http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed" # all time
page = agent.get(url)
# parse again w/ Hpcricot for some XML convenience
doc = Hpricot.parse(page.body)
# pp (doc/:entry) # like "search"; cool division overload
images = (doc/'media:thumbnail') # use strings instead of symbols for namespaces
FileUtils.mkdir_p 'youtube-images' # make the images dir
urls = images.map { |i| i[:url] }
urls.each_with_index do |file,index|
  puts "Saving image #{file}"
  agent.get(file).save_as("youtube-images/vid#{index}_#{File.basename file}")
end

Ruby: Conform Sequence Files to Final Cut Pro XML (BETA)


#! /usr/bin/env ruby

# Tested with Apple XML Interchange Format Vr. 4
# The program infers the location of the seq. files by using the offline MOV's name.
# 1. It first looks for subfolders in the location of the MOV file. If a subfolder is found to have the same name as
# the file itself, then the whole folder is treated as a mirror of itself.
# 2. If no matching folders are found, it will use the first non-matching folder it comes across, and then assume
# that the files contained within it are prefixed with a name similar to its own.
# 3. Offline MOVs must run at the same framerate as your editing timebase in FCP! No putting a 24fps MOV into a 30fps timeline!

require 'rubygems'
require 'hpricot'

class Range

def length
  to_a.size
end

end

class Fixnum

def pad(n=8)
  x = to_s
  pad = n - x.length
  x.insert(0,"#{'0'*pad}")
  x
end

end

class Editor

attr_reader :edl

def initialize(file)

  @masters = {}
  @edits = []
  @edl = []
  @doc = Hpricot(open(file))

  find_masters()
  breakdown()

end

def find_masters

@doc.search("//file/pathurl").each do |elem| 
  
  base = File.basename(elem.inner_html).gsub!(/\....$/,'')
  path = abspath(elem.inner_html)

  folders = `ls #{path}`.split("\n").delete_if { |f| f =~ /\..../ }

  if folders.index(base).nil?
    @masters[elem.parent.attributes['id']] = path + "/" + folders.first + "/" + base + "."
  else
    @masters[elem.parent.attributes['id']] = path + "/" + base + "/"
  end

end

end

def breakdown

  @edits = @doc.search("//clipitem/file").each do |elem| 

    cut = []
    file_id = elem.attributes['id']
    seq_location = @masters[file_id]
    places = getpad(seq_location)

    cutin = (elem.parent/"in").inner_html.to_i.pad(places)
    cutout = (elem.parent/"out").inner_html.to_i.pad(places)

    cut.push(Range.new(cutin.succ!,cutout)) # Compensate for off-by-one count in IN-point from XML
    cut.push(seq_location)

    @edl.push(cut)

  end

end

def getpad(path)

  if path =~ /\.$/
    f = `ls #{path}*`.split("\n")[1]
  else
    f = `ls #{path}`.split("\n")[1]
  end
  
  return f.match(/(.+\.)?(.+)\..../).to_a.last.length

end

def abspath(path)

  path.gsub!(/file:\/\/localhost/,'')
  path.gsub!(/%20/,'\ ')

  return File.dirname(path)

end

def print_edl
  @edl.each_with_index do |cut,i|
    puts i.pad(4).succ + "|" + cut[0].first + " -- " + cut[0].last + " ++ " + cut[1]
  end
end

end

class Manager

def initialize

  @subroll = '00'
  @maxslots = 0
  @current_directory = ""
  @current_frame = "00000001"

  refresh_current

end

def refresh_current
  @subroll.succ!
  @maxslots = FRAMES_PER_ROLL
  @current_directory = "#{TARGET}/#{BIGROLL}_#{@subroll}"
  `mkdir -p #{@current_directory}`
  puts "NEW ROLL! #{@current_directory}"
  sleep 5
end

def assemble(cut)

  seq, source = cut[0], cut[1]

  if seq.length+1 > @maxslots
    refresh_current()
  end

  puts "Copying files to temporary folder..."

  seq.each do |frame|
    if source =~ /\/$/
      `cp #{source}/*.#{frame}.dpx #{TEMP}/`
    else
      `cp #{source}#{frame}.dpx #{TEMP}/`
    end
  end

  Dir.chdir(TEMP)
  
  list = Dir.entries(TEMP).delete_if { |x| x =~ /^\./ }
  
  list.map! { |f| File.rename(f,f + "x") ; f + "x" }

  puts "Moving files into roll #{@subroll}"

  list.each do |f|

    `mv -i #{f} #{@current_directory}/`

    Dir.chdir(@current_directory)
    
    `mv -i #{f} #{BIGROLL}_#{@subroll}.#{@current_frame}.dpx`

    puts "Moved #{f}"

    Dir.chdir(TEMP)
    
    @current_frame.succ!
    @maxslots -= 1
    
  end

end

end

## SETUP ##

FRAMES_PER_ROLL = 80
BIGROLL = '03'
TEMP = "/Volumes/Offline/Cache/Rubycut"
TARGET = "/Volumes/Offline/REEL3RC2"
FCP_XML = "/Users/anak/Desktop/edl.xml"

## MAIN ##

editor = Editor.new(FCP_XML)

editor.print_edl

puts "Press ENTER to continue..."

gets

manager = Manager.new

editor.edl.each_with_index do |cut,i|
  
  puts "Assembling #{(i+1).pad(4)} | #{cut[0]} > #{cut[1]}"
  
  manager.assemble(cut)
  
end

puts "CONFORMED: #{FCP_XML}"

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed