Permalink
Browse files

Add support to generate preview images via ffmpeg, stuff off in settings

folder
  • Loading branch information...
paulcbetts committed Jun 3, 2008
1 parent 7003c4c commit caa6297861f0e4f1a268a05bd330ef8b5376fa1c
Showing with 68 additions and 13 deletions.
  1. +25 −5 lib/controllers.rb
  2. +28 −5 lib/engine.rb
  3. +9 −3 lib/platform.rb
  4. +6 −0 lib/state.rb
View
@@ -29,6 +29,7 @@
require 'ramaze'
require 'builder'
require 'builder/xmlmarkup'
+require 'fileutils'
# Yikes
require 'config'
@@ -94,16 +95,15 @@ def url_base
end
end
-class FilesController < Ramaze::Controller
- map '/files'
-
+module StaticFileMixin
def index(*paths)
do_send_file File.join(paths)
end
-private
+protected
+
def do_send_file(subpath)
- target = Yikes.instance.state.target
+ target = get_file_path
p = Pathname.new(File.join(target, subpath))
begin
return nil unless p.realpath.to_s.index(target) == 0
@@ -114,3 +114,23 @@ def do_send_file(subpath)
send_file(p.to_s, 'video/mp4')
end
end
+
+class FilesController < Ramaze::Controller
+ include StaticFileMixin
+ map '/files'
+
+protected
+ def get_file_path
+ Yikes.instance.state.target
+ end
+end
+
+class PreviewController < Ramaze::Controller
+ include StaticFileMixin
+ map '/preview'
+
+protected
+ def get_file_path
+ Platform.screenshot_dir
+ end
+end
View
@@ -57,7 +57,9 @@ def convert_file(item, state)
def create_path_and_convert(item)
dest_file = Pathname.new(item.target_path)
FileUtils.mkdir_p(dest_file.dirname.to_s)
+
transcode(item.source_path, dest_file.to_s)
+ get_screenshot(item.source_path, item.screenshot_path)
end
def transcode(input, output)
@@ -69,13 +71,28 @@ def transcode(input, output)
logger.info "#{p.basename.to_s} doesn't exist, starting transcode..."
@transcoder.transcode(input, output)
end
+
+ def get_screenshot(input, output)
+ logger.debug "Saving screenshot to #{output}"
+ @transcoder.get_screenshot(input, output)
+ end
end
module ExternalTranscoder
def transcode(input, output)
- cmd = get_command(input, output)
- #puts "Running #{cmd}"
-
+ cmd = get_transcode_command(input, output)
+ run_external_command(cmd)
+ end
+
+ def get_screenshot(input, output)
+ cmd = get_screenshot_command(input, output)
+ run_external_command(cmd)
+ end
+
+private
+ def run_external_command(cmd)
+ logger.debug "Running #{cmd}"
+
# FIXME: Dumb hack code!
pid = nil
unless (pid = fork)
@@ -110,8 +127,8 @@ def transcode(input, output)
# Kernel.exec
# after_transcode() if self.respond_to? :before_transcode
# end
-
end
+
end
class FFMpegTranscoder
@@ -167,11 +184,17 @@ class FFMpegTranscoder
AudioParams = %w( -acodec libfaac -ac 2 -ar 44100 -ab 0 -aq 120 -alang ENG )
FFMpegPath = File.join(AppConfig::RootDir, 'libexec', 'bin', 'ffmpeg')
- def get_command(input, output)
+ def get_transcode_command(input, output)
ret = ["#{FFMpegPath} -i \"#{input}\""] + MiscParams + VideoParams + AudioParams + ["\"#{output}\""]
ret.join ' '
end
+ ScreenshotParams = %w( -f mjpeg -s 244x164 -vframes 1 -ss 2 )
+ def get_screenshot_command(input, output)
+ ret = ["#{FFMpegPath} -i \"#{input}\""] + ScreenshotParams + ["\"#{output}\""]
+ ret.join ' '
+ end
+
def before_transcode
ENV["LD_LIBRARY_PATH"] = File.join(AppConfig::RootDir, 'libexec', 'lib')
end
View
@@ -72,9 +72,15 @@ def pidfile_dir
def settings_dir
return "DONTKNOW" if os == :windows
- p = File.join(home_dir, ".yikes")
- `mkdir -p #{p}`
- return p
+ path = File.join(home_dir, ".yikes")
+ FileUtils.mkdir_p path
+ return path
+ end
+
+ def screenshot_dir
+ path = File.join(Platform.settings_dir, 'preview')
+ FileUtils.mkdir_p path
+ return path
end
def hostname
View
@@ -29,6 +29,7 @@
require 'fileutils'
require 'thread'
require 'yaml'
+require 'digest/md5'
include GetText
@@ -159,6 +160,11 @@ def subpath_target
EncodingItem.subpath_as_target_ext(@subpath)
end
+ def screenshot_path
+ md = Digest::MD5.new << @subpath
+ File.join Platform.screenshot_dir, md.to_s + ".jpg"
+ end
+
class << self
def extract_subpath(source_root, file)
return file unless file.index(source_root) == 0 and not source_root.empty?

0 comments on commit caa6297

Please sign in to comment.