Skip to content

Instantly share code, notes, and snippets.

@kubawolanin
Last active April 7, 2021 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubawolanin/7aadfed80e41954beadd285acf12ef90 to your computer and use it in GitHub Desktop.
Save kubawolanin/7aadfed80e41954beadd285acf12ef90 to your computer and use it in GitHub Desktop.
Automatically copy latest footage from Sony NEX camera to predefined folder on Windows PC

Automation idea: When I stop recording my footage and camera is plugged to USB port, copy latest video file to a predefined folder + to a currently open REAPER project.

TODO:

  • Check if file exists in the destination path before copying
  • Run the bat script hidden (prevent the window from appearing)
  • Or get rid of the bat script altogether and try to reimplement it all in powershell function?
  • Try to insert the footage into Reaper (InsertFootage.lua)

Prereq

  1. Plug your camera to USB port
  2. Note its drive letter and label

PowerShell ISE

  1. Open PowerShell ISE as Administrator
  2. Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy ByPass
  3. Click New
  4. Paste SyncNEX.ps1
  5. Modify variables: $cameraDriveLetter and cameraDriveLabel
  6. Save file

Camera drive

  1. In your camera create a new file called sync.bat in the root path
  2. Paste sync.bat content inside
  3. Modify FootageOrigin and FootageDestination

Task Scheduler

  1. Open Task Scheduler
  2. Create basic task
  3. Trigger: At log on
  4. Action: Start a program
  5. Program/script: powershell
  6. Arguments
-WindowStyle Hidden -ExecutionPolicy Unrestricted -File "C:\Users\Studio\Documents\WindowsPowerShell\SyncNEX.ps1"

get latest file in forlder

for /f "tokens=* usebackq" %f in (`dir /b /o:-d *.mkv`) do echo %f
function Get-IniContent ($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
“^\[(.+)\]” # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
“^(;.*)$” # Comment
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = “Comment” + $CommentCount
$ini[$section][$name] = $value
}
“(.+?)\s*=(.*)” # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
$iniContent = Get-IniContent “$env:APPDATA\REAPER\REAPER.ini”
Split-Path -Path $iniContent["reaper"]["lastproject"]
-- dofile(reaper.GetResourcePath().."/UserPlugins/ultraschall_api.lua")
-- reaper.Main_openProject("C:/Users/Studio/AppData/Roaming/REAPER/TrackTemplates/improvtemplate.RTrackTemplate")
path = reaper.GetProjectPath("")
-- TODO: try to get latest file from that path
reaper.InsertMedia(path .. "/Video/" .. "2020-03-31 12-27-21.mkv", 1) -- 1 = insert item into a new Track
-- f = io.popen("dir \"".. path .."\"", "r")
-- array files = ultraschall.GetAllFilenamesInPath(string path)
@REM Place this file in root directory of your camera drive
echo Copy latest footage
for /f "skip=1" %%i in ('wmic os get localdatetime') do if not defined fulldate set fulldate=%%i
set year=%fulldate:~0,4%
set month=%fulldate:~4,2%
set day=%fulldate:~6,2%
set todayfolder=%year%-%month%-%day%
SET FootageOrigin=%CD%PRIVATE\AVCHD\BDMV\STREAM
SET FootageDestination=D:\Video\raw\%todayfolder%
md %FootageDestination%
FOR /F "delims=|" %%I IN ('DIR "%FootageOrigin%\*.MTS" /B /O:D') DO SET NewestFile=%%I
copy "%FootageOrigin%\%NewestFile%" "%FootageDestination%"
# Source: https://superuser.com/a/845411
$cameraDriveLetter = 'G:'
$cameraDriveLabel = 'NEX 5T'
#Requires -version 2.0
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
write-host (get-date -format s) " Beginning script..."
do{
$newEvent = Wait-Event -SourceIdentifier volumeChange
$eventType = $newEvent.SourceEventArgs.NewEvent.EventType
$eventTypeName = switch($eventType)
{
1 {"Configuration changed"}
2 {"Device arrival"}
3 {"Device removal"}
4 {"docking"}
}
write-host (get-date -format s) " Event detected = " $eventTypeName
if ($eventType -eq 2)
{
$driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
$driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeName
write-host (get-date -format s) " Drive name = " $driveLetter
write-host (get-date -format s) " Drive label = " $driveLabel
# Execute process if drive matches specified condition(s)
if ($driveLetter -eq $cameraDriveLetter -and $driveLabel -eq $cameraDriveLabel)
{
write-host (get-date -format s) " Starting task in 3 seconds..."
start-sleep -seconds 3
start-process "$cameraDriveLetter\sync.bat"
}
}
Remove-Event -SourceIdentifier volumeChange
} while (1-eq1) #Loop until next event
Unregister-Event -SourceIdentifier volumeChange
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment