I have a web server which uses youtube_dl.extract_info to get video length. It works well for long time, but begin to fail in last 3 months. After upgrading to (v2021.06.06), it still fails. See sample code below.
import youtube_dl
def get_duration(self, yt_video_id):
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'tmp/%(id)s.%(ext)s',
'noplaylist': True,
'quiet': True,
'prefer_ffmpeg': True,
'audioformat': 'wav',
'forceduration':True
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
ret_dict = ydl.extract_info(
"https://www.youtube.com/watch?v={sID}".format(sID=yt_video_id),
download=True)
except Exception as ex:
err = 'Exception: get_duration. ' + str(ex)
print(err)
return ret_dict['duration']
When it fails, "except Exception as ex" block does not catch the exception.
The get video length code is in a rest API with Ajax in UI. Ajax error block catches the error with followings:
<head><title>502 Bad Gateway</title></head>
...
<h2><center>nginx/1.14.0 (Ubuntu)</center></h2>
===Javascript code
let defer = this.myRestApiCall('POST',...);
defer
.done(function(data) {
// code here to handle success.
}.bind(this))
.fail(function(data) {
alert('Save failed!' + data.responseText)
}.bind(this))
If running same code NOT behind nginx, it works.
Questions:
- How to solve it?
- Why Python catch block does not catch the error?