Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
a0d10e6a00
|
|||
|
a46cfda029
|
|||
|
e03f40d728
|
|||
|
22c72aa842
|
|||
|
56ecd6cb1b
|
|||
|
f629565e77
|
|||
|
1f8c13adff
|
12
.build.yml
12
.build.yml
@@ -1,12 +0,0 @@
|
|||||||
image: debian/buster
|
|
||||||
packages:
|
|
||||||
- python3-pip
|
|
||||||
- virtualenv
|
|
||||||
tasks:
|
|
||||||
- test: |
|
|
||||||
cd yt-local
|
|
||||||
virtualenv -p python3 venv
|
|
||||||
source venv/bin/activate
|
|
||||||
python --version
|
|
||||||
pip install -r requirements-dev.txt
|
|
||||||
pytest
|
|
||||||
10
.drone.yml
10
.drone.yml
@@ -1,10 +0,0 @@
|
|||||||
kind: pipeline
|
|
||||||
name: default
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: test
|
|
||||||
image: python:3.7.3
|
|
||||||
commands:
|
|
||||||
- pip install --upgrade pip
|
|
||||||
- pip install -r requirements-dev.txt
|
|
||||||
- pytest
|
|
||||||
@@ -173,7 +173,6 @@ This project is completely free/Libre and will always be.
|
|||||||
- [NewPipe](https://newpipe.schabi.org/) (app for android)
|
- [NewPipe](https://newpipe.schabi.org/) (app for android)
|
||||||
- [mps-youtube](https://github.com/mps-youtube/mps-youtube) (terminal-only program)
|
- [mps-youtube](https://github.com/mps-youtube/mps-youtube) (terminal-only program)
|
||||||
- [youtube-viewer](https://github.com/trizen/youtube-viewer)
|
- [youtube-viewer](https://github.com/trizen/youtube-viewer)
|
||||||
- [FreeTube](https://github.com/FreeTubeApp/FreeTube) (Similar to this project, but is an electron app outside the browser)
|
|
||||||
- [smtube](https://www.smtube.org/)
|
- [smtube](https://www.smtube.org/)
|
||||||
- [Minitube](https://flavio.tordini.org/minitube), [github here](https://github.com/flaviotordini/minitube)
|
- [Minitube](https://flavio.tordini.org/minitube), [github here](https://github.com/flaviotordini/minitube)
|
||||||
- [toogles](https://github.com/mikecrittenden/toogles) (only embeds videos, doesn't use mp4)
|
- [toogles](https://github.com/mikecrittenden/toogles) (only embeds videos, doesn't use mp4)
|
||||||
|
|||||||
@@ -8,5 +8,4 @@ urllib3>=1.24.1
|
|||||||
defusedxml>=0.5.0
|
defusedxml>=0.5.0
|
||||||
cachetools>=4.0.0
|
cachetools>=4.0.0
|
||||||
stem>=1.8.0
|
stem>=1.8.0
|
||||||
yt-dlp>=2026.01.01
|
|
||||||
requests>=2.25.0
|
requests>=2.25.0
|
||||||
|
|||||||
@@ -99,7 +99,6 @@ def proxy_site(env, start_response, video=False):
|
|||||||
if response.status >= 400:
|
if response.status >= 400:
|
||||||
print('Error: YouTube returned "%d %s" while routing %s' % (
|
print('Error: YouTube returned "%d %s" while routing %s' % (
|
||||||
response.status, response.reason, url.split('?')[0]))
|
response.status, response.reason, url.split('?')[0]))
|
||||||
|
|
||||||
total_received = 0
|
total_received = 0
|
||||||
retry = False
|
retry = False
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
12
settings.py
12
settings.py
@@ -340,15 +340,6 @@ Archive: https://archive.ph/OZQbN''',
|
|||||||
'hidden': True,
|
'hidden': True,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
('ytdlp_enabled', {
|
|
||||||
'type': bool,
|
|
||||||
'default': True,
|
|
||||||
'comment': '''Enable yt-dlp integration for multi-language audio and subtitles''',
|
|
||||||
'hidden': False,
|
|
||||||
'label': 'Enable yt-dlp integration',
|
|
||||||
'category': 'playback',
|
|
||||||
}),
|
|
||||||
|
|
||||||
('settings_version', {
|
('settings_version', {
|
||||||
'type': int,
|
'type': int,
|
||||||
'default': 6,
|
'default': 6,
|
||||||
@@ -359,7 +350,8 @@ Archive: https://archive.ph/OZQbN''',
|
|||||||
|
|
||||||
program_directory = os.path.dirname(os.path.realpath(__file__))
|
program_directory = os.path.dirname(os.path.realpath(__file__))
|
||||||
acceptable_targets = SETTINGS_INFO.keys() | {
|
acceptable_targets = SETTINGS_INFO.keys() | {
|
||||||
'enable_comments', 'enable_related_videos', 'preferred_video_codec'
|
'enable_comments', 'enable_related_videos', 'preferred_video_codec',
|
||||||
|
'ytdlp_enabled',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from flask import request
|
|||||||
import jinja2
|
import jinja2
|
||||||
import settings
|
import settings
|
||||||
import traceback
|
import traceback
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
from sys import exc_info
|
from sys import exc_info
|
||||||
from flask_babel import Babel
|
from flask_babel import Babel
|
||||||
@@ -12,6 +13,15 @@ from flask_babel import Babel
|
|||||||
yt_app = flask.Flask(__name__)
|
yt_app = flask.Flask(__name__)
|
||||||
yt_app.config['TEMPLATES_AUTO_RELOAD'] = True
|
yt_app.config['TEMPLATES_AUTO_RELOAD'] = True
|
||||||
yt_app.url_map.strict_slashes = False
|
yt_app.url_map.strict_slashes = False
|
||||||
|
|
||||||
|
# Don't log full tracebacks for handled FetchErrors
|
||||||
|
class FetchErrorFilter(logging.Filter):
|
||||||
|
def filter(self, record):
|
||||||
|
if record.exc_info and record.exc_info[0] == util.FetchError:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
yt_app.logger.addFilter(FetchErrorFilter())
|
||||||
# yt_app.jinja_env.trim_blocks = True
|
# yt_app.jinja_env.trim_blocks = True
|
||||||
# yt_app.jinja_env.lstrip_blocks = True
|
# yt_app.jinja_env.lstrip_blocks = True
|
||||||
|
|
||||||
@@ -124,49 +134,54 @@ def timestamps(text):
|
|||||||
@yt_app.errorhandler(500)
|
@yt_app.errorhandler(500)
|
||||||
def error_page(e):
|
def error_page(e):
|
||||||
slim = request.args.get('slim', False) # whether it was an ajax request
|
slim = request.args.get('slim', False) # whether it was an ajax request
|
||||||
if (exc_info()[0] == util.FetchError
|
if exc_info()[0] == util.FetchError:
|
||||||
and exc_info()[1].code == '429'
|
fetch_err = exc_info()[1]
|
||||||
and settings.route_tor
|
error_code = fetch_err.code
|
||||||
):
|
|
||||||
|
if error_code == '429' and settings.route_tor:
|
||||||
error_message = ('Error: YouTube blocked the request because the Tor'
|
error_message = ('Error: YouTube blocked the request because the Tor'
|
||||||
' exit node is overutilized. Try getting a new exit node by'
|
' exit node is overutilized. Try getting a new exit node by'
|
||||||
' using the New Identity button in the Tor Browser.')
|
' using the New Identity button in the Tor Browser.')
|
||||||
if exc_info()[1].error_message:
|
if fetch_err.error_message:
|
||||||
error_message += '\n\n' + exc_info()[1].error_message
|
error_message += '\n\n' + fetch_err.error_message
|
||||||
if exc_info()[1].ip:
|
if fetch_err.ip:
|
||||||
error_message += '\n\nExit node IP address: ' + exc_info()[1].ip
|
error_message += '\n\nExit node IP address: ' + fetch_err.ip
|
||||||
return flask.render_template('error.html', error_message=error_message, slim=slim), 502
|
return flask.render_template('error.html', error_message=error_message, slim=slim), 502
|
||||||
elif exc_info()[0] == util.FetchError and exc_info()[1].error_message:
|
|
||||||
# Handle specific error codes with user-friendly messages
|
|
||||||
error_code = exc_info()[1].code
|
|
||||||
error_msg = exc_info()[1].error_message
|
|
||||||
|
|
||||||
if error_code == '400':
|
elif error_code == '429':
|
||||||
error_message = (f'Error: Bad Request (400)\n\n{error_msg}\n\n'
|
error_message = ('YouTube is temporarily blocking requests from your IP address (429 Too Many Requests).\n\n'
|
||||||
'This usually means the URL or parameters are invalid. '
|
'Try:\n'
|
||||||
'Try going back and trying a different option.')
|
'• Wait a few minutes and refresh\n'
|
||||||
|
'• Enable Tor routing in Settings for automatic IP rotation\n'
|
||||||
|
'• Use a VPN to change your IP address')
|
||||||
|
if fetch_err.ip:
|
||||||
|
error_message += '\n\nYour IP: ' + fetch_err.ip
|
||||||
|
return flask.render_template('error.html', error_message=error_message, slim=slim), 429
|
||||||
|
|
||||||
|
elif error_code == '502' and ('Failed to resolve' in str(fetch_err) or 'Failed to establish' in str(fetch_err)):
|
||||||
|
error_message = ('Could not connect to YouTube.\n\n'
|
||||||
|
'Check your internet connection and try again.')
|
||||||
|
return flask.render_template('error.html', error_message=error_message, slim=slim), 502
|
||||||
|
|
||||||
|
elif error_code == '403':
|
||||||
|
error_message = ('YouTube blocked this request (403 Forbidden).\n\n'
|
||||||
|
'Try enabling Tor routing in Settings.')
|
||||||
|
return flask.render_template('error.html', error_message=error_message, slim=slim), 403
|
||||||
|
|
||||||
elif error_code == '404':
|
elif error_code == '404':
|
||||||
error_message = 'Error: The page you are looking for isn\'t here.'
|
error_message = 'Error: The page you are looking for isn\'t here.'
|
||||||
else:
|
return flask.render_template('error.html', error_code=error_code,
|
||||||
error_message = f'Error: {error_code} - {error_msg}'
|
error_message=error_message, slim=slim), 404
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Catch-all for any other FetchError (400, etc.)
|
||||||
|
error_message = f'Error communicating with YouTube ({error_code}).'
|
||||||
|
if fetch_err.error_message:
|
||||||
|
error_message += '\n\n' + fetch_err.error_message
|
||||||
|
return flask.render_template('error.html', error_message=error_message, slim=slim), 502
|
||||||
|
|
||||||
return (flask.render_template(
|
|
||||||
'error.html',
|
|
||||||
error_message=error_message,
|
|
||||||
slim=slim
|
|
||||||
), 502)
|
|
||||||
elif (exc_info()[0] == util.FetchError
|
|
||||||
and exc_info()[1].code == '404'
|
|
||||||
):
|
|
||||||
error_message = ('Error: The page you are looking for isn\'t here.')
|
|
||||||
return flask.render_template('error.html',
|
|
||||||
error_code=exc_info()[1].code,
|
|
||||||
error_message=error_message,
|
|
||||||
slim=slim), 404
|
|
||||||
return flask.render_template('error.html', traceback=traceback.format_exc(),
|
return flask.render_template('error.html', traceback=traceback.format_exc(),
|
||||||
error_code=exc_info()[1].code,
|
|
||||||
slim=slim), 500
|
slim=slim), 500
|
||||||
# return flask.render_template('error.html', traceback=traceback.format_exc(), slim=slim), 500
|
|
||||||
|
|
||||||
|
|
||||||
font_choices = {
|
font_choices = {
|
||||||
|
|||||||
@@ -406,12 +406,12 @@ def post_process_channel_info(info):
|
|||||||
info['avatar'] = util.prefix_url(info['avatar'])
|
info['avatar'] = util.prefix_url(info['avatar'])
|
||||||
info['channel_url'] = util.prefix_url(info['channel_url'])
|
info['channel_url'] = util.prefix_url(info['channel_url'])
|
||||||
for item in info['items']:
|
for item in info['items']:
|
||||||
# For playlists, use first_video_id for thumbnail, not playlist id
|
# Only set thumbnail if YouTube didn't provide one
|
||||||
|
if not item.get('thumbnail'):
|
||||||
if item.get('type') == 'playlist' and item.get('first_video_id'):
|
if item.get('type') == 'playlist' and item.get('first_video_id'):
|
||||||
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hq720.jpg".format(item['first_video_id'])
|
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hqdefault.jpg".format(item['first_video_id'])
|
||||||
elif item.get('type') == 'video':
|
elif item.get('type') == 'video' and item.get('id'):
|
||||||
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hq720.jpg".format(item['id'])
|
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hqdefault.jpg".format(item['id'])
|
||||||
# For channels and other types, keep existing thumbnail
|
|
||||||
util.prefix_urls(item)
|
util.prefix_urls(item)
|
||||||
util.add_extra_html_info(item)
|
util.add_extra_html_info(item)
|
||||||
if info['current_tab'] == 'about':
|
if info['current_tab'] == 'about':
|
||||||
|
|||||||
@@ -150,7 +150,7 @@ def post_process_comments_info(comments_info):
|
|||||||
util.URL_ORIGIN, '/watch?v=', comments_info['video_id'])
|
util.URL_ORIGIN, '/watch?v=', comments_info['video_id'])
|
||||||
comments_info['video_thumbnail'] = concat_or_none(
|
comments_info['video_thumbnail'] = concat_or_none(
|
||||||
settings.img_prefix, 'https://i.ytimg.com/vi/',
|
settings.img_prefix, 'https://i.ytimg.com/vi/',
|
||||||
comments_info['video_id'], '/hq720.jpg'
|
comments_info['video_id'], '/hqdefault.jpg'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,15 @@ def get_playlist_page():
|
|||||||
abort(400)
|
abort(400)
|
||||||
|
|
||||||
playlist_id = request.args.get('list')
|
playlist_id = request.args.get('list')
|
||||||
|
|
||||||
|
# Radio/Mix playlists (RD...) only work as watch page, not playlist page
|
||||||
|
if playlist_id.startswith('RD'):
|
||||||
|
first_video_id = playlist_id[2:] # video ID after 'RD' prefix
|
||||||
|
return flask.redirect(
|
||||||
|
util.URL_ORIGIN + '/watch?v=' + first_video_id + '&list=' + playlist_id,
|
||||||
|
302
|
||||||
|
)
|
||||||
|
|
||||||
page = request.args.get('page', '1')
|
page = request.args.get('page', '1')
|
||||||
|
|
||||||
if page == '1':
|
if page == '1':
|
||||||
@@ -106,8 +115,8 @@ def get_playlist_page():
|
|||||||
for item in info.get('items', ()):
|
for item in info.get('items', ()):
|
||||||
util.prefix_urls(item)
|
util.prefix_urls(item)
|
||||||
util.add_extra_html_info(item)
|
util.add_extra_html_info(item)
|
||||||
if 'id' in item:
|
if 'id' in item and not item.get('thumbnail'):
|
||||||
item['thumbnail'] = f"{settings.img_prefix}https://i.ytimg.com/vi/{item['id']}/hq720.jpg"
|
item['thumbnail'] = f"{settings.img_prefix}https://i.ytimg.com/vi/{item['id']}/hqdefault.jpg"
|
||||||
|
|
||||||
item['url'] += '&list=' + playlist_id
|
item['url'] += '&list=' + playlist_id
|
||||||
if item['index']:
|
if item['index']:
|
||||||
|
|||||||
@@ -121,11 +121,12 @@ window.addEventListener('DOMContentLoaded', function() {
|
|||||||
* Priority: hq720.jpg -> sddefault.jpg -> hqdefault.jpg -> mqdefault.jpg -> default.jpg
|
* Priority: hq720.jpg -> sddefault.jpg -> hqdefault.jpg -> mqdefault.jpg -> default.jpg
|
||||||
*/
|
*/
|
||||||
function thumbnail_fallback(img) {
|
function thumbnail_fallback(img) {
|
||||||
const src = img.src || img.dataset.src;
|
// Once src is set (image was loaded or attempted), always work with src
|
||||||
|
const src = img.src;
|
||||||
if (!src) return;
|
if (!src) return;
|
||||||
|
|
||||||
// Handle YouTube video thumbnails
|
// Handle YouTube video thumbnails
|
||||||
if (src.includes('/i.ytimg.com/')) {
|
if (src.includes('/i.ytimg.com/') || src.includes('/i.ytimg.com%2F')) {
|
||||||
// Extract video ID from URL
|
// Extract video ID from URL
|
||||||
const match = src.match(/\/vi\/([^/]+)/);
|
const match = src.match(/\/vi\/([^/]+)/);
|
||||||
if (!match) return;
|
if (!match) return;
|
||||||
@@ -138,36 +139,32 @@ function thumbnail_fallback(img) {
|
|||||||
'hq720.jpg',
|
'hq720.jpg',
|
||||||
'sddefault.jpg',
|
'sddefault.jpg',
|
||||||
'hqdefault.jpg',
|
'hqdefault.jpg',
|
||||||
'mqdefault.jpg',
|
|
||||||
'default.jpg'
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// Find current quality and try next fallback
|
// Find current quality and try next fallback
|
||||||
for (let i = 0; i < fallbacks.length; i++) {
|
for (let i = 0; i < fallbacks.length; i++) {
|
||||||
if (src.includes(fallbacks[i])) {
|
if (src.includes(fallbacks[i])) {
|
||||||
// Try next quality
|
|
||||||
if (i < fallbacks.length - 1) {
|
if (i < fallbacks.length - 1) {
|
||||||
const newSrc = imgPrefix + 'https://i.ytimg.com/vi/' + videoId + '/' + fallbacks[i + 1];
|
img.src = imgPrefix + 'https://i.ytimg.com/vi/' + videoId + '/' + fallbacks[i + 1];
|
||||||
if (img.dataset.src) {
|
|
||||||
img.dataset.src = newSrc;
|
|
||||||
} else {
|
} else {
|
||||||
img.src = newSrc;
|
// Last fallback failed, stop retrying
|
||||||
}
|
img.onerror = null;
|
||||||
}
|
}
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Unknown quality format, stop retrying
|
||||||
|
img.onerror = null;
|
||||||
}
|
}
|
||||||
// Handle YouTube channel avatars (ggpht.com)
|
// Handle YouTube channel avatars (ggpht.com)
|
||||||
else if (src.includes('ggpht.com') || src.includes('yt3.ggpht.com')) {
|
else if (src.includes('ggpht.com') || src.includes('yt3.ggpht.com')) {
|
||||||
// Try to increase avatar size (s88 -> s240)
|
|
||||||
const newSrc = src.replace(/=s\d+-c-k/, '=s240-c-k-c0x00ffffff-no-rj');
|
const newSrc = src.replace(/=s\d+-c-k/, '=s240-c-k-c0x00ffffff-no-rj');
|
||||||
if (newSrc !== src) {
|
if (newSrc !== src) {
|
||||||
if (img.dataset.src) {
|
|
||||||
img.dataset.src = newSrc;
|
|
||||||
} else {
|
|
||||||
img.src = newSrc;
|
img.src = newSrc;
|
||||||
|
} else {
|
||||||
|
img.onerror = null;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
img.onerror = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1089,12 +1089,26 @@ def serve_subscription_thumbnail(thumbnail):
|
|||||||
f.close()
|
f.close()
|
||||||
return flask.Response(image, mimetype='image/jpeg')
|
return flask.Response(image, mimetype='image/jpeg')
|
||||||
|
|
||||||
url = f"https://i.ytimg.com/vi/{video_id}/hq720.jpg"
|
image = None
|
||||||
|
for quality in ('hq720.jpg', 'sddefault.jpg', 'hqdefault.jpg'):
|
||||||
|
url = f"https://i.ytimg.com/vi/{video_id}/{quality}"
|
||||||
try:
|
try:
|
||||||
image = util.fetch_url(url, report_text="Saved thumbnail: " + video_id)
|
image = util.fetch_url(url, report_text="Saved thumbnail: " + video_id)
|
||||||
|
break
|
||||||
|
except util.FetchError as e:
|
||||||
|
if '404' in str(e):
|
||||||
|
continue
|
||||||
|
print("Failed to download thumbnail for " + video_id + ": " + str(e))
|
||||||
|
flask.abort(500)
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
|
if e.code == 404:
|
||||||
|
continue
|
||||||
print("Failed to download thumbnail for " + video_id + ": " + str(e))
|
print("Failed to download thumbnail for " + video_id + ": " + str(e))
|
||||||
flask.abort(e.code)
|
flask.abort(e.code)
|
||||||
|
|
||||||
|
if image is None:
|
||||||
|
flask.abort(404)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f = open(thumbnail_path, 'wb')
|
f = open(thumbnail_path, 'wb')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
{% macro render_comment(comment, include_avatar, timestamp_links=False) %}
|
{% macro render_comment(comment, include_avatar, timestamp_links=False) %}
|
||||||
<div class="comment-container">
|
<div class="comment-container">
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<a class="author-avatar" href="{{ comment['author_url'] }}" title="{{ comment['author'] }}">
|
<a class="author-avatar" href="{{ comment['author_url'] or '#' }}" title="{{ comment['author'] }}">
|
||||||
{% if include_avatar %}
|
{% if include_avatar %}
|
||||||
<img class="author-avatar-img" alt="{{ comment['author'] }}" src="{{ comment['author_avatar'] }}">
|
<img class="author-avatar-img" alt="{{ comment['author'] }}" src="{{ comment['author_avatar'] }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
<address class="author-name">
|
<address class="author-name">
|
||||||
<a class="author" href="{{ comment['author_url'] }}" title="{{ comment['author'] }}">{{ comment['author'] }}</a>
|
<a class="author" href="{{ comment['author_url'] or '#' }}" title="{{ comment['author'] }}">{{ comment['author'] }}</a>
|
||||||
</address>
|
</address>
|
||||||
<a class="permalink" href="{{ comment['permalink'] }}" title="permalink">
|
<a class="permalink" href="{{ comment['permalink'] }}" title="permalink">
|
||||||
<span>{{ comment['time_published'] }}</span>
|
<span>{{ comment['time_published'] }}</span>
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
{{ info['error'] }}
|
{{ info['error'] }}
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="item-video {{ info['type'] + '-item' }}">
|
<div class="item-video {{ info['type'] + '-item' }}">
|
||||||
<a class="thumbnail-box" href="{{ info['url'] }}" title="{{ info['title'] }}">
|
<a class="thumbnail-box" href="{{ info['url'] or '#' }}" title="{{ info['title'] }}">
|
||||||
<div class="thumbnail {% if info['type'] == 'channel' %} channel {% endif %}">
|
<div class="thumbnail {% if info['type'] == 'channel' %} channel {% endif %}">
|
||||||
{% if lazy_load %}
|
{% if lazy_load %}
|
||||||
<img class="thumbnail-img lazy" alt=" " data-src="{{ info['thumbnail'] }}" onerror="thumbnail_fallback(this)">
|
<img class="thumbnail-img lazy" alt=" " data-src="{{ info['thumbnail'] }}" onerror="thumbnail_fallback(this)">
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
<h4 class="title"><a href="{{ info['url'] }}" title="{{ info['title'] }}">{{ info['title'] }}</a></h4>
|
<h4 class="title"><a href="{{ info['url'] or '#' }}" title="{{ info['title'] }}">{{ info['title'] }}</a></h4>
|
||||||
|
|
||||||
{% if include_author %}
|
{% if include_author %}
|
||||||
{% set author_description = info['author'] %}
|
{% set author_description = info['author'] %}
|
||||||
|
|||||||
@@ -10,11 +10,17 @@
|
|||||||
|
|
||||||
<div class="playlist-metadata">
|
<div class="playlist-metadata">
|
||||||
<div class="author">
|
<div class="author">
|
||||||
|
{% if thumbnail %}
|
||||||
<img alt="{{ title }}" src="{{ thumbnail }}">
|
<img alt="{{ title }}" src="{{ thumbnail }}">
|
||||||
|
{% endif %}
|
||||||
<h2>{{ title }}</h2>
|
<h2>{{ title }}</h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary">
|
<div class="summary">
|
||||||
|
{% if author_url %}
|
||||||
<a class="playlist-author" href="{{ author_url }}">{{ author }}</a>
|
<a class="playlist-author" href="{{ author_url }}">{{ author }}</a>
|
||||||
|
{% else %}
|
||||||
|
<span class="playlist-author">{{ author }}</span>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<div class="playlist-stats">
|
<div class="playlist-stats">
|
||||||
<div>{{ video_count|commatize }} videos</div>
|
<div>{{ video_count|commatize }} videos</div>
|
||||||
|
|||||||
@@ -86,15 +86,6 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
{% if audio_tracks and audio_tracks|length > 1 %}
|
|
||||||
<select id="audio-language-select" autocomplete="off" title="Audio language">
|
|
||||||
{% for track in audio_tracks %}
|
|
||||||
<option value="{{ track.get('track_id', track['language']) }}" {{ 'selected' if loop.index0 == 0 else '' }}>
|
|
||||||
🔊 {{ track['language_name'] }}{% if track.get('is_default') %} (Default){% endif %}
|
|
||||||
</option>
|
|
||||||
{% endfor %}
|
|
||||||
</select>
|
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<input class="v-checkbox" name="video_info_list" value="{{ video_info }}" form="playlist-edit" type="checkbox">
|
<input class="v-checkbox" name="video_info_list" value="{{ video_info }}" form="playlist-edit" type="checkbox">
|
||||||
@@ -181,7 +172,11 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<li>{{ playlist['current_index']+1 }}/{{ playlist['video_count'] }}</li>
|
<li>{{ playlist['current_index']+1 }}/{{ playlist['video_count'] }}</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if playlist['author_url'] %}
|
||||||
<li><a href="{{ playlist['author_url'] }}" title="{{ playlist['author'] }}">{{ playlist['author'] }}</a></li>
|
<li><a href="{{ playlist['author_url'] }}" title="{{ playlist['author'] }}">{{ playlist['author'] }}</a></li>
|
||||||
|
{% elif playlist['author'] %}
|
||||||
|
<li>{{ playlist['author'] }}</li>
|
||||||
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<nav class="playlist-videos">
|
<nav class="playlist-videos">
|
||||||
@@ -257,37 +252,6 @@
|
|||||||
// @license-end
|
// @license-end
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Audio language selector handler -->
|
|
||||||
<script>
|
|
||||||
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
|
|
||||||
(function() {
|
|
||||||
'use strict';
|
|
||||||
const audioSelect = document.getElementById('audio-language-select');
|
|
||||||
const qualitySelect = document.getElementById('quality-select');
|
|
||||||
|
|
||||||
if (audioSelect && qualitySelect) {
|
|
||||||
audioSelect.addEventListener('change', function() {
|
|
||||||
const selectedAudio = this.value;
|
|
||||||
const selectedQuality = qualitySelect.value;
|
|
||||||
|
|
||||||
// Parse current quality selection
|
|
||||||
let qualityData;
|
|
||||||
try {
|
|
||||||
qualityData = JSON.parse(selectedQuality);
|
|
||||||
} catch(e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload video with new audio language
|
|
||||||
const currentUrl = new URL(window.location.href);
|
|
||||||
currentUrl.searchParams.set('audio_lang', selectedAudio);
|
|
||||||
window.location.href = currentUrl.toString();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}());
|
|
||||||
// @license-end
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script src="/youtube.com/static/js/common.js"></script>
|
<script src="/youtube.com/static/js/common.js"></script>
|
||||||
<script src="/youtube.com/static/js/transcript-table.js"></script>
|
<script src="/youtube.com/static/js/transcript-table.js"></script>
|
||||||
{% if settings.use_video_player == 2 %}
|
{% if settings.use_video_player == 2 %}
|
||||||
|
|||||||
@@ -367,29 +367,26 @@ def fetch_url(url, headers=(), timeout=15, report_text=None, data=None,
|
|||||||
response.getheader('Set-Cookie') or '')
|
response.getheader('Set-Cookie') or '')
|
||||||
ip = ip.group(1) if ip else None
|
ip = ip.group(1) if ip else None
|
||||||
|
|
||||||
# If this is the last attempt, raise error
|
# Without Tor, no point retrying with same IP
|
||||||
if attempt >= max_retries - 1:
|
|
||||||
if not use_tor or not settings.route_tor:
|
if not use_tor or not settings.route_tor:
|
||||||
logger.warning(f'YouTube returned 429 but Tor is not enabled. Consider enabling Tor routing.')
|
logger.warning('Rate limited (429). Enable Tor routing to retry with new IP.')
|
||||||
raise FetchError('429', reason=response.reason, ip=ip)
|
raise FetchError('429', reason=response.reason, ip=ip)
|
||||||
|
|
||||||
logger.error(f'YouTube blocked request - Tor exit node overutilized. Exit IP: {ip}')
|
# Tor: exhausted retries
|
||||||
|
if attempt >= max_retries - 1:
|
||||||
|
logger.error(f'Rate limited after {max_retries} retries. Exit IP: {ip}')
|
||||||
|
raise FetchError('429', reason=response.reason, ip=ip,
|
||||||
|
error_message='Tor exit node overutilized after multiple retries')
|
||||||
|
|
||||||
# get new identity
|
# Tor: get new identity and retry
|
||||||
|
logger.info(f'Rate limited. Getting new Tor identity... (IP: {ip})')
|
||||||
error = tor_manager.new_identity(start_time)
|
error = tor_manager.new_identity(start_time)
|
||||||
if error:
|
if error:
|
||||||
raise FetchError(
|
raise FetchError(
|
||||||
'429', reason=response.reason, ip=ip,
|
'429', reason=response.reason, ip=ip,
|
||||||
error_message='Automatic circuit change: ' + error)
|
error_message='Automatic circuit change: ' + error)
|
||||||
else:
|
|
||||||
continue # retry with new identity
|
continue # retry with new identity
|
||||||
|
|
||||||
# Calculate delay with exponential backoff and jitter
|
|
||||||
delay = (base_delay * (2 ** attempt)) + random.uniform(0, 1)
|
|
||||||
logger.info(f'Rate limited (429). Waiting {delay:.1f}s before retry {attempt + 1}/{max_retries}...')
|
|
||||||
time.sleep(delay)
|
|
||||||
continue # retry
|
|
||||||
|
|
||||||
# Check for client errors (400, 404) - don't retry these
|
# Check for client errors (400, 404) - don't retry these
|
||||||
if response.status == 400:
|
if response.status == 400:
|
||||||
logger.error(f'Bad Request (400) - Invalid parameters or URL: {url[:100]}')
|
logger.error(f'Bad Request (400) - Invalid parameters or URL: {url[:100]}')
|
||||||
@@ -536,11 +533,19 @@ class RateLimitedQueue(gevent.queue.Queue):
|
|||||||
|
|
||||||
|
|
||||||
def download_thumbnail(save_directory, video_id):
|
def download_thumbnail(save_directory, video_id):
|
||||||
url = f"https://i.ytimg.com/vi/{video_id}/hq720.jpg"
|
|
||||||
save_location = os.path.join(save_directory, video_id + ".jpg")
|
save_location = os.path.join(save_directory, video_id + ".jpg")
|
||||||
|
for quality in ('hq720.jpg', 'sddefault.jpg', 'hqdefault.jpg'):
|
||||||
|
url = f"https://i.ytimg.com/vi/{video_id}/{quality}"
|
||||||
try:
|
try:
|
||||||
thumbnail = fetch_url(url, report_text="Saved thumbnail: " + video_id)
|
thumbnail = fetch_url(url, report_text="Saved thumbnail: " + video_id)
|
||||||
|
except FetchError as e:
|
||||||
|
if '404' in str(e):
|
||||||
|
continue
|
||||||
|
print("Failed to download thumbnail for " + video_id + ": " + str(e))
|
||||||
|
return False
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
|
if e.code == 404:
|
||||||
|
continue
|
||||||
print("Failed to download thumbnail for " + video_id + ": " + str(e))
|
print("Failed to download thumbnail for " + video_id + ": " + str(e))
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
@@ -551,6 +556,8 @@ def download_thumbnail(save_directory, video_id):
|
|||||||
f.write(thumbnail)
|
f.write(thumbnail)
|
||||||
f.close()
|
f.close()
|
||||||
return True
|
return True
|
||||||
|
print("No thumbnail available for " + video_id)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def download_thumbnails(save_directory, ids):
|
def download_thumbnails(save_directory, ids):
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
__version__ = 'v0.4.0'
|
__version__ = 'v0.4.2'
|
||||||
|
|||||||
115
youtube/watch.py
115
youtube/watch.py
@@ -180,8 +180,34 @@ def make_caption_src(info, lang, auto=False, trans_lang=None):
|
|||||||
label += ' (Automatic)'
|
label += ' (Automatic)'
|
||||||
if trans_lang:
|
if trans_lang:
|
||||||
label += ' -> ' + trans_lang
|
label += ' -> ' + trans_lang
|
||||||
|
|
||||||
|
# Try to use Android caption URL directly (no PO Token needed)
|
||||||
|
caption_url = None
|
||||||
|
for track in info.get('_android_caption_tracks', []):
|
||||||
|
track_lang = track.get('languageCode', '')
|
||||||
|
track_kind = track.get('kind', '')
|
||||||
|
if track_lang == lang and (
|
||||||
|
(auto and track_kind == 'asr') or
|
||||||
|
(not auto and track_kind != 'asr')
|
||||||
|
):
|
||||||
|
caption_url = track.get('baseUrl')
|
||||||
|
break
|
||||||
|
|
||||||
|
if caption_url:
|
||||||
|
# Add format
|
||||||
|
if '&fmt=' in caption_url:
|
||||||
|
caption_url = re.sub(r'&fmt=[^&]*', '&fmt=vtt', caption_url)
|
||||||
|
else:
|
||||||
|
caption_url += '&fmt=vtt'
|
||||||
|
if trans_lang:
|
||||||
|
caption_url += '&tlang=' + trans_lang
|
||||||
|
url = util.prefix_url(caption_url)
|
||||||
|
else:
|
||||||
|
# Fallback to old method
|
||||||
|
url = util.prefix_url(yt_data_extract.get_caption_url(info, lang, 'vtt', auto, trans_lang))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'url': util.prefix_url(yt_data_extract.get_caption_url(info, lang, 'vtt', auto, trans_lang)),
|
'url': url,
|
||||||
'label': label,
|
'label': label,
|
||||||
'srclang': trans_lang[0:2] if trans_lang else lang[0:2],
|
'srclang': trans_lang[0:2] if trans_lang else lang[0:2],
|
||||||
'on': False,
|
'on': False,
|
||||||
@@ -387,19 +413,38 @@ def extract_info(video_id, use_invidious, playlist_id=None, index=None):
|
|||||||
info = tasks[0].value or {}
|
info = tasks[0].value or {}
|
||||||
player_response = tasks[1].value or {}
|
player_response = tasks[1].value or {}
|
||||||
|
|
||||||
|
# Save android_vr caption tracks (no PO Token needed for these URLs)
|
||||||
|
if isinstance(player_response, str):
|
||||||
|
try:
|
||||||
|
pr_data = json.loads(player_response)
|
||||||
|
except Exception:
|
||||||
|
pr_data = {}
|
||||||
|
else:
|
||||||
|
pr_data = player_response or {}
|
||||||
|
android_caption_tracks = yt_data_extract.deep_get(
|
||||||
|
pr_data, 'captions', 'playerCaptionsTracklistRenderer',
|
||||||
|
'captionTracks', default=[])
|
||||||
|
info['_android_caption_tracks'] = android_caption_tracks
|
||||||
|
|
||||||
yt_data_extract.update_with_new_urls(info, player_response)
|
yt_data_extract.update_with_new_urls(info, player_response)
|
||||||
|
|
||||||
# Fallback to 'ios' if no valid URLs are found
|
# Fallback to 'ios' if no valid URLs are found
|
||||||
if not info.get('formats') or info.get('player_urls_missing'):
|
if not info.get('formats') or info.get('player_urls_missing'):
|
||||||
print(f"No URLs found in '{primary_client}', attempting with '{fallback_client}'.")
|
print(f"No URLs found in '{primary_client}', attempting with '{fallback_client}'.")
|
||||||
|
try:
|
||||||
player_response = fetch_player_response(fallback_client, video_id) or {}
|
player_response = fetch_player_response(fallback_client, video_id) or {}
|
||||||
yt_data_extract.update_with_new_urls(info, player_response)
|
yt_data_extract.update_with_new_urls(info, player_response)
|
||||||
|
except util.FetchError as e:
|
||||||
|
print(f"Fallback '{fallback_client}' failed: {e}")
|
||||||
|
|
||||||
# Final attempt with 'tv_embedded' if there are still no URLs
|
# Final attempt with 'tv_embedded' if there are still no URLs
|
||||||
if not info.get('formats') or info.get('player_urls_missing'):
|
if not info.get('formats') or info.get('player_urls_missing'):
|
||||||
print(f"No URLs found in '{fallback_client}', attempting with '{last_resort_client}'")
|
print(f"No URLs found in '{fallback_client}', attempting with '{last_resort_client}'")
|
||||||
|
try:
|
||||||
player_response = fetch_player_response(last_resort_client, video_id) or {}
|
player_response = fetch_player_response(last_resort_client, video_id) or {}
|
||||||
yt_data_extract.update_with_new_urls(info, player_response)
|
yt_data_extract.update_with_new_urls(info, player_response)
|
||||||
|
except util.FetchError as e:
|
||||||
|
print(f"Fallback '{last_resort_client}' failed: {e}")
|
||||||
|
|
||||||
# signature decryption
|
# signature decryption
|
||||||
if info.get('formats'):
|
if info.get('formats'):
|
||||||
@@ -628,12 +673,12 @@ def get_watch_page(video_id=None):
|
|||||||
|
|
||||||
# prefix urls, and other post-processing not handled by yt_data_extract
|
# prefix urls, and other post-processing not handled by yt_data_extract
|
||||||
for item in info['related_videos']:
|
for item in info['related_videos']:
|
||||||
# For playlists, use first_video_id for thumbnail, not playlist id
|
# Only set thumbnail if YouTube didn't provide one
|
||||||
|
if not item.get('thumbnail'):
|
||||||
if item.get('type') == 'playlist' and item.get('first_video_id'):
|
if item.get('type') == 'playlist' and item.get('first_video_id'):
|
||||||
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hq720.jpg".format(item['first_video_id'])
|
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hqdefault.jpg".format(item['first_video_id'])
|
||||||
elif item.get('type') == 'video':
|
elif item.get('type') == 'video' and item.get('id'):
|
||||||
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hq720.jpg".format(item['id'])
|
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hqdefault.jpg".format(item['id'])
|
||||||
# For other types, keep existing thumbnail or skip
|
|
||||||
util.prefix_urls(item)
|
util.prefix_urls(item)
|
||||||
util.add_extra_html_info(item)
|
util.add_extra_html_info(item)
|
||||||
for song in info['music_list']:
|
for song in info['music_list']:
|
||||||
@@ -641,9 +686,9 @@ def get_watch_page(video_id=None):
|
|||||||
if info['playlist']:
|
if info['playlist']:
|
||||||
playlist_id = info['playlist']['id']
|
playlist_id = info['playlist']['id']
|
||||||
for item in info['playlist']['items']:
|
for item in info['playlist']['items']:
|
||||||
# Set high quality thumbnail for playlist videos
|
# Only set thumbnail if YouTube didn't provide one
|
||||||
if item.get('type') == 'video' and item.get('id'):
|
if not item.get('thumbnail') and item.get('type') == 'video' and item.get('id'):
|
||||||
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hq720.jpg".format(item['id'])
|
item['thumbnail'] = "https://i.ytimg.com/vi/{}/hqdefault.jpg".format(item['id'])
|
||||||
util.prefix_urls(item)
|
util.prefix_urls(item)
|
||||||
util.add_extra_html_info(item)
|
util.add_extra_html_info(item)
|
||||||
if playlist_id:
|
if playlist_id:
|
||||||
@@ -696,30 +741,6 @@ def get_watch_page(video_id=None):
|
|||||||
pair_sources = source_info['pair_sources']
|
pair_sources = source_info['pair_sources']
|
||||||
uni_idx, pair_idx = source_info['uni_idx'], source_info['pair_idx']
|
uni_idx, pair_idx = source_info['uni_idx'], source_info['pair_idx']
|
||||||
|
|
||||||
# Extract audio tracks using yt-dlp for multi-language support
|
|
||||||
audio_tracks = []
|
|
||||||
try:
|
|
||||||
from youtube import ytdlp_integration
|
|
||||||
logger.info(f'Extracting audio tracks for video: {video_id}')
|
|
||||||
ytdlp_info = ytdlp_integration.extract_video_info_ytdlp(video_id)
|
|
||||||
audio_tracks = ytdlp_info.get('audio_tracks', [])
|
|
||||||
|
|
||||||
if audio_tracks:
|
|
||||||
logger.info(f'✓ Found {len(audio_tracks)} audio tracks:')
|
|
||||||
for i, track in enumerate(audio_tracks[:10], 1): # Log first 10
|
|
||||||
logger.info(f' [{i}] {track["language_name"]} ({track["language"]}) - '
|
|
||||||
f'bitrate: {track.get("audio_bitrate", "N/A")}k, '
|
|
||||||
f'codec: {track.get("acodec", "N/A")}, '
|
|
||||||
f'format_id: {track.get("format_id", "N/A")}')
|
|
||||||
if len(audio_tracks) > 10:
|
|
||||||
logger.info(f' ... and {len(audio_tracks) - 10} more')
|
|
||||||
else:
|
|
||||||
logger.warning(f'No audio tracks found for video {video_id}')
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f'Failed to extract audio tracks: {e}', exc_info=True)
|
|
||||||
audio_tracks = []
|
|
||||||
|
|
||||||
pair_quality = yt_data_extract.deep_get(pair_sources, pair_idx, 'quality')
|
pair_quality = yt_data_extract.deep_get(pair_sources, pair_idx, 'quality')
|
||||||
uni_quality = yt_data_extract.deep_get(uni_sources, uni_idx, 'quality')
|
uni_quality = yt_data_extract.deep_get(uni_sources, uni_idx, 'quality')
|
||||||
|
|
||||||
@@ -843,9 +864,7 @@ def get_watch_page(video_id=None):
|
|||||||
'playlist': info['playlist'],
|
'playlist': info['playlist'],
|
||||||
'related': info['related_videos'],
|
'related': info['related_videos'],
|
||||||
'playability_error': info['playability_error'],
|
'playability_error': info['playability_error'],
|
||||||
'audio_tracks': audio_tracks,
|
|
||||||
},
|
},
|
||||||
audio_tracks = audio_tracks,
|
|
||||||
font_family = youtube.font_choices[settings.font], # for embed page
|
font_family = youtube.font_choices[settings.font], # for embed page
|
||||||
**source_info,
|
**source_info,
|
||||||
using_pair_sources = using_pair_sources,
|
using_pair_sources = using_pair_sources,
|
||||||
@@ -854,16 +873,13 @@ def get_watch_page(video_id=None):
|
|||||||
|
|
||||||
@yt_app.route('/api/<path:dummy>')
|
@yt_app.route('/api/<path:dummy>')
|
||||||
def get_captions(dummy):
|
def get_captions(dummy):
|
||||||
|
url = 'https://www.youtube.com' + request.full_path
|
||||||
try:
|
try:
|
||||||
result = util.fetch_url('https://www.youtube.com' + request.full_path)
|
result = util.fetch_url(url, headers=util.mobile_ua)
|
||||||
result = result.replace(b"align:start position:0%", b"")
|
result = result.replace(b"align:start position:0%", b"")
|
||||||
return result
|
return flask.Response(result, mimetype='text/vtt')
|
||||||
except util.FetchError as e:
|
|
||||||
# Return empty captions gracefully instead of error page
|
|
||||||
logger.warning(f'Failed to fetch captions: {e}')
|
|
||||||
return flask.Response(b'WEBVTT\n\n', mimetype='text/vtt', status=200)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f'Unexpected error fetching captions: {e}')
|
logger.debug(f'Caption fetch failed: {e}')
|
||||||
return flask.Response(b'WEBVTT\n\n', mimetype='text/vtt', status=200)
|
return flask.Response(b'WEBVTT\n\n', mimetype='text/vtt', status=200)
|
||||||
|
|
||||||
|
|
||||||
@@ -929,18 +945,3 @@ def get_transcript(caption_path):
|
|||||||
|
|
||||||
return flask.Response(result.encode('utf-8'),
|
return flask.Response(result.encode('utf-8'),
|
||||||
mimetype='text/plain;charset=UTF-8')
|
mimetype='text/plain;charset=UTF-8')
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
|
||||||
# yt-dlp Integration Routes
|
|
||||||
# ============================================================================
|
|
||||||
|
|
||||||
@yt_app.route('/ytl-api/video-with-audio/<video_id>')
|
|
||||||
def proxy_video_with_audio(video_id):
|
|
||||||
"""
|
|
||||||
Proxy para servir video con audio específico usando yt-dlp
|
|
||||||
"""
|
|
||||||
from youtube import ytdlp_proxy
|
|
||||||
audio_lang = request.args.get('lang', 'en')
|
|
||||||
max_quality = int(request.args.get('quality', 720))
|
|
||||||
return ytdlp_proxy.stream_video_with_audio(video_id, audio_lang, max_quality)
|
|
||||||
|
|||||||
@@ -369,9 +369,9 @@ def extract_item_info(item, additional_info={}):
|
|||||||
['detailedMetadataSnippets', 0, 'snippetText'],
|
['detailedMetadataSnippets', 0, 'snippetText'],
|
||||||
))
|
))
|
||||||
info['thumbnail'] = normalize_url(multi_deep_get(item,
|
info['thumbnail'] = normalize_url(multi_deep_get(item,
|
||||||
['thumbnail', 'thumbnails', 0, 'url'], # videos
|
['thumbnail', 'thumbnails', -1, 'url'], # videos (highest quality)
|
||||||
['thumbnails', 0, 'thumbnails', 0, 'url'], # playlists
|
['thumbnails', 0, 'thumbnails', -1, 'url'], # playlists
|
||||||
['thumbnailRenderer', 'showCustomThumbnailRenderer', 'thumbnail', 'thumbnails', 0, 'url'], # shows
|
['thumbnailRenderer', 'showCustomThumbnailRenderer', 'thumbnail', 'thumbnails', -1, 'url'], # shows
|
||||||
))
|
))
|
||||||
|
|
||||||
info['badges'] = []
|
info['badges'] = []
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ def extract_playlist_metadata(polymer_json):
|
|||||||
if metadata['first_video_id'] is None:
|
if metadata['first_video_id'] is None:
|
||||||
metadata['thumbnail'] = None
|
metadata['thumbnail'] = None
|
||||||
else:
|
else:
|
||||||
metadata['thumbnail'] = f"https://i.ytimg.com/vi/{metadata['first_video_id']}/hq720.jpg"
|
metadata['thumbnail'] = f"https://i.ytimg.com/vi/{metadata['first_video_id']}/hqdefault.jpg"
|
||||||
|
|
||||||
metadata['video_count'] = extract_int(header.get('numVideosText'))
|
metadata['video_count'] = extract_int(header.get('numVideosText'))
|
||||||
metadata['description'] = extract_str(header.get('descriptionText'), default='')
|
metadata['description'] = extract_str(header.get('descriptionText'), default='')
|
||||||
|
|||||||
@@ -628,6 +628,7 @@ def extract_watch_info(polymer_json):
|
|||||||
info['manual_caption_languages'] = []
|
info['manual_caption_languages'] = []
|
||||||
info['_manual_caption_language_names'] = {} # language name written in that language, needed in some cases to create the url
|
info['_manual_caption_language_names'] = {} # language name written in that language, needed in some cases to create the url
|
||||||
info['translation_languages'] = []
|
info['translation_languages'] = []
|
||||||
|
info['_caption_track_urls'] = {} # lang_code -> full baseUrl from player response
|
||||||
captions_info = player_response.get('captions', {})
|
captions_info = player_response.get('captions', {})
|
||||||
info['_captions_base_url'] = normalize_url(deep_get(captions_info, 'playerCaptionsRenderer', 'baseUrl'))
|
info['_captions_base_url'] = normalize_url(deep_get(captions_info, 'playerCaptionsRenderer', 'baseUrl'))
|
||||||
# Sometimes the above playerCaptionsRender is randomly missing
|
# Sometimes the above playerCaptionsRender is randomly missing
|
||||||
@@ -658,6 +659,10 @@ def extract_watch_info(polymer_json):
|
|||||||
else:
|
else:
|
||||||
info['manual_caption_languages'].append(lang_code)
|
info['manual_caption_languages'].append(lang_code)
|
||||||
base_url = caption_track.get('baseUrl', '')
|
base_url = caption_track.get('baseUrl', '')
|
||||||
|
# Store the full URL from the player response (includes valid tokens)
|
||||||
|
if base_url:
|
||||||
|
normalized = normalize_url(base_url) if base_url.startswith('/') or not base_url.startswith('http') else base_url
|
||||||
|
info['_caption_track_urls'][lang_code + ('_asr' if caption_track.get('kind') == 'asr' else '')] = normalized
|
||||||
lang_name = deep_get(urllib.parse.parse_qs(urllib.parse.urlparse(base_url).query), 'name', 0)
|
lang_name = deep_get(urllib.parse.parse_qs(urllib.parse.urlparse(base_url).query), 'name', 0)
|
||||||
if lang_name:
|
if lang_name:
|
||||||
info['_manual_caption_language_names'][lang_code] = lang_name
|
info['_manual_caption_language_names'][lang_code] = lang_name
|
||||||
@@ -825,6 +830,21 @@ def captions_available(info):
|
|||||||
|
|
||||||
def get_caption_url(info, language, format, automatic=False, translation_language=None):
|
def get_caption_url(info, language, format, automatic=False, translation_language=None):
|
||||||
'''Gets the url for captions with the given language and format. If automatic is True, get the automatic captions for that language. If translation_language is given, translate the captions from `language` to `translation_language`. If automatic is true and translation_language is given, the automatic captions will be translated.'''
|
'''Gets the url for captions with the given language and format. If automatic is True, get the automatic captions for that language. If translation_language is given, translate the captions from `language` to `translation_language`. If automatic is true and translation_language is given, the automatic captions will be translated.'''
|
||||||
|
# Try to use the direct URL from the player response first (has valid tokens)
|
||||||
|
track_key = language + ('_asr' if automatic else '')
|
||||||
|
direct_url = info.get('_caption_track_urls', {}).get(track_key)
|
||||||
|
if direct_url:
|
||||||
|
url = direct_url
|
||||||
|
# Override format
|
||||||
|
if '&fmt=' in url:
|
||||||
|
url = re.sub(r'&fmt=[^&]*', '&fmt=' + format, url)
|
||||||
|
else:
|
||||||
|
url += '&fmt=' + format
|
||||||
|
if translation_language:
|
||||||
|
url += '&tlang=' + translation_language
|
||||||
|
return url
|
||||||
|
|
||||||
|
# Fallback to base_url construction
|
||||||
url = info['_captions_base_url']
|
url = info['_captions_base_url']
|
||||||
if not url:
|
if not url:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""
|
|
||||||
yt-dlp integration wrapper for backward compatibility.
|
|
||||||
|
|
||||||
This module now uses the centralized ytdlp_service for all operations.
|
|
||||||
"""
|
|
||||||
import logging
|
|
||||||
from youtube.ytdlp_service import (
|
|
||||||
extract_video_info,
|
|
||||||
get_language_name,
|
|
||||||
clear_cache,
|
|
||||||
get_cache_info,
|
|
||||||
)
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def extract_video_info_ytdlp(video_id):
|
|
||||||
"""
|
|
||||||
Extract video information using yt-dlp (with caching).
|
|
||||||
|
|
||||||
This is a wrapper around ytdlp_service.extract_video_info()
|
|
||||||
for backward compatibility.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
video_id: YouTube video ID
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dictionary with audio_tracks, formats, title, duration
|
|
||||||
"""
|
|
||||||
logger.debug(f'Extracting video info (legacy API): {video_id}')
|
|
||||||
|
|
||||||
info = extract_video_info(video_id)
|
|
||||||
|
|
||||||
# Convert to legacy format for backward compatibility
|
|
||||||
return {
|
|
||||||
'audio_tracks': info.get('audio_tracks', []),
|
|
||||||
'all_audio_formats': info.get('formats', []),
|
|
||||||
'formats': info.get('formats', []),
|
|
||||||
'title': info.get('title', ''),
|
|
||||||
'duration': info.get('duration', 0),
|
|
||||||
'error': info.get('error'),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def get_audio_formats_for_language(video_id, language='en'):
|
|
||||||
"""
|
|
||||||
Get available audio formats for a specific language.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
video_id: YouTube video ID
|
|
||||||
language: Language code (default: 'en')
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of audio format dicts
|
|
||||||
"""
|
|
||||||
info = extract_video_info_ytdlp(video_id)
|
|
||||||
|
|
||||||
if 'error' in info:
|
|
||||||
logger.warning(f'Cannot get audio formats: {info["error"]}')
|
|
||||||
return []
|
|
||||||
|
|
||||||
audio_formats = []
|
|
||||||
for track in info.get('audio_tracks', []):
|
|
||||||
if track['language'] == language:
|
|
||||||
audio_formats.append(track)
|
|
||||||
|
|
||||||
logger.debug(f'Found {len(audio_formats)} {language} audio formats')
|
|
||||||
return audio_formats
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'extract_video_info_ytdlp',
|
|
||||||
'get_audio_formats_for_language',
|
|
||||||
'get_language_name',
|
|
||||||
'clear_cache',
|
|
||||||
'get_cache_info',
|
|
||||||
]
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""
|
|
||||||
Proxy for serving videos with specific audio using yt-dlp.
|
|
||||||
|
|
||||||
This module provides streaming functionality for unified formats
|
|
||||||
with specific audio languages.
|
|
||||||
"""
|
|
||||||
import logging
|
|
||||||
from flask import Response, request, stream_with_context
|
|
||||||
import urllib.request
|
|
||||||
import urllib.error
|
|
||||||
from youtube.ytdlp_service import find_best_unified_format
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def stream_video_with_audio(video_id: str, audio_language: str = 'en', max_quality: int = 720):
|
|
||||||
"""
|
|
||||||
Stream video with specific audio language.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
video_id: YouTube video ID
|
|
||||||
audio_language: Preferred audio language (default: 'en')
|
|
||||||
max_quality: Maximum video height (default: 720)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Flask Response with video stream, or 404 if not available
|
|
||||||
"""
|
|
||||||
logger.info(f'Stream request: {video_id} | audio={audio_language} | quality={max_quality}p')
|
|
||||||
|
|
||||||
# Find best unified format
|
|
||||||
best_format = find_best_unified_format(video_id, audio_language, max_quality)
|
|
||||||
|
|
||||||
if not best_format:
|
|
||||||
logger.info(f'No suitable unified format found, returning 404 to trigger fallback')
|
|
||||||
return Response('No suitable unified format available', status=404)
|
|
||||||
|
|
||||||
url = best_format.get('url')
|
|
||||||
if not url:
|
|
||||||
logger.error('Format found but no URL available')
|
|
||||||
return Response('Format URL not available', status=500)
|
|
||||||
|
|
||||||
logger.debug(f'Streaming from: {url[:80]}...')
|
|
||||||
|
|
||||||
# Stream the video
|
|
||||||
try:
|
|
||||||
req = urllib.request.Request(url)
|
|
||||||
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
|
|
||||||
req.add_header('Accept', '*/*')
|
|
||||||
|
|
||||||
# Add Range header if client requests it
|
|
||||||
if 'Range' in request.headers:
|
|
||||||
req.add_header('Range', request.headers['Range'])
|
|
||||||
logger.debug(f'Range request: {request.headers["Range"]}')
|
|
||||||
|
|
||||||
resp = urllib.request.urlopen(req, timeout=60)
|
|
||||||
|
|
||||||
def generate():
|
|
||||||
"""Generator for streaming video chunks."""
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
chunk = resp.read(65536) # 64KB chunks
|
|
||||||
if not chunk:
|
|
||||||
break
|
|
||||||
yield chunk
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f'Stream error: {e}')
|
|
||||||
raise
|
|
||||||
|
|
||||||
# Build response headers
|
|
||||||
response_headers = {
|
|
||||||
'Content-Type': resp.headers.get('Content-Type', 'video/mp4'),
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
}
|
|
||||||
|
|
||||||
# Copy important headers
|
|
||||||
for header in ['Content-Length', 'Content-Range', 'Accept-Ranges']:
|
|
||||||
if header in resp.headers:
|
|
||||||
response_headers[header] = resp.headers[header]
|
|
||||||
|
|
||||||
status_code = resp.getcode()
|
|
||||||
logger.info(f'Streaming started: {status_code}')
|
|
||||||
|
|
||||||
return Response(
|
|
||||||
stream_with_context(generate()),
|
|
||||||
status=status_code,
|
|
||||||
headers=response_headers,
|
|
||||||
direct_passthrough=True
|
|
||||||
)
|
|
||||||
|
|
||||||
except urllib.error.HTTPError as e:
|
|
||||||
logger.error(f'HTTP error streaming: {e.code} {e.reason}')
|
|
||||||
return Response(f'Error: {e.code} {e.reason}', status=e.code)
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
logger.error(f'URL error streaming: {e.reason}')
|
|
||||||
return Response(f'Network error: {e.reason}', status=502)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f'Streaming error: {e}', exc_info=True)
|
|
||||||
return Response(f'Error: {e}', status=500)
|
|
||||||
@@ -1,393 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""
|
|
||||||
Centralized yt-dlp integration with caching, logging, and error handling.
|
|
||||||
|
|
||||||
This module provides a clean interface for yt-dlp functionality:
|
|
||||||
- Multi-language audio track extraction
|
|
||||||
- Subtitle extraction
|
|
||||||
- Age-restricted video support
|
|
||||||
|
|
||||||
All yt-dlp usage should go through this module for consistency.
|
|
||||||
"""
|
|
||||||
import logging
|
|
||||||
from functools import lru_cache
|
|
||||||
from typing import Dict, List, Optional, Any
|
|
||||||
import yt_dlp
|
|
||||||
import settings
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
# Language name mapping
|
|
||||||
LANGUAGE_NAMES = {
|
|
||||||
'en': 'English',
|
|
||||||
'es': 'Español',
|
|
||||||
'fr': 'Français',
|
|
||||||
'de': 'Deutsch',
|
|
||||||
'it': 'Italiano',
|
|
||||||
'pt': 'Português',
|
|
||||||
'ru': 'Русский',
|
|
||||||
'ja': '日本語',
|
|
||||||
'ko': '한국어',
|
|
||||||
'zh': '中文',
|
|
||||||
'ar': 'العربية',
|
|
||||||
'hi': 'हिन्दी',
|
|
||||||
'und': 'Unknown',
|
|
||||||
'zxx': 'No linguistic content',
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def get_language_name(lang_code: str) -> str:
|
|
||||||
"""Convert ISO 639-1/2 language code to readable name."""
|
|
||||||
if not lang_code:
|
|
||||||
return 'Unknown'
|
|
||||||
return LANGUAGE_NAMES.get(lang_code.lower(), lang_code.upper())
|
|
||||||
|
|
||||||
|
|
||||||
def _get_ytdlp_config() -> Dict[str, Any]:
|
|
||||||
"""Get yt-dlp configuration from settings."""
|
|
||||||
config = {
|
|
||||||
'quiet': True,
|
|
||||||
'no_warnings': True,
|
|
||||||
'extract_flat': False,
|
|
||||||
'format': 'best',
|
|
||||||
'skip_download': True,
|
|
||||||
'socket_timeout': 30,
|
|
||||||
'extractor_retries': 3,
|
|
||||||
'http_chunk_size': 10485760, # 10MB
|
|
||||||
}
|
|
||||||
|
|
||||||
# Configure Tor proxy if enabled
|
|
||||||
if settings.route_tor:
|
|
||||||
config['proxy'] = 'socks5://127.0.0.1:9150'
|
|
||||||
logger.debug('Tor proxy enabled for yt-dlp')
|
|
||||||
|
|
||||||
# Use cookies if available
|
|
||||||
import os
|
|
||||||
cookies_file = 'youtube_cookies.txt'
|
|
||||||
if os.path.exists(cookies_file):
|
|
||||||
config['cookiefile'] = cookies_file
|
|
||||||
logger.debug('Using cookies file for yt-dlp')
|
|
||||||
|
|
||||||
return config
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=128)
|
|
||||||
def extract_video_info(video_id: str) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Extract video information using yt-dlp with caching.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
video_id: YouTube video ID
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dictionary with video information including audio tracks
|
|
||||||
|
|
||||||
Caching:
|
|
||||||
Results are cached to avoid repeated requests to YouTube.
|
|
||||||
Cache size is limited to prevent memory issues.
|
|
||||||
"""
|
|
||||||
# Check if yt-dlp is enabled
|
|
||||||
if not getattr(settings, 'ytdlp_enabled', True):
|
|
||||||
logger.debug('yt-dlp integration is disabled')
|
|
||||||
return {'error': 'yt-dlp disabled', 'audio_tracks': []}
|
|
||||||
|
|
||||||
url = f'https://www.youtube.com/watch?v={video_id}'
|
|
||||||
ydl_opts = _get_ytdlp_config()
|
|
||||||
|
|
||||||
try:
|
|
||||||
logger.debug(f'Extracting video info: {video_id}')
|
|
||||||
|
|
||||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
||||||
info = ydl.extract_info(url, download=False)
|
|
||||||
|
|
||||||
if not info:
|
|
||||||
logger.warning(f'No info returned for video: {video_id}')
|
|
||||||
return {'error': 'No info returned', 'audio_tracks': []}
|
|
||||||
|
|
||||||
logger.info(f'Extracted {len(info.get("formats", []))} total formats')
|
|
||||||
|
|
||||||
# Extract audio tracks grouped by language
|
|
||||||
audio_tracks = _extract_audio_tracks(info)
|
|
||||||
|
|
||||||
return {
|
|
||||||
'video_id': video_id,
|
|
||||||
'title': info.get('title', ''),
|
|
||||||
'duration': info.get('duration', 0),
|
|
||||||
'audio_tracks': audio_tracks,
|
|
||||||
'formats': info.get('formats', []),
|
|
||||||
'subtitles': info.get('subtitles', {}),
|
|
||||||
'automatic_captions': info.get('automatic_captions', {}),
|
|
||||||
}
|
|
||||||
|
|
||||||
except yt_dlp.utils.DownloadError as e:
|
|
||||||
logger.error(f'yt-dlp download error for {video_id}: {e}')
|
|
||||||
return {'error': str(e), 'audio_tracks': []}
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f'yt-dlp extraction error for {video_id}: {e}', exc_info=True)
|
|
||||||
return {'error': str(e), 'audio_tracks': []}
|
|
||||||
|
|
||||||
|
|
||||||
def _extract_audio_tracks(info: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
||||||
"""
|
|
||||||
Extract audio tracks from video info, grouped by language.
|
|
||||||
|
|
||||||
Returns a list of unique audio tracks (one per language),
|
|
||||||
keeping the highest quality for each language.
|
|
||||||
"""
|
|
||||||
audio_by_language = {}
|
|
||||||
all_formats = info.get('formats', [])
|
|
||||||
|
|
||||||
logger.debug(f'Processing {len(all_formats)} formats to extract audio tracks')
|
|
||||||
|
|
||||||
for fmt in all_formats:
|
|
||||||
# Only audio-only formats
|
|
||||||
has_audio = fmt.get('acodec') and fmt.get('acodec') != 'none'
|
|
||||||
has_video = fmt.get('vcodec') and fmt.get('vcodec') != 'none'
|
|
||||||
|
|
||||||
if not has_audio or has_video:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Extract language information
|
|
||||||
lang = (
|
|
||||||
fmt.get('language') or
|
|
||||||
fmt.get('audio_language') or
|
|
||||||
fmt.get('lang') or
|
|
||||||
'und'
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get language name
|
|
||||||
lang_name = (
|
|
||||||
fmt.get('language_name') or
|
|
||||||
fmt.get('lang_name') or
|
|
||||||
get_language_name(lang)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get bitrate
|
|
||||||
bitrate = fmt.get('abr') or fmt.get('tbr') or 0
|
|
||||||
|
|
||||||
# Create track info
|
|
||||||
track_info = {
|
|
||||||
'language': lang,
|
|
||||||
'language_name': lang_name,
|
|
||||||
'format_id': str(fmt.get('format_id', '')),
|
|
||||||
'itag': str(fmt.get('format_id', '')),
|
|
||||||
'ext': fmt.get('ext'),
|
|
||||||
'acodec': fmt.get('acodec'),
|
|
||||||
'audio_bitrate': int(bitrate) if bitrate else 0,
|
|
||||||
'audio_sample_rate': fmt.get('asr'),
|
|
||||||
'url': fmt.get('url'),
|
|
||||||
'filesize': fmt.get('filesize'),
|
|
||||||
}
|
|
||||||
|
|
||||||
# Keep best quality per language
|
|
||||||
lang_key = lang.lower()
|
|
||||||
if lang_key not in audio_by_language:
|
|
||||||
audio_by_language[lang_key] = track_info
|
|
||||||
logger.debug(f' Added {lang} ({lang_name}) - {bitrate}k')
|
|
||||||
else:
|
|
||||||
current_bitrate = audio_by_language[lang_key].get('audio_bitrate', 0)
|
|
||||||
if bitrate > current_bitrate:
|
|
||||||
logger.debug(f' Updated {lang} ({lang_name}): {current_bitrate}k → {bitrate}k')
|
|
||||||
audio_by_language[lang_key] = track_info
|
|
||||||
|
|
||||||
# Convert to list and sort
|
|
||||||
audio_tracks = list(audio_by_language.values())
|
|
||||||
|
|
||||||
# Sort: English first, then by bitrate (descending)
|
|
||||||
audio_tracks.sort(
|
|
||||||
key=lambda x: (
|
|
||||||
0 if x['language'] == 'en' else 1,
|
|
||||||
-x.get('audio_bitrate', 0)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.info(f'Extracted {len(audio_tracks)} unique audio languages')
|
|
||||||
for track in audio_tracks[:5]: # Log first 5
|
|
||||||
logger.info(f' → {track["language_name"]} ({track["language"]}): {track["audio_bitrate"]}k')
|
|
||||||
|
|
||||||
return audio_tracks
|
|
||||||
|
|
||||||
|
|
||||||
def get_subtitle_url(video_id: str, lang: str = 'en') -> Optional[str]:
|
|
||||||
"""
|
|
||||||
Get subtitle URL for a specific language.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
video_id: YouTube video ID
|
|
||||||
lang: Language code (default: 'en')
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
URL to subtitle file, or None if not available
|
|
||||||
"""
|
|
||||||
info = extract_video_info(video_id)
|
|
||||||
|
|
||||||
if 'error' in info:
|
|
||||||
logger.warning(f'Cannot get subtitles: {info["error"]}')
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Try manual subtitles first
|
|
||||||
subtitles = info.get('subtitles', {})
|
|
||||||
if lang in subtitles:
|
|
||||||
for sub in subtitles[lang]:
|
|
||||||
if sub.get('ext') == 'vtt':
|
|
||||||
logger.debug(f'Found manual {lang} subtitle')
|
|
||||||
return sub.get('url')
|
|
||||||
|
|
||||||
# Try automatic captions
|
|
||||||
auto_captions = info.get('automatic_captions', {})
|
|
||||||
if lang in auto_captions:
|
|
||||||
for sub in auto_captions[lang]:
|
|
||||||
if sub.get('ext') == 'vtt':
|
|
||||||
logger.debug(f'Found automatic {lang} subtitle')
|
|
||||||
return sub.get('url')
|
|
||||||
|
|
||||||
logger.debug(f'No {lang} subtitle found')
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def find_best_unified_format(
|
|
||||||
video_id: str,
|
|
||||||
audio_language: str = 'en',
|
|
||||||
max_quality: int = 720
|
|
||||||
) -> Optional[Dict[str, Any]]:
|
|
||||||
"""
|
|
||||||
Find best unified (video+audio) format for specific language and quality.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
video_id: YouTube video ID
|
|
||||||
audio_language: Preferred audio language
|
|
||||||
max_quality: Maximum video height (e.g., 720, 1080)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Format dict if found, None otherwise
|
|
||||||
"""
|
|
||||||
info = extract_video_info(video_id)
|
|
||||||
|
|
||||||
if 'error' in info or not info.get('formats'):
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Quality thresholds (minimum acceptable height as % of requested)
|
|
||||||
thresholds = {
|
|
||||||
2160: 0.85,
|
|
||||||
1440: 0.80,
|
|
||||||
1080: 0.70,
|
|
||||||
720: 0.70,
|
|
||||||
480: 0.60,
|
|
||||||
360: 0.50,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get threshold for requested quality
|
|
||||||
threshold = 0.70
|
|
||||||
for q, t in thresholds.items():
|
|
||||||
if max_quality >= q:
|
|
||||||
threshold = t
|
|
||||||
break
|
|
||||||
|
|
||||||
min_height = int(max_quality * threshold)
|
|
||||||
logger.debug(f'Quality threshold: {threshold:.0%} = min {min_height}p for {max_quality}p')
|
|
||||||
|
|
||||||
candidates = []
|
|
||||||
audio_lang_lower = audio_language.lower()
|
|
||||||
|
|
||||||
for fmt in info['formats']:
|
|
||||||
# Must have both video and audio
|
|
||||||
has_video = fmt.get('vcodec') and fmt.get('vcodec') != 'none'
|
|
||||||
has_audio = fmt.get('acodec') and fmt.get('acodec') != 'none'
|
|
||||||
|
|
||||||
if not (has_video and has_audio):
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Skip HLS/DASH formats
|
|
||||||
protocol = fmt.get('protocol', '')
|
|
||||||
format_id = str(fmt.get('format_id', ''))
|
|
||||||
|
|
||||||
if any(x in protocol.lower() for x in ['m3u8', 'hls', 'dash']):
|
|
||||||
continue
|
|
||||||
if format_id.startswith('9'): # HLS formats
|
|
||||||
continue
|
|
||||||
|
|
||||||
height = fmt.get('height', 0)
|
|
||||||
if height < min_height:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Language matching
|
|
||||||
lang = (
|
|
||||||
fmt.get('language') or
|
|
||||||
fmt.get('audio_language') or
|
|
||||||
'en'
|
|
||||||
).lower()
|
|
||||||
|
|
||||||
lang_match = (
|
|
||||||
lang == audio_lang_lower or
|
|
||||||
lang.startswith(audio_lang_lower[:2]) or
|
|
||||||
audio_lang_lower.startswith(lang[:2])
|
|
||||||
)
|
|
||||||
|
|
||||||
if not lang_match:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Calculate score
|
|
||||||
score = 0
|
|
||||||
|
|
||||||
# Language match bonus
|
|
||||||
if lang == audio_lang_lower:
|
|
||||||
score += 10000
|
|
||||||
elif lang.startswith(audio_lang_lower[:2]):
|
|
||||||
score += 8000
|
|
||||||
else:
|
|
||||||
score += 5000
|
|
||||||
|
|
||||||
# Quality score
|
|
||||||
quality_diff = abs(height - max_quality)
|
|
||||||
if height >= max_quality:
|
|
||||||
score += 3000 - quality_diff
|
|
||||||
else:
|
|
||||||
score += 2000 - quality_diff
|
|
||||||
|
|
||||||
# Protocol preference
|
|
||||||
if protocol in ('https', 'http'):
|
|
||||||
score += 500
|
|
||||||
|
|
||||||
# Format preference
|
|
||||||
if fmt.get('ext') == 'mp4':
|
|
||||||
score += 100
|
|
||||||
|
|
||||||
candidates.append({
|
|
||||||
'format': fmt,
|
|
||||||
'score': score,
|
|
||||||
'height': height,
|
|
||||||
'lang': lang,
|
|
||||||
})
|
|
||||||
|
|
||||||
if not candidates:
|
|
||||||
logger.debug(f'No unified format found for {max_quality}p + {audio_language}')
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Sort by score and return best
|
|
||||||
candidates.sort(key=lambda x: x['score'], reverse=True)
|
|
||||||
best = candidates[0]
|
|
||||||
|
|
||||||
logger.info(
|
|
||||||
f'Selected unified format: {best["format"].get("format_id")} | '
|
|
||||||
f'{best["lang"]} | {best["height"]}p | score={best["score"]}'
|
|
||||||
)
|
|
||||||
|
|
||||||
return best['format']
|
|
||||||
|
|
||||||
|
|
||||||
def clear_cache():
|
|
||||||
"""Clear the video info cache."""
|
|
||||||
extract_video_info.cache_clear()
|
|
||||||
logger.info('yt-dlp cache cleared')
|
|
||||||
|
|
||||||
|
|
||||||
def get_cache_info() -> Dict[str, Any]:
|
|
||||||
"""Get cache statistics."""
|
|
||||||
cache_info = extract_video_info.cache_info()
|
|
||||||
return {
|
|
||||||
'hits': cache_info.hits,
|
|
||||||
'misses': cache_info.misses,
|
|
||||||
'size': cache_info.currsize,
|
|
||||||
'maxsize': cache_info.maxsize,
|
|
||||||
'hit_rate': cache_info.hits / (cache_info.hits + cache_info.misses) if (cache_info.hits + cache_info.misses) > 0 else 0,
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user