Skip to content

Instantly share code, notes, and snippets.

@dav
Last active August 2, 2022 04:46
Show Gist options
  • Save dav/d5dcf3209a70315e3162469d19346b6d to your computer and use it in GitHub Desktop.
Save dav/d5dcf3209a70315e3162469d19346b6d to your computer and use it in GitHub Desktop.
Script for copying Insta360 videos from device to a NAS drive, and renaming for Pigasus VR Viewing
require 'fileutils'
require 'digest'
class MediaMover
NAS_MEDIA_DIR = '/media/nas/video'
NAS_RAW_INCOMING_DIR = NAS_MEDIA_DIR + '/Incoming Raw Footage/Insta360'
INSTA360_MOUNT_POINT = '/media/dav/disk'
INSTA360_MEDIA_DIR = INSTA360_MOUNT_POINT + '/DCIM/Camera01'
def run
ensure_exists NAS_MEDIA_DIR
ensure_exists NAS_RAW_INCOMING_DIR do
puts <<-EOEXPLANATION
It seems that `#{NAS_RAW_INCOMING_DIR}` does not exist. The NAS is probably not mounted.
You can run `sudo mount -t nfs 192.168.1.60:/volume1/video /media/nas/video/`
But really should look at why /etc/fstab did not automount. This was working previously with this line:
192.168.1.60:/volume1/video /media/nas/video nfs auto,defaults,nofail 0 0
EOEXPLANATION
exit
end
ensure_exists INSTA360_MOUNT_POINT do
puts <<-NO_I360
It seems that the Insta360 camera is not mounted at #{INSTA360_MOUNT_POINT}.
It should be connected to the computer via cable and turned with a blinking blue light.
NO_I360
exit
end
ensure_exists INSTA360_MEDIA_DIR do
puts "It seems that the Insta360 camera file system is not as expected, there should be a #{INSTA360_MEDIA_DIR}."
exit
end
file_index = {}
puts "Processing files in #{INSTA360_MEDIA_DIR}"
puts "Note: creating the hash is slow for large files"
Dir.glob("#{INSTA360_MEDIA_DIR}/*.*") do |media_file|
puts "Hashing #{media_file}"
file_md5_hash = Digest::MD5.file(media_file).hexdigest
file_index[file_md5_hash] = media_file
begin
safe_copy_and_rename media_file
rescue => e
puts e
end
end
compare_to_other_dirs(file_index)
puts "OK"
end
private
def ensure_exists(dir_path)
return if Dir.exist?(dir_path)
if block_given?
yield
else
FileUtils.mkdir_p dir_path
end
end
def filename_with_360_appended(filename)
end
# Note: rname accomplishes two things:
# 1: Appendiing a _360 allows Pigasus VR Viewer app to automatically treat it as 360 vide
# 2: Replacing _ with - makes it so that Pigasus does not interpret a substring like _180 to mean it is a 180 video
def safe_copy_and_rename(src_file_path)
src_basename = File.basename src_file_path
src_extname = File.extname src_file_path
dst_basename = File.basename(src_basename) + src_extname
nas_file_path = "#{NAS_RAW_INCOMING_DIR}/#{dst_basename}"
if File.exist? nas_file_path
if same_file(src_file_path, nas_file_path)
puts "#{src_basename} already exists on NAS as #{nas_file_path}"
else
puts "#{src_basename} already exists on NAS but is not identical to what's on the camera with same name: #{nas_file_path}"
end
else
begin
FileUtils.cp(src_file_path, nas_file_path)
rescue Exception => e
raise "Could not copy #{src_file_path} to #{nas_file_path}: #{e}"
end
puts "#{src_basename} copied to #{nas_file_path}"
end
end
def same_file(file_1, file_2)
cmd = "cmp \"#{file_1}\" \"#{file_2}\""
return system(cmd)
end
def compare_to_other_dirs(file_index)
other_dirs = [
'/media/nas/video/Backups/Insta360/',
'/media/nas/video/Backups/Insta360/'
]
other_dirs.each do |dir|
Dir.glob(dir+'*.ins?').each do |other_file|
file_md5_hash = Digest::MD5.file(other_file).hexdigest
incoming_file = file_index[file_md5_hash]
if !incoming_file.nil?
puts "#{other_file} already exists on NAS as #{incoming_file}"
else
puts "#{other_file} does not exist in the main incoming dir"
end
end
end
end
end
MediaMover.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment