-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathYouTube.ps1
498 lines (425 loc) · 16 KB
/
YouTube.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
function Add-YouTube-Video
{
<#
.SYNOPSIS
Uploads a video to YouTube.
.DESCRIPTION
Uploada a video to YouTube via the YouTube API v3 and sets privacy and various other parameters.
.EXAMPLE
Add-YouTube-Video -File "my_video.mp4" -Titel "My Video" -CategoryID 1 -PrivacyStatus 'public'
.PARAMETER File
Path to the video that shall be uploaded.
.PARAMETER Title
Title of the video.
.PARAMETER CategoryId
Sets the video category via an ID.
For possible IDs check:
https://developers.google.com/youtube/v3/docs/videoCategories/list
.PARAMETER Description
Description of the video.
.PARAMETER Tags
A list of tags for the video.
.PARAMETER PrivacyStatus
Sets the video privacy settings.
Valid values are 'private', 'public' and 'unlisted'.
.PARAMETER PublicStatsViewable
Hides the video statistics (view count, etc.) for the video.
.PARAMETER Dimension
Marks the uploaded video as '2D' or '3D' recording.
.PARAMETER Definition
Marks the uploaded video as 'HD' or 'SD' recording.
.PARAMETER LocationDescription
Textual description of where the video was recorded.
.PARAMETER LocationCoordinates
Coordinates from where the video was recorded.
.PARAMETER RecordingDate
The date the video was recorded on.
Requires the 'yyyy-MM-dd' date format.
.PARAMETER DisableComments
Disables comments for this video
Uses the deprecated YouTube API v2
.PARAMETER DisableRating
Disables rating for this video
Uses the deprecated YouTube API v2
.PARAMETER DisableVideoResponds
Disables videos responds for this video
Uses the deprecated YouTube API v2
.PARAMETER EnableProgressBar
Displays a progress bar using 'Write-Progress'
.OUTPUTS
YouTube video ID of the uploaded video
.NOTES
It is necessary to supply a client_secrets.json file that should be located in the same folder as the .exe and .ps1 file.
You need to generate your own client secret file.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=0)]
[ValidateScript({Test-Path $_.Replace("\\","\")})]
[string]$File,
[Parameter(Mandatory=$True,Position=1)]
[string]$Title,
[Parameter(Mandatory=$False,Position=2)]
[int]$CategoryID = 22,
[Parameter(Mandatory=$False,Position=3)]
[string]$Description,
[Parameter(Mandatory=$False,Position=4)]
[string[]]$Tags,
[ValidateSet('unlisted','private','public')]
[Parameter(Mandatory=$False,Position=5)]
[string]$PrivacyStatus = 'private',
[Parameter(Mandatory=$False,Position=6)]
[bool]$PublicStatsViewable,
[ValidateSet('2D','3D')]
[Parameter(Mandatory=$False,Position=7)]
[string]$Dimension,
[ValidateSet('HD','SD')]
[Parameter(Mandatory=$False,Position=8)]
[string]$Definition,
[Parameter(Mandatory=$False,Position=9)]
[string]$LocationDescription,
[Parameter(Mandatory=$False,Position=10)]
[System.Numerics.Vector2]$LocationCoordinates,
[Parameter(Mandatory=$False,Position=11)]
[ValidateScript({[datetime]::ParseExact($_, 'yyyy-MM-dd', $null)})]
[string]$RecordingDate,
[Obsolete("Not possible with YouTube API v3")]
[Parameter(Mandatory=$False,Position=12)]
[bool]$DisableComment,
[Obsolete("Not possible with YouTube API v3")]
[Parameter(Mandatory=$False,Position=13)]
[bool]$DisableRating,
[Obsolete("Not possible with YouTube API v3")]
[Parameter(Mandatory=$False,Position=14)]
[bool]$DisableVideoRespond,
[Parameter(Mandatory=$False, Position=15)]
[switch]$EnableProgressBar
)
$File = (Resolve-Path $File.Replace("\\","\")).Path;
$YouTubeCLI = (Resolve-Path (join-path $PSScriptRoot "YoutubeCLI.exe")).Path;
$YouTubeCLI_Params = "-Mode Video -Operation Add -File ""$File"" -Title ""$Title"" -CategoryID $CategoryID";
if($PSBoundParameters.ContainsKey('Description')) {
$YouTubeCLI_Params += " -Description ""$Description""";
}
if($PSBoundParameters.ContainsKey('Tags')) {
$YouTubeCLI_Params += " -Tags ""$($Tags -join ",")""";
}
if($PSBoundParameters.ContainsKey('PrivacyStatus')) {
$YouTubeCLI_Params += " -PrivacyStatus $PrivacyStatus";
}
if($PSBoundParameters.ContainsKey('PublicStatsViewable')) {
$YouTubeCLI_Params += " -PublicStatsViewable $PublicStatsViewable";
}
if($PSBoundParameters.ContainsKey('Dimension')) {
$YouTubeCLI_Params += " -Dimension $Dimension";
}
if($PSBoundParameters.ContainsKey('Definition')) {
$YouTubeCLI_Params += " -Definition $Definition";
}
if($PSBoundParameters.ContainsKey('LocationDescription')) {
$YouTubeCLI_Params += " -LocationDescription ""$LocationDescription""";
}
if($PSBoundParameters.ContainsKey('LocationCoordinates')) {
$YouTubeCLI_Params += " -Location_Latitude $($LocationCoordinates.X)";
$YouTubeCLI_Params += " -Location_Longitude $($LocationCoordinates.Y)";
}
if($PSBoundParameters.ContainsKey('Location_Longitude')) {
}
if($PSBoundParameters.ContainsKey('RecordingDate')) {
$YouTubeCLI_Params += " -RecordingDate $RecordingDate";
}
if($PSBoundParameters.ContainsKey('DisableComment')) {
$YouTubeCLI_Params += " -DisableComment $DisableComment";
}
if($PSBoundParameters.ContainsKey('DisableRating')) {
$YouTubeCLI_Params += " -DisableRating $DisableRating";
}
if($PSBoundParameters.ContainsKey('DisableVideoRespond')) {
$YouTubeCLI_Params += " -DisableVideoRespond $DisableVideoRespond";
}
$MaxRetry = 5;
$NumRetrys = 0;
$VideoId = "";
while($NumRetrys -lt $MaxRetry)
{
$NumRetrys = $NumRetrys + 1;
if($EnableProgressBar)
{
Write-Progress -Activity "Uploading to YouTube" -Status "[Attempt: $NumRetrys] $Title" -PercentComplete 0;
}
$p = New-Object System.Diagnostics.Process;
$p.StartInfo.Filename = "$YouTubeCLI";
$p.StartInfo.Arguments = "$YouTubeCLI_Params";
$p.StartInfo.UseShellExecute = $false;
$p.StartInfo.RedirectStandardOutput = $true;
$p.StartInfo.CreateNoWindow = $true;
$p.Start() | Out-Null;
while (-not $p.HasExited)
{
if ($p.StandardOutput.Peek())
{
$line = $p.StandardOutput.ReadLineAsync().Result;
if ($line)
{
$info = $line.Trim();
if ($info.StartsWith("Percent complete: "))
{
if($EnableProgressBar)
{
[int32]$progperc = [Math]::Floor([decimal](($info.Split("|")[0]).Substring(18).Replace(",",".")));
if ($progperc -gt $PrevProgress)
{
Write-Progress -Activity "Uploading to YouTube" -Status "[Attempt: $NumRetrys] $Title" -PercentComplete $progperc;
$PrevProgress = $progperc;
}
}
}
elseif ($info.StartsWith("VideoID: "))
{
$VideoId = $line.Replace("VideoID: ","").Trim();
}
}
}
}
if($EnableProgressBar)
{
Write-Progress -Activity "Uploading to YouTube" -Status "[Attempt: $NumRetrys] $Title" -PercentComplete 100 -Completed;
}
$p.WaitForExit();
if (($p.ExitCode -ne 0) -or ($yt_video_id -eq ""))
{
if ($VideoId -ne "")
{
Start-Process -FilePath "$youtubeCLI" -ArgumentList "-Mode Video -Operation Remove -VideoID ""$yt_video_id""" -WindowStyle Hidden -Wait -PassThru;
}
continue;
}
break;
}
if($NumRetrys -ge $MaxRetry)
{
throw "YouTube upload canceled after $MaxRetry attempts";
}
return $VideoId;
}
function Add-YouTube-Playlist
{
<#
.SYNOPSIS
Creates a YouTube playlist
.DESCRIPTION
Creates a YouTube playlist via the YouTube API v3 and sets privacy and various other parameters.
.EXAMPLE
Add-YouTube-Playlist -VideoIDs ('oHg5SJYRHA0','boPyHl3iptQ') -Titel "Great videos!" -PrivacyStatus 'public'
.PARAMETER VideoIDs
List of VideoIDs that shall be added to the playlist.
.PARAMETER Title
Title of the playlist.
.PARAMETER Description
Description of the playlist.
.PARAMETER PrivacyStatus
Sets the playlist privacy settings.
Valid values are 'private', 'public' and 'unlisted'.
.OUTPUTS
YouTube playlist ID
.NOTES
It is necessary to supply a client_secrets.json file that should be located in the same folder as the .exe and .ps1 file.
You need to generate your own client secret file.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=0)]
[ValidateScript({$_.Length -ge 1})]
[string[]]$VideoIDs,
[Parameter(Mandatory=$True,Position=1)]
[string]$Title,
[Parameter(Mandatory=$False,Position=2)]
[string]$Description,
[ValidateSet('unlisted','private','public')]
[Parameter(Mandatory=$False,Position=3)]
[string]$PrivacyStatus
)
$YouTubeCLI = (Resolve-Path (join-path $PSScriptRoot "YoutubeCLI.exe")).Path;
$YouTubeCLI_Params = "-Mode Playlist -Operation Add -VideoIDs ""$($VideoIDs -join '/')"" -Title ""$Title""";
if($PSBoundParameters.ContainsKey('Description')) {
$YouTubeCLI_Params += " -Description ""$Description""";
}
if($PSBoundParameters.ContainsKey('PrivacyStatus')) {
$YouTubeCLI_Params += " -PrivacyStatus $PrivacyStatus";
}
$p = New-Object System.Diagnostics.Process;
$p.StartInfo.Filename = "$YouTubeCLI";
$p.StartInfo.Arguments = "$YouTubeCLI_Params";
$p.StartInfo.UseShellExecute = $false;
$p.StartInfo.RedirectStandardOutput = $true;
$p.StartInfo.CreateNoWindow = $true;
$p.Start() | Out-Null;
$p.WaitForExit();
if ($p.ExitCode -ne 0)
{
throw "YouTube playlist creation failed";
}
else
{
$p_out = $p.StandardOutput.ReadToEnd();
$p_out = $p_out.Split("`n");
foreach ($line in $p_out)
{
if ($line.StartsWith('PlaylistID: '))
{
return $line.Replace("PlaylistID: ", "").Trim();
}
}
throw "YouTube playlist creation failed";
}
}
function Remove-YouTube-Video
{
<#
.SYNOPSIS
Deletes a YouTube video
.DESCRIPTION
Deletes a YouTube video via the YouTube API v3.
.EXAMPLE
Remove-YouTube-Video -VideoID 'deadbeef'
.PARAMETER VideoID
ID the video that shall be deleted.
.NOTES
It is necessary to supply a client_secrets.json file that should be located in the same folder as the .exe and .ps1 file.
You need to generate your own client secret file.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=0)]
[string]$VideoID
)
$YouTubeCLI = (Resolve-Path (join-path $PSScriptRoot "YoutubeCLI.exe")).Path;
$YouTubeCLI_Params = "-Mode Video -Operation Remove -VideoID ""$VideoID""";
$p = New-Object System.Diagnostics.Process;
$p.StartInfo.Filename = "$YouTubeCLI";
$p.StartInfo.Arguments = "$YouTubeCLI_Params";
$p.StartInfo.UseShellExecute = $false;
$p.StartInfo.RedirectStandardOutput = $true;
$p.StartInfo.CreateNoWindow = $true;
$p.Start() | Out-Null;
$p.WaitForExit();
if ($p.ExitCode -ne 0)
{
throw "YouTube video deletion failed";
}
}
function Remove-YouTube-Playlist
{
<#
.SYNOPSIS
Deletes a YouTube playlist
.DESCRIPTION
Deletes a YouTube playlist via the YouTube API v3.
.EXAMPLE
Remove-YouTube-Playlist -PlaylistID 'deadbeef'
.PARAMETER PlaylistID
ID the playlist that shall be deleted.
.NOTES
It is necessary to supply a client_secrets.json file that should be located in the same folder as the .exe and .ps1 file.
You need to generate your own client secret file.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=0)]
[string]$PlaylistID
)
$YouTubeCLI = (Resolve-Path (join-path $PSScriptRoot "YoutubeCLI.exe")).Path;
$YouTubeCLI_Params = "-Mode Playlist -Operation Remove -PlaylistID ""$PlaylistID""";
$p = New-Object System.Diagnostics.Process;
$p.StartInfo.Filename = "$YouTubeCLI";
$p.StartInfo.Arguments = "$YouTubeCLI_Params";
$p.StartInfo.UseShellExecute = $false;
$p.StartInfo.RedirectStandardOutput = $true;
$p.StartInfo.CreateNoWindow = $true;
$p.Start() | Out-Null;
$p.WaitForExit();
if ($p.ExitCode -ne 0)
{
throw "YouTube playlist deletion failed";
}
}
function Include-YouTube-Playlist
{
<#
.SYNOPSIS
Adds new videos to an existing YouTube playlist
.DESCRIPTION
Appends multiple videos to the an existing YouTube playlist via the YouTube API v3.
.EXAMPLE
Include-YouTube-Playlist -PlaylistID 'deadbeef' -VideoIDs ('oHg5SJYRHA0','boPyHl3iptQ')
.PARAMETER PlaylistID
ID the playlist that shall be modified.
.PARAMETER VideoIDs
List of VideoIDs that shall be added to the playlist.
.NOTES
It is necessary to supply a client_secrets.json file that should be located in the same folder as the .exe and .ps1 file.
You need to generate your own client secret file.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=0)]
[string]$PlaylistID,
[Parameter(Mandatory=$True,Position=1)]
[ValidateScript({$_.Length -ge 1})]
[string[]]$VideoIDs
)
$YouTubeCLI = (Resolve-Path (join-path $PSScriptRoot "YoutubeCLI.exe")).Path;
$YouTubeCLI_Params = "-Mode Playlist -Operation Include -PlaylistID ""$PlaylistID"" -VideoIDs ""$($VideoIDs -join '/')""";
$p = New-Object System.Diagnostics.Process;
$p.StartInfo.Filename = "$YouTubeCLI";
$p.StartInfo.Arguments = "$YouTubeCLI_Params";
$p.StartInfo.UseShellExecute = $false;
$p.StartInfo.RedirectStandardOutput = $true;
$p.StartInfo.CreateNoWindow = $true;
$p.Start() | Out-Null;
$p.WaitForExit();
if ($p.ExitCode -ne 0)
{
throw "YouTube playlist inclusion failed";
}
}
function Exclude-YouTube-Playlist
{
<#
.SYNOPSIS
Removes videos from a YouTube playlist
.DESCRIPTION
Removes multiple videos from a YouTube playlist via the YouTube API v3.
.EXAMPLE
Exclude-YouTube-Playlist -PlaylistID 'deadbeef' -VideoIDs ('oHg5SJYRHA0','boPyHl3iptQ')
.PARAMETER PlaylistID
ID the playlist that shall be modified.
.PARAMETER VideoIDs
List of VideoIDs that shall be removed from the playlist.
.NOTES
It is necessary to supply a client_secrets.json file that should be located in the same folder as the .exe and .ps1 file.
You need to generate your own client secret file.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=0)]
[string]$PlaylistID,
[Parameter(Mandatory=$True,Position=1)]
[ValidateScript({$_.Length -ge 1})]
[string[]]$VideoIDs
)
$YouTubeCLI = (Resolve-Path (join-path $PSScriptRoot "YoutubeCLI.exe")).Path;
$YouTubeCLI_Params = "-Mode Playlist -Operation Exclude -PlaylistID ""$PlaylistID"" -VideoIDs ""$($VideoIDs -join '/')""";
$p = New-Object System.Diagnostics.Process;
$p.StartInfo.Filename = "$YouTubeCLI";
$p.StartInfo.Arguments = "$YouTubeCLI_Params";
$p.StartInfo.UseShellExecute = $false;
$p.StartInfo.RedirectStandardOutput = $true;
$p.StartInfo.CreateNoWindow = $true;
$p.Start() | Out-Null;
$p.WaitForExit();
if ($p.ExitCode -ne 0)
{
throw "YouTube playlist exclusion failed";
}
}