-
-
Save iqbmo04/85da73ddbf85b9faff0ef27de6ae39a8 to your computer and use it in GitHub Desktop.
[YouTube API V3] New simple Powershell script to get the duration of a YouTube video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Powershell 4+ | |
| #Fix of ShinNoNoir's script that broke with the new API | |
| #https://gist.github.com/ShinNoNoir/d59ca5da3cd5c554a832 | |
| function Get-VideoSeconds ([string]$VideoID){ | |
| $gdata_uri = "https://www.googleapis.com/youtube/v3/videos?id=$VideoId&key=<APIKEY>&part=contentDetails" #Replace <APIKEY> | |
| $metadata = irm $gdata_uri | |
| $duration = $metadata.items.contentDetails.duration; | |
| $ts = [Xml.XmlConvert]::ToTimeSpan("$duration") | |
| '{0:00},{1:00},{2:00}.{3:00}' -f ($ts.Hours+$ts.Days*24), $ts.Minutes, $ts.Seconds, $ts.Milliseconds | Out-Null #To prevent output | |
| $timespan = [TimeSpan]::Parse($ts) | |
| $totalSeconds = $timespan.TotalSeconds | |
| $totalSeconds | |
| } | |
| #Function test | |
| Get-VideoSeconds -VideoID dQw4w9WgXcQ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment