Clase para los videos de la Web TV.
Nota sobre los idiomas: las columnas que determinan el idioma en el que está disponible el video (show_in_es, show_in_eu y show_in_en) se actualizan automáticamente con un cronjob que llama a RAILS_ROOT/batch_processes/check_webtv_video_languages.sh.
Este proceso es necesario porque los videos se suben independientemente a través de SFTP sin tener que pasar por la aplicación web. Las columnas son necesarias para que por ejemplo en euskera, los listados solo contengan los videos disponibles en euskera
# File app/models/video.rb, line 164 def self.categories Tree.find_videos_tree ? Tree.find_videos_tree.categories.roots : [] end
# File app/models/video.rb, line 155 def self.featured self.published.translated.find(:first, :conditions => ["featured=?", true]) || Video.published.translated.find(:first, :order => "published_at DESC") end
Devuelve los videos FLV que hay en el directoio path
# File app/models/video.rb, line 160 def self.videos_in_dir(path) Dir.glob(Video::VIDEO_PATH + path + "_e[sun].flv") + Dir.glob(Video::VIDEO_PATH + path + ".flv") end
# File app/models/video.rb, line 175 def body # self.title "" end
# File app/models/video.rb, line 242 def captions_available? self.subtitles_es.exists? || self.subtitles_eu.exists? || self.subtitles_en.exists? end
Subtítulos
# File app/models/video.rb, line 238 def captions_file_name "#{self.featured_video}".gsub(/\.flv/,'.srt') end
# File app/models/video.rb, line 246 def captions_url(locale = I18n.locale) # self.captions_available? ? "#{Video::VIDEO_URL}#{self.captions_file_name}" : nil if self.captions_available? self.send("subtitles_#{locale}").exists? ? self.send("subtitles_#{locale}").url : self.subtitles_es.url else nil end end
# File app/models/video.rb, line 184 def current_video_file_exists? File.exists?(current_video_file_path) && File.file?(current_video_file_path) end
# File app/models/video.rb, line 180 def current_video_file_path "#{Video::VIDEO_PATH}#{self.featured_video}" end
# File app/models/video.rb, line 317 def display_format_from_file info_from_file[:display_format] end
# File app/models/video.rb, line 321 def duration_from_file info_from_file[:duration] end
Video a mostrar, en función del idioma actual
# File app/models/video.rb, line 109 def featured_video videos[I18n.locale.to_sym] || videos[:common] || videos[I18n.default_locale.to_sym] || videos.values.first end
Devuelve un array de tiempos con todas las coincidencias de la palabra
# File app/models/video.rb, line 266 def get_times_from_keyword(keyword, locale=I18n.locale) sec_values = [] if self.send("subtitles_#{locale}").exists? srt_file = File.new(self.send("subtitles_#{locale}").path).read values = [] keyword.split(' ').each do |kw| # teniendo en cuenta apariciones dentro de palabras /\s?\n(.*)\s-->\s.*[^\r]\s?\n.*\s#{kw}\s.*\s?\n/i values << srt_file.scan(/\s?\n(.*)\s-->\s.*[^\r]\s?\n.*#{kw}.*\s?\n/) end values.flatten.each do |val| start = Time.parse(val.to_s) sec_values << start.hour * 60 * 60 + start.min * 60 + start.sec end end return sec_values end
Determina si este video tiene imagen para preview
# File app/models/video.rb, line 139 def has_cover_photo? File.exists?("#{Video::VIDEO_PATH}{video_path}.jpg") end
# File app/models/video.rb, line 148 def is_private? !self.is_public? end
/ Igual que en Document
# File app/models/video.rb, line 144 def is_public? !self.published_at.nil? end
Obtener departamentos del album a partir de los tags
# File app/models/video.rb, line 328 def organization Department.find(:first, :conditions => {:tag_name => self.tags.private.map(&:name)}) end
Indica si el video está publicado
# File app/models/video.rb, line 90 def published? !published_at.nil? && published_at <= Time.now end
Devuelve los cuepoints del video parseando el fichero con la transcripción. Si falla el parser del CSV, no se muestra de transcripción.
# File app/models/video.rb, line 200 def transcription subtitles = self.send("subtitles_#{I18n.locale}").exists? ? self.send("subtitles_#{I18n.locale}") : self.subtitles_es filename = subtitles.path cuepoints = {} time_regex = /(\d{2}):(\d{2}):(\d{2}),(\d{3})/ begin if File.exists?(filename) && File.file?(filename) f = File.open(filename) time = nil text = nil get_text = false f.each do |line| if line.match(time_regex) hh,mm,ss,ms = line.scan(time_regex).flatten.map{|i| i.to_i} time = hh*3600 + mm*60 + ss cuepoints[time.to_s] = "" get_text = true next end if get_text cuepoints[time.to_s] += line.strip.gsub(/\.{2,}$/,'').gsub(/^\s*\.{2,}/,'').strip end if line.blank? get_text = false time = nil end end end rescue => err logger.error "ERROR transcripción: Video #{self.id}, fichero #{filename}, #{err}" end cuepoints end
Transccripción del vídeo
La generamos a partir del fichero con los subtítulos.
# File app/models/video.rb, line 194 def transcription_available? self.captions_available? end
Indica si el video está traducido a lang
# File app/models/video.rb, line 114 def translated_to?(lang) !videos[lang.to_sym].nil? || !videos[:common].nil? end
Devuelve el listado de videos disponible para este video. Puede haber varios, uno para cada idioma.
# File app/models/video.rb, line 96 def videos videos = {} if video_path.present? videos_in_dir = Video.videos_in_dir(video_path) Video::LANGUAGES.each do |l| videos[l.to_sym] = "#{video_path}_#{l}.flv" if videos_in_dir.include?("#{Video::VIDEO_PATH}#{video_path}_#{l}.flv") end videos[:common] = "#{video_path}.flv" if videos_in_dir.include?("#{Video::VIDEO_PATH}#{video_path}.flv") end return videos end
Asegura que sólo hay un video destacado. Se llama desde before_update
# File app/models/video.rb, line 334 def check_only_one_featured Video.update_all("featured='f'") if featured && featured_changed? end
Obtiene la duración del video
# File app/models/video.rb, line 339 def set_video_duration_and_display_format if current_video_file_exists? if self.duration.blank? logger.info "Obteniendo duracion de #{current_video_file_path}" self.duration = self.duration_from_file end if self.display_format.blank? logger.info "Obteniendo formato de #{current_video_file_path}" self.display_format = self.display_format_from_file end end return true end
# File app/models/video.rb, line 353 def sync_mapping_with_closed_captions_category x= AvailableLocales::AVAILABLE_LANGUAGES.keys.collect {|l| self.send("subtitles_#{l.to_s}_file_name")} if x.uniq.compact == [] self.tag_list.remove Category::CLOSED_CAPTIONS_TAG else self.tag_list.add Category::CLOSED_CAPTIONS_TAG end end