1

我正在尝试扫描一个大型硬盘驱动器(想想 17TB)以查找特定文件夹中视频文件的存储估计值。此外,我正在尝试查找视频文件的特定独特属性。这样做的目的是为数字资产管理系统提供一个案例,该系统可以支持我们可以追溯到 2009 年的视频文件。我正在使用 mediainfo 来检查每个视频。

我的文件大小/存储计数正常工作,但是在将视频属性的哈希添加到循环中的数组时遇到问题。我的目标是让媒体信息查看每个视频的特定属性,将它们放入散列并将该散列添加到数组中。然后,一旦我收集了视频属性的所有哈希值,我就会调用 uniq!在阵列上,以便它向我展示独特的视频属性。

我的代码输出当前一遍又一遍地返回最后一个视频的视频属性。我看不出我做错了什么。有什么建议吗?

require 'yaml'

#!/usr/bin/ruby

library_path = ARGV[0]

files_list = Dir.glob("#{library_path}/**/*")
total_capture_scatch_and_exports_size = 0

video_audit = Hash.new()
video_info = []
codecs = Hash.new()




files_list.each do |filepath|
filename = File.basename(filepath.to_s)
  filepath.chomp!
  puts filename
  folders_to_scan = ["/capture scratch/", "/capturescratch/", "/capture-scratch/", "/capture_scratch/", "exports", "export"]

  file_size = File.size(filepath)
  file_extension = File.extname(filepath)
  if 
    folders_to_scan.any? { |folder| filepath.downcase.include? folder }

      if 
        File.file?(filepath) && filename[0] != "." && file_extension != ".TIFF" && file_extension != ".TIF" && file_extension != ".tif" && file_extension != ".tiff" && file_extension != ".jpg" && file_extension != ".jpeg" && file_extension != ".JPG" && file_extension != ".JPEG"
            duration = %x[mediainfo --Inform="General;%Duration/String3%" '#{filepath}'].chomp!
            format = %x[mediainfo --Inform="General;%Format%" '#{filepath}'].chomp!
            commercial_name = %x[mediainfo --Inform="General;%Format_Commercial_IfAny%" '#{filepath}'].chomp!
            format_profile = %x[mediainfo --Inform="General;%Format_Profile%" '#{filepath}'].chomp!
            writing_library = %x[mediainfo --Inform="General;%Encoded_Library%" '#{filepath}'].chomp!


            video_audit[:filepath] = filepath
            video_audit[:filename] = filename   
            video_audit[:duration] = duration
            video_audit[:file_size] = file_size
            video_audit[:format] = format
            video_audit[:commercial_name] = commercial_name
            video_audit[:format_profile] = format_profile
            video_audit[:writing_library] = writing_library
            video_audit[:file_extension] = file_extension

            codecs[:filename] = filename
            codecs[:format] = format
            codecs[:commercial_name] = commercial_name
            codecs[:format_profile] = format_profile
            codecs[:writing_library] = writing_library
            codecs[:file_extension] = file_extension    
      end   

  end
  puts video_audit.to_yaml
  puts codecs
  video_info << codecs
  total_capture_scatch_and_exports_size += file_size

end
puts "THE VIDEO INFO IS=======>>>> #{video_info}"
puts "THE UNIQUE CODECS ARE: #{video_info.uniq!}"
#1000**3 is for gigabytes (this is how finder on OSX calculates storage on the Drobo Harddrives)use 1024**3 ofr gibibytes
puts "The total filesize is : #{total_capture_scatch_and_exports_size/(1000**3).to_f} GB"
4

1 回答 1

0

我想到了。我在循环之外创建了新的哈希值。每次迭代都需要创建一个新的散列,以便将其添加到 video_info 数组中。然后,当我在脚本末尾的 video_info 上调用 uniq 时,我需要删除 bang 运算符。这是我的最终代码:

require 'json'

#developed by Maile Thiesen
#!/usr/bin/ruby

library_path = ARGV[0]

files_list = Dir.glob("#{library_path}/**/*")
total_capture_scatch_and_exports_size = 0
counter = 0

video_info = []

files_list.each do |filepath|
filename = File.basename(filepath.to_s)
  codecs = {}
  filepath.chomp!
  folders_to_scan = ["/capture scratch/", "/capturescratch/", "/capture-scratch/", "/capture_scratch/", "exports", "export"]

  file_size = File.size(filepath)
  file_extension = File.extname(filepath)
  if 
      folders_to_scan.any? { |folder| filepath.downcase.include? folder }

      if 
          File.file?(filepath) && filename[0] != "." && file_extension != ".TIFF" && file_extension != ".TIF" && file_extension != ".tif" && file_extension != ".tiff" && file_extension != ".jpg" && file_extension != ".jpeg" && file_extension != ".JPG" && file_extension != ".JPEG"
              duration = %x[mediainfo --Inform="General;%Duration/String3%" '#{filepath}'].chomp!
              format = %x[mediainfo --Inform="General;%Format%" '#{filepath}'].chomp!
              commercial_name = %x[mediainfo --Inform="General;%Format_Commercial_IfAny%" '#{filepath}'].chomp!
              format_profile = %x[mediainfo --Inform="General;%Format_Profile%" '#{filepath}'].chomp!
              writing_library = %x[mediainfo --Inform="General;%Encoded_Library%" '#{filepath}'].chomp!

              codecs[:format] = format
              codecs[:commercial_name] = commercial_name
              codecs[:format_profile] = format_profile
              codecs[:writing_library] = writing_library
            codecs[:file_extension] = file_extension    
            total_capture_scatch_and_exports_size += file_size

            counter += 1
            video_info << codecs
      end    

  end

end
puts "THE UNIQUE CODECS ARE: #{JSON.pretty_generate(video_info.uniq)}"
puts "THE TOTAL FILESIZE IS : #{total_capture_scatch_and_exports_size/(1000**3).to_f} GB"
于 2016-10-07T16:50:49.100 回答