-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaily_Video_Handler.ps1
132 lines (111 loc) · 6.22 KB
/
Daily_Video_Handler.ps1
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<#
Author: Adam Mikolajczyk
Email: [email protected]
Description: Creates a daily time lapse video based on 5 minute camera still frame snapshots. Uploads to FTP server while doing some basic file handling on the FTP server to prevent pile-up.
I create two versions of the time laspse, one high quality version suitable for archival and one lower quality version for possible web hosting platforms.
Requires: ImageMagic utility bundle's identify.exe and ffmpeg.exe
#>
#path to ffmpeg
$ffmpeg = "ffmpeg.exe"
#Establish staging area to work with still frame pictures
$staging = "D:\temp\"
#create staging folder if necessary, remove content of staging folder if necessary
if (!(test-path $staging)){new-item -itemtype directory $staging}
if (test-path $staging\*.jpg){remove-item $staging\*.jpg}
#As this is meant to run after midnight, it needs to handle yesterday's folder and files
$Yesterday = '\\SERVERNAME\media\pictures\weathercam\'+((Get-Date).AddDays(-1).ToString('yyyyMMdd'))
#Get list of all of yesterday's files sorted by last write time
$Pictures = GCI $Yesterday\*Camera01.jpg | ?{$_.Length -gt 0} | sort-object -property LastWriteTime
#Since FFMPEG needs a numerically indexed file naming scheme, we are copying the files in chronological order to the staging area in the format #####.jpg
For ($i=0; ($i -lt ($Pictures.count)); $i++){copy-item -path ($Yesterday + "\" + ($Pictures[$i].Name)) -destination (${Staging} + ("{0:D5}" -f $i) + ".jpg") -force}
#Create the arguments to be fed to FFMPEG for botht the HQ and streaming versions of the video
$web_arguments = "-framerate 15 -y -v quiet -i $staging\%05d.JPG -c`:v libx264 -crf 20 -r 15 -vf scale=640:-1 ${Yesterday}Cam01Day_web.mp4"
$arguments = "-framerate 15 -y -v quiet -i $staging\%05d.JPG -c`:v libx264 -crf 20 -r 15 ${Yesterday}Cam01Day.mp4"
#Process the video creation
Start-Process -FilePath $ffmpeg -Argumentlist $arguments -Wait
Start-Process -FilePath $ffmpeg -Argumentlist $web_arguments -Wait
#Remove the content from the staging area once complete
remove-item $staging\*.jpg
#Since I have two cameras, here I'm starting the whole process over again using the still-frame pictures of the second camera
#Establish staging area to work with still frame pictures
$staging = "D:\temp\"
if (!(test-path $staging)){new-item -itemtype directory $staging}
if (test-path $staging\*.jpg){remove-item $staging\*.jpg}
#As this is meant to run after midnight, it needs to handle yesterday's folder and files
$Yesterday = '\\SERVERNAME\media\pictures\weathercam\'+((Get-Date).AddDays(-1).ToString('yyyyMMdd'))
#Get list of all of yesterday's files sorted by last write time
$Pictures = GCI $Yesterday\*Camera02.jpg | ?{$_.Length -gt 0} | sort-object -property LastWriteTime
#Since FFMPEG needs a numerically indexed file naming scheme, we are copying the files in chronological order to the staging area in the format #####.jpg
For ($i=0; ($i -lt ($Pictures.count)); $i++){copy-item -path ($Yesterday + "\" + ($Pictures[$i].Name)) -destination (${Staging} + ("{0:D5}" -f $i) + ".jpg") -force}
#Create the arguemtns to be fed to FFMPEG for both the HQ and streaming versions of the video
$web_arguments = "-framerate 15 -y -v quiet -i $staging\%05d.JPG -c`:v libx264 -crf 20 -r 15 -vf scale=640:-1 ${Yesterday}Cam02Day_web.mp4"
$arguments = "-framerate 15 -y -v quiet -i $staging\%05d.JPG -c`:v libx264 -crf 20 -r 15 ${Yesterday}Cam02Day.mp4"
#Process the video creation
Start-Process -FilePath $ffmpeg -Argumentlist $arguments -Wait
Start-Process -FilePath $ffmpeg -Argumentlist $web_arguments -Wait
#Remvoe the content from the staging area once complete
remove-item $staging\*.jpg
#FTP Section
#We are now going to FTP the web content versions of yesterday's videos to the FTP server for web hosting
$LocalFile01 = "${Yesterday}Cam01Day_Web.mp4"
$FileName01 = ((Get-Date).AddDays(-1).ToString('yyyyMMdd'))+'Cam01Day_web.mp4'
$LocalFile02 = "${Yesterday}Cam02Day_Web.mp4"
$FileName02 = ((Get-Date).AddDays(-1).ToString('yyyyMMdd'))+'Cam02Day_web.mp4'
$ftp = 'ftp://ftp.SOME_FTP_SERVER.com/'
$user = 'ftp_user'
$pass = 'ftp_password'
#Upload the files via FTP to the server for web hosting purposes
$wc = New-Object System.Net.WebClient
$wc.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$uri01 = New-Object System.Uri($ftp+$FileName01)
$uri02 = New-Object System.Uri($ftp+$FileName02)
$wc.UploadFile($uri01,$LocalFile01)
$wc.UploadFile($uri02,$LocalFile02)
$uri=[system.URI] $ftp
$ftp=[system.net.ftpwebrequest]::Create($uri)
$ftp.Credentials=New-Object System.Net.NetworkCredential($user,$pass)
#Get a list of files in the current directory.
#Use ListDirectoryDetails instead if you need date, size and other additional file information.
$ftp.Method=[system.net.WebRequestMethods+ftp]::ListDirectory
$ftp.UsePassive=$true
try
{
$response=$ftp.GetResponse()
$strm=$response.GetResponseStream()
$reader=New-Object System.IO.StreamReader($strm,'UTF-8')
$list=$reader.ReadToEnd()
$lines=$list.Split("`n")
}
catch
{
$_|fl * -Force
}
#Create an array containing the names of all of the files already on the FTP server
$FileListarray = @()
foreach ($line in $lines)
{
if ($line.contains('.mp4'))
{
#TRIM the file_name because if it contains blank spaces you will get errors when creating the file
$file_name = $line.ToString().Trim()
$FileListArray += $file_name
}
}
$FileListarray = $FileListArray | Sort-Object -Descending
#If there are more than one week's worth of files (more than 14 files, 7 for each camera) we want to delete the oldest so as not let the files pile up on the server
If ($FileListArray.Count -gt 14)
{
$i = 15
For ($i=15; $i -le ($FileListArray.Count); $i++)
{
$file = 'ftp://ftp.SOME_FTP_SERVER.com/'+($FileListArray[($i-1)])
$ftpuri=[system.URI] $file
$request = [system.Net.FtpWebRequest]::Create($ftpuri)
$request.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$request.Method = [System.Net.WebRequestMethods+FTP]::DeleteFile
$response = $request.GetResponse()
#Next line for debugging
#"Deleted from FTP: " $FileListArray[($i-1)] -f $response.StatusDescription
$response.Close()
}
}