-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscode.ps1
More file actions
108 lines (96 loc) · 2.91 KB
/
Transcode.ps1
File metadata and controls
108 lines (96 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Here lie the global variables
$ScreenConnectDir = "C:\Program Files (x86)\ScreenConnect"
$SaveDirectory = "C:\Transcoded Sessions"
Add-Type -Path (Get-Item "$ScreenConnectDir\bin\ScreenConnect.Core.dll").FullName
Add-Type -Path (Get-Item "$ScreenConnectDir\bin\ScreenConnect.Server.dll").FullName
$TranscodeAttempts=0
# Returns true if a transcode has already happened (based on filename)
Function ExistingTranscode($fileNameToTest)
{
# Returns true if the file exists
Return (Test-Path "$SaveDirectory\$fileNameToTest")
}
# Transcodes a raw capture file to its output
Function Transcode
{
Param(
[Parameter(Mandatory=$false,Position=0)] $rawCapturePath,
[Parameter(Mandatory=$false,Position=1)] $outputFileName
)
# If a transcode hasn't happened yet, do it
If(-Not(ExistingTranscode($outputFileName)))
{
Write-Host "Transcoding $outputFileName"
Try
{
# Attempt the transcode
$inputStream = [System.IO.File]::OpenRead($rawCapturePath)
$toolkitInstance = [ScreenConnect.ServerToolkit]::Instance
$encoder = $toolkitInstance.CreateVideoFileEncoder()
[ScreenConnect.ServerExtensions]::Transcode($inputStream, $encoder, "$SaveDirectory\" + $outputFileName)
}
Catch
{
# Display encountered errors
echo "Error processing $($captureFile): $($Error[0])"
}
Finally
{
# Close the source file reader
$inputStream.Dispose()
}
}
Else
{
# If the file exists, but it is less than 6000 bytes, redo it
If((Get-Item ("$SaveDirectory\" + $outputFileName)).Length -lt 6000)
{
If($TranscodeAttempts -lt 3)
{
Write-Host "Removing item... $outputFileName"
Remove-Item ("$SaveDirectory\" + $outputFileName)
Transcode -rawCapturePath $rawCapturePath -outputFileName $outputFileName
$TranscodeAttempts += 1
}
Else
{
$TranscodeAttempts=0
Break
}
}
}
}
# Loop through the raw captures
$files = Get-ChildItem("$ScreenConnectDir\App_Data\Session\*")
ForEach ($captureFile in $files)
{
$captureFileName = $captureFile.Name
$captureFilePath = $captureFile.FullName
# Split the filename into pieces (GUIDs and timestamp)
$RegEx = "-"
$GUIDS = $captureFileName -Split $RegEx
# Obtain the session GUID
$SessionGUID = ""
For($i=0; $i -lt 5; $i++)
{
$SessionGUID = $SessionGUID + $GUIDS[$i] + "-"
}
$SessionGUID = $SessionGUID.Substring(0,$SessionGUID.Length-1)
# Obtain the capture GUID
$CaptureGUID = ""
For($i=5; $i -lt 10; $i++)
{
$CaptureGUID = $CaptureGUID + $GUIDS[$i] + "-"
}
$CaptureGUID = $CaptureGUID.Substring(0,$CaptureGUID.Length-1)
# Obtain the timestamp
$timestamp = ""
For($i=10; $i -lt 16; $i++)
{
$timestamp = $timestamp + $GUIDS[$i] + "-"
}
$timestamp = $timestamp.Substring(0,$timestamp.Length-1)
# Format the filename as timestamp_SessionGUID_CaptureGUID.avi
$outputFileName = $timestamp + "_" + $SessionGUID + "_" + $CaptureGUID + ".avi"
Transcode -rawCapturePath $captureFilePath -outputFileName $outputFileName
}