Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 117x 117x 117x 117x | import { DateTime } from 'luxon';
import { Plugin } from '../../model/plugin';
import { Mod } from '../../model/tag';
export const ytdlpDeltaPlugin: Plugin = {
tag: '_plugin/delta/ytdlp',
name: $localize`▶️ Get Video`,
config: {
mod: $localize`▶️ YT-DLP`,
version: 1,
default: false,
generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
description: $localize`Download and cache videos using YT-DLP.`,
actions: [
{ if: 'plugin/embed', tag: '_plugin/delta/ytdlp', labelOff: $localize`yt-dlp`, title: $localize`Download video with YT-DLP.`, global: true },
{ tag: '_plugin/delta/ytdlp', labelOn: $localize`cancel`, title: $localize`Cancel YT-DLP.` },
],
timeoutMs: 900_000,
requirements: `
yt-dlp
requests
`,
language: 'python',
// language=python
script: `
import tempfile
import os
import sys
import requests
import json
import yt_dlp
ref = json.load(sys.stdin)
origin = ref.get('origin', '')
url = ref.get('plugins', {}).get('plugin/embed', {}).get('url', ref.get('url', ''))
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
base_name = temp_file.name
downloaded_file = None
try:
ydl_opts = {
'outtmpl': f'{base_name}.%(ext)s',
'remote_components': ['ejs:github'],
'js_runtimes': {'bun': {'path': os.environ['JASPER_NODE']}},
'noprogress': True,
}
saveStdout = sys.stdout
sys.stdout = sys.stderr
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
ext = info.get('ext', 'mp4')
except Exception as e:
if 'This live event will begin in' in str(e):
sys.exit(0)
raise
finally:
sys.stdout = saveStdout
# Find the actual downloaded file
downloaded_file = f'{base_name}.{ext}'
# Read the file content
with open(downloaded_file, 'rb') as f:
file_data = f.read()
# Determine mime type based on extension
mime_types = {
'mp4': 'video/mp4',
'webm': 'video/webm',
'mp3': 'audio/mpeg',
'm4a': 'audio/mp4',
'mkv': 'video/x-matroska',
}
mime = mime_types.get(ext, 'application/octet-stream')
response = requests.post(
f"{os.environ['JASPER_API']}/pub/api/v1/repl/cache",
data=file_data,
headers={
'Local-Origin': origin or 'default',
'User-Role': 'ROLE_ADMIN',
'Content-Type': mime,
},
params={
'origin': origin,
'title': ref.get('title', ''),
'mime': mime,
}
)
if not response.ok:
print(f"Error {response.status_code}: {response.text}", file=sys.stderr)
sys.exit(1)
cache = response.json()
ref.pop('metadata', None)
ref.setdefault('tags', []).append('plugin/video')
ref['tags'] = [t for t in ref['tags'] if not (t + '/').startswith('_plugin/delta/ytdlp/') and not (t + '/').startswith('plugin/embed/')]
ref.setdefault('plugins', {}).setdefault('plugin/video', {})['url'] = cache['url']
ref.setdefault('plugins', {}).pop('plugin/embed', None)
print(json.dumps({
'ref': [{
**cache,
**ref,
}],
}))
finally:
# Clean up both the base temp file and the downloaded file
if os.path.exists(base_name):
os.remove(base_name)
if downloaded_file and os.path.exists(downloaded_file):
os.remove(downloaded_file)
`,
},
};
export const ytdlpMetaDeltaPlugin: Plugin = {
tag: '_plugin/delta/ytmeta',
name: $localize`🔍️ Get Metadata`,
config: {
mod: $localize`▶️ YT-DLP`,
version: 1,
default: false,
generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
description: $localize`Fetches metadata and storyboards using yt-dlp.`,
language: 'python',
timeoutMs: 300_000,
requirements: `yt-dlp`,
// language=python
script: `
import json
import os
import sys
import yt_dlp
ref = json.load(sys.stdin)
plugins = ref.get('plugins', {})
embed = plugins.get('plugin/embed', {})
url = embed.get('url') or ref.get('url')
ydl_opts = {
'quiet': True,
'remote_components': ['ejs:github'],
'js_runtimes': {'bun': {'path': os.environ['JASPER_NODE']}},
'skip_download': True,
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
except Exception as e:
if 'This live event will begin in' in str(e):
sys.exit(0)
print(json.dumps({'error': str(e)}), file=sys.stderr)
sys.exit(1)
duration = info.get('duration')
storyboards = [f for f in info.get('formats', []) if f.get('format_note') == 'storyboard']
# Duration handling via tag (plugin/duration/...)
if duration:
m, s = divmod(duration, 60)
h, m = divmod(m, 60)
iso_dur = 'pt'
if h > 0: iso_dur += f'{int(h)}h'
if m > 0: iso_dur += f'{int(m)}m'
iso_dur += f'{int(s)}s'
tags = ref.get('tags', [])
# Filter out old durations and add the newly fetched one
tags = [t for t in tags if not t.startswith('plugin/duration/')]
tags.append(f'plugin/duration/{iso_dur}')
ref['tags'] = tags
# Storyboard handling
plugins = ref.get('plugins', {})
if storyboards:
# Target the best available storyboard spec
# Pick the storyboard with the highest individual frame resolution
spec = max(storyboards, key=lambda f: f.get('width', 0) * f.get('height', 0))
# URL contains $M placeholder for fragment index; use fragment 0
sb_url = spec.get('url', '').replace('$M', '0')
if sb_url:
sb_rows = int(spec.get('rows', 1))
sb_cols = int(spec.get('columns', 1))
plugins['plugin/thumbnail/storyboard'] = {
'url': sb_url,
'rows': sb_rows,
'cols': sb_cols,
'width': int(spec.get('width', 0)),
'height': int(spec.get('height', 0)),
}
tags = ref.get('tags', [])
if 'plugin/thumbnail/storyboard' not in tags:
tags.append('plugin/thumbnail/storyboard')
ref['tags'] = tags
ref['plugins'] = plugins
# Remove the yt trigger tag so it doesn't infinitely loop
ref['tags'] = [t for t in ref.get('tags', []) if not (t + '/').startswith('_plugin/delta/ytmeta/')]
ref.pop('metadata', None)
print(json.dumps({'ref': [ref]}))
`
},
};
export const ytdlpMod: Mod = {
plugin: [
ytdlpDeltaPlugin,
ytdlpMetaDeltaPlugin,
]
};
|