From: Chris Donhofer Date: 2006-09-25T07:10:09+09:00 Subject: Re: catching process output (Kernel#system) --------------030808020403030003010809 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit i got my video converter script working and since im very new to ruby, i'd really appreciate feedback, so i can learn how to use ruby better. (the script is attached) all comments welcome! =) TIA, chris --------------030808020403030003010809 Content-Type: text/plain; name="flv_encoder.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="flv_encoder.rb" #@Author: Christian (insel) Donhofer #@Contact: c.donhofer@chello.at require 'ftools' require 'date' class FlvEncoder def instantiate end def start_conversion @ts = DateTime.now msg "FLV_Encoder [SESSION STARTING](#{@ts.day}.#{@ts.month}.#{@ts.year}, #{@ts.hour}:#{@ts.min}:#{@ts.sec})..." #define working directories @indir = Dir.pwd+"/in" @spooldir = Dir.pwd+"/spool" @outdir = Dir.pwd+"/out" @bakdir = Dir.pwd+"/bak" @dumpdir = Dir.pwd+"/dump" @files = [] #get filelist msg "reading #{@indir}" @files = Dir.entries(@indir) #transcode all media files @files.each do |file| do_convert(file) unless (file.eql?(".") || file.eql?("..")) end end def do_convert(file) @ts = DateTime.now msg "[PROCESS STARTING](#{@ts.day}.#{@ts.month}.#{@ts.year}, #{@ts.hour}:#{@ts.min}:#{@ts.sec})..." #create backup msg "Creating backup instance...".upcase msg "#{@indir}/#{file} ---> #{@bakdir}/#{file}" File.copy("#{@indir}/#{file}", "#{@bakdir}/#{file}") #move file msg "MOVING FILE..." msg "#{@indir}/#{file} ---> #{@spooldir}/#{file}" File.move("#{@indir}/#{file}", "#{@spooldir}/#{file}") #define actual and targetfile actual_file = "#{@spooldir}/#{file}"; target_file = "#{@outdir}/"+File.basename(file, File.extname(file))+".flv"; #check file for readability if(File.exist?(actual_file) && File.readable?(actual_file)) msg "File ready for conversion..." else msg "File not readable or not found - [SKIP]" return end #convert file msg "STARTING CONVERSION..." if(File.extname(actual_file)==".mov"|| File.extname(actual_file)==".avi") msg "USING FFMPEG..." msg "CMD: ffmpeg -i #{actual_file} -t flv #{target_file} 2>&1" IO.popen("ffmpeg -i #{actual_file} -t flv #{target_file} 2>&1") do |l| msg l.readlines end elsif(File.extname(actual_file)==".swf") msg "USING SWF2FLV..." msg "CMD: swf2flv #{actual_file} #{target_file}" IO.popen("swf2flv #{actual_file} #{target_file}") do |l| msg l.readlines end else msg "FILETYPE NOT RECKOGNIZED, DUMPING FILE, [SKIP]..." File.move("#{@spooldir}/#{file}", "#{@dumpdir}/#{file}") return end msg "[UNLINK]ING(DELETING) FILE IN SPOOL DIRECTORY..." File.unlink("#{@spooldir}/#{file}") if File.exist?("#{@spooldir}/#{file}") #check resulting flv file msg "[FLVINFO] for resulting file:" IO.popen("flvinfo #{target_file}") do |l| msg l.readlines end @ts = DateTime.now msg "[PROCESS ENDING](#{@ts.day}.#{@ts.month}.#{@ts.year}, #{@ts.hour}:#{@ts.min}:#{@ts.sec})..." #TODO write log to file end #log/output text def msg(str) puts str #write to log if @log.nil? @log = [] end @log.push str end end #create converter conv = FlvEncoder.new conv.start_conversion --------------030808020403030003010809--