#!/usr/bin/env ruby $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') require 'rest_client' require 'mime/types' require 'tempfile' def upload_audio(filename, api,public,tag,txt) begin RestClient.post('http://cluster.metaforas.cl/audios', "audio[audio_original]" => File.new(filename), :api => api, "audio[public]" => public, "audio[tag]" => tag, "audio[txt]" => txt ) rescue RestClient.post('http://cluster.metaforas.cl:8000/audios', "audio[audio_original]" => File.new(filename), :api => api, "audio[public]" => public, "audio[tag]" => tag, "audio[txt]" => txt ) end end filename = ARGV[1] basename = File.basename(filename) types = MIME::Types.type_for(filename) valido = false for i in types if i.content_type.include? "wav" or i.content_type.include? "x-wav" valido = true end end t = nil if !valido t = Tempfile.new(basename) puts "convirtiendo a wav antes de subir" `rm -f "#{t.path}"` `ffmpeg -i "#{filename}" -vn -acodec pcm_s16le -ar 16000 -ac 1 -f wav "#{t.path}"` filename = t.path end file = File.new(ARGV[1]) public = false if ARGV.size >= 3 public = ARGV[2] end if f[3].nil? puts "uploading..." upload_audio(filename,ARGV[0],public,nil,ARGV[4]) else limit = ARGV[3].to_i puts "cortando archivos en trozos de #{limit} segundos" lr = `ffmpeg -i "#{filename}" 2>&1 | grep 'Duration'| cut -d ' ' -f 4 | cut -d ',' -f 1` lr = lr.split(':') length = lr[0].to_i*3600 + lr[1].to_i*60 + lr[2].to_f puts "largo original archivo: #{length}" start_time = 0 counter = 0 max_parts = (length/limit).ceil while start_time <= length do counter += 1 duration = [limit,length - start_time].min hex = Digest::MD5.hexdigest(rand().to_s) tag = hex[0, 8] + "_#{counter}_#{max_parts}" puts "convirtiendo #{counter}/#{max_parts}" `sox '#{filename}' -b 16 -s -c 1 -r 16k '#{filename}-#{counter}-#{max_parts}.wav' trim #{start_time} #{duration}` puts "uploading... #{counter}/#{max_parts}" upload_audio("#{filename}-#{counter}-#{max_parts}.wav",ARGV[0],public,tag,ARGV[4]) start_time += limit end end if !valido t.close `rm -f "#{t.path}"` end puts "ready"