- # The "itag" parameter makes the 403 happen.
- video_url.gsub!(/itag=\d+&/, '')
+ # A duplicated "itag" parameter results in a 403.
+ itag_regex = /&itag=\d+/
+ matches = video_url.scan(itag_regex)
+
+ if matches.length > 1
+ # Get rid of the first occurrence.
+ video_url.sub!(itag_regex, '')
+ end