Skip to content

Instantly share code, notes, and snippets.

@C2N14
Last active April 16, 2022 08:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save C2N14/339914ffab6cf16994f2f63e80dd1400 to your computer and use it in GitHub Desktop.
Save C2N14/339914ffab6cf16994f2f63e80dd1400 to your computer and use it in GitHub Desktop.
[YouTube API V3] New simple Powershell script to get the duration of a YouTube video
#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