0
    import webbrowser
    import os
    import urlparse
    import urllib
    webbrowser.open('https://www.youtube.com/watch?v=SC4xMk98Pdc')
    webbrowser.open('https://www.youtube.com/watch?v=SC4xMk98Pdc')

this code immediately plays the second URL in my browser. i want the code to wait till first video stops .So i need play time of the video from the page and set it in sleep function so how do i do this. or there is another way to do this.

Thanks in advance.

Mahipalsaran
  • 81
  • 1
  • 3

2 Answers2

1

you can use time

import time
time.sleep(secs)
Achraf
  • 1,412
  • 10
  • 14
1

To determine the length of a video you need to use the YouTube Data API. You can refer to this Stack Overflow answer, which gives an example of how to make a call to the API

For example the following call:

https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY}

Gives this result:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/ny1S4th-ku477VARrY_U4tIqcTw\"",
 "items": [
  {
   "id": "9bZkp7q19f0",
   "kind": "youtube#video",
   "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/HN8ILnw-DBXyCcTsc7JG0z51BGg\"",
   "contentDetails": {
    "duration": "PT4M13S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": true,
    "regionRestriction": {
     "blocked": [
      "DE"
     ]
    }
   }
  }
 ]
}

The time is formatted as an ISO 8601 string. PT stands for Time Duration, 4M is 4 minutes, and 13S is 13 seconds.

You can then use time.sleep(secs) to sleep for the number of secs returned from the API.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Tim
  • 2,123
  • 4
  • 27
  • 44
  • Please do not use the `>` character for custom formatting. The (improper) use of the `>` character [has been discussed on meta](https://meta.stackoverflow.com/q/355183/1415724) and I had to edit those from the answer. – Funk Forty Niner Aug 20 '17 at 21:09