ruby 处理来自我们手机的btsynced照片和视频

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 处理来自我们手机的btsynced照片和视频相关的知识,希望对你有一定的参考价值。

require 'mini_magick'
require 'active_support/core_ext/object/blank'
require 'fileutils'

SOURCE_DIR      = 'C:/store/sync'
DESTINATION_DIR = 'C:/Users/ikon/megasync'

Dir.glob "#{SOURCE_DIR}/**/*.{jpg,mp4}" do |filepath|

  50.times { print '=' }
  puts ''
  puts "Processing [#{filepath}]"

  case filepath.slice -4, 4
    when '.jpg'
      image = MiniMagick::Image.new filepath
      exif  = image.exif
      if exif.present? and exif['DateTimeOriginal'].present?
        puts 'Using exif date'

        exif_date = exif['DateTimeOriginal']
        date      = Date.parse exif_date.slice(0, 10).gsub(/:/, '-')
        filename  = Time.parse(exif_date.gsub(/(\d{4}):(\d{2}):(.+)/) { "#{$1}-#{$2}-#{$3}" }).strftime('%Y-%m-%d %H.%M.%S') + '.jpg'
      else
        puts 'Using file modification time'

        # use file modification date
        date     = File.mtime(filepath).to_date
        filename = File.mtime(filepath).strftime('%Y-%m-%d %H.%M.%S') + '.jpg'
      end
    when '.mp4'
      date     = File.mtime(filepath).to_date
      filename = File.mtime(filepath).strftime('%Y-%m-%d %H.%M.%S') + '.mp4'
    else
      puts "Where did this come from: #{filepath} ?"
      next # skip to the next file
  end

  # process file

  # change this if a different directory name is needed
  dir      = "#{DESTINATION_DIR}/Mark - #{date.year} #{Date::MONTHNAMES[date.month]}"
  FileUtils.mkdir_p dir

  # add a microsec suffix if another file with same name already exists
  filename = filename.insert -5, " - #{Time.now.usec}" if File.exist? "#{dir}/#{filename}"

  puts "Final destination: #{dir}/#{filename}"
  FileUtils.copy_file filepath, "#{dir}/#{filename}"
end
source 'https://rubygems.org'

gem 'mini_magick'
gem 'activesupport'

以上是关于ruby 处理来自我们手机的btsynced照片和视频的主要内容,如果未能解决你的问题,请参考以下文章

如何利用 BTSync 搭建私有云储存

怎样在手机上调整照片像素和大小

求问怎么把照片裁剪成证件照?希望可以批量处理的。

iOS8中怎样旋转和裁剪相片

手机有啥软件可以调节照片的像素分辨率和大小?

手机处理报名照片海南省特岗教师报名流程及照片手机在线处理工具教程