@barbapapazes/video-toolkit 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +60 -0
  2. package/dist/cli.mjs +13 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -141,6 +141,66 @@ Create an SVG file with a `{{text}}` placeholder where you want the text to appe
141
141
 
142
142
  The `{{text}}` placeholder will be replaced with the actual text you provide during thumbnail generation. The SVG should match the dimensions of your video frames (typically 1920x1080 for HD video).
143
143
 
144
+ ### Using Custom Fonts
145
+
146
+ To use custom fonts in your SVG templates, place the font files (`.ttf`, `.woff`, or `.woff2`) in the same directory as your SVG template. The toolkit will automatically embed them when rendering.
147
+
148
+ **Example structure:**
149
+
150
+ ```
151
+ ~/.config/video-toolkit/templates/
152
+ ├── my-template.svg
153
+ ├── SofiaSans-Light-300.ttf
154
+ ├── SofiaSans-Medium-500.ttf
155
+ └── SofiaSans-Bold-700.ttf
156
+ ```
157
+
158
+ **In your SVG:**
159
+
160
+ ```xml
161
+ <text font-family="Sofia Sans" font-size="128" font-weight="500">
162
+ {{text}}
163
+ </text>
164
+ ```
165
+
166
+ The toolkit will:
167
+ 1. Detect the `font-family="Sofia Sans"` attribute in your SVG
168
+ 2. Look for matching font files (e.g., `SofiaSans*.ttf`, `SofiaSans*.woff`)
169
+ 3. Automatically embed them as base64 data URIs
170
+ 4. Extract font weights from filenames (e.g., `-300`, `-Light` → weight 300)
171
+
172
+ **Font file naming conventions:**
173
+
174
+ - `FontName.ttf` → default weight (400)
175
+ - `FontName-300.ttf` or `FontName-Light.ttf` → weight 300
176
+ - `FontName-500.ttf` or `FontName-Medium.ttf` → weight 500
177
+ - `FontName-700.ttf` or `FontName-Bold.ttf` → weight 700
178
+
179
+ #### Google Fonts Fallback
180
+
181
+ If font files are not found locally, the toolkit will **automatically download them from Google Fonts**. This works for any font available on [fonts.google.com](https://fonts.google.com/).
182
+
183
+ **Example:**
184
+
185
+ ```xml
186
+ <text font-family="Roboto" font-size="80" font-weight="700">
187
+ {{text}}
188
+ </text>
189
+ ```
190
+
191
+ The toolkit will:
192
+ 1. Detect that "Roboto" isn't available locally
193
+ 2. Automatically download the font from Google Fonts with weight 700
194
+ 3. Embed it in your SVG for rendering
195
+
196
+ **Benefits:**
197
+ - No need to manually download font files for Google Fonts
198
+ - Works with 1400+ font families
199
+ - Automatically downloads only the weights used in your SVG
200
+ - Local font files take precedence (for custom/modified fonts)
201
+
202
+ This ensures your SVG text renders with the correct fonts as designed, whether using custom fonts or popular web fonts.
203
+
144
204
  ### Managing Multiple Templates
145
205
 
146
206
  You can have multiple templates for different purposes:
package/dist/cli.mjs CHANGED
@@ -1,3 +1,14 @@
1
1
  #!/usr/bin/env node
2
- import{basename as e,extname as t,isAbsolute as n,join as r,resolve as i}from"node:path";import a from"node:process";import{intro as o,isCancel as s,log as c,outro as l,select as u,spinner as d,text as f}from"@clack/prompts";import{cac as ee}from"cac";import{homedir as p}from"node:os";import{loadConfig as m}from"c12";import{copyFileSync as h,createReadStream as g,existsSync as _,mkdirSync as v,readFileSync as y,readdirSync as b,statSync as x,unlinkSync as S,writeFileSync as C}from"node:fs";import{format as w,parse as T}from"date-fns";import{Buffer as E}from"node:buffer";import D from"sharp";import{execSync as O}from"node:child_process";import k from"openai";var A=`0.8.2`;const j={openaiApiKey:``,language:`fr`,model:`whisper-1`,templatesDir:r(p(),`.config`,`video-toolkit`,`templates`)};async function M(){let{config:e}=await m({name:`video-toolkit`,defaults:j,globalRc:!0});return{openaiApiKey:e.openaiApiKey||``,language:e.language||`fr`,model:e.model||`whisper-1`,templatesDir:e.templatesDir}}function N(n){let a=i(n),o=e(a,t(a)),s=i(a,`..`),c=r(s,`${o}_audio.mp3`),l=r(s,`${o}.srt`),u=r(s,`thumbnails`);_(u)||v(u,{recursive:!0});let d=[`first`,`25`,`50`,`75`,`last`];return{videoPath:a,audioPath:c,srtPath:l,thumbnailTempPaths:d.map(e=>r(u,`${o}_thumbnail_${e}_temp.png`)),thumbnailPaths:d.map(e=>r(u,`${o}_thumbnail_${e}.png`))}}function P(e){return n(e)?e:i(a.cwd(),e)}function F(e){let n=P(e);if(!_(n))return[];try{return b(n,{withFileTypes:!0}).filter(e=>e.isFile()&&t(e.name).toLowerCase()===`.svg`).map(e=>e.name.replace(/\.svg$/i,``))}catch{return[]}}function I(e,t){return r(P(t),`${e}.svg`)}function L(e,t,r){let i=n(e)?e:I(e,r);if(!_(i))throw Error(`SVG template not found at ${i}. Please ensure the template exists in ${P(r)}.`);let a;try{a=y(i,`utf-8`)}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Failed to load SVG template from ${i}: ${t}`)}let o=t.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`).replace(/'/g,`&apos;`);return a.replace(/\{\{text\}\}/g,o)}async function R(e,t,n,r,i){try{let a=L(r,n,i),o=await D(e).metadata();if(!o.width||!o.height)throw Error(`Could not determine image dimensions`);let s=await D(E.from(a)).resize(o.width,o.height,{fit:`fill`}).png().toBuffer();await D(e).composite([{input:s,blend:`over`}]).toFile(t)}catch(e){throw console.error(`Error adding text to image:`,e),e}}function z(e=a.cwd()){let n=[];try{let i=b(e).filter(t=>x(r(e,t)).isDirectory()&&/^\d{4}$/.test(t));for(let a of i){let i=r(e,a),o=b(i).filter(e=>x(r(i,e)).isDirectory()&&/^\d{2}$/.test(e));for(let e of o){let o=r(i,e),s=b(o).filter(e=>x(r(o,e)).isDirectory()&&/^\d{2}$/.test(e));for(let i of s){let s=r(o,i),c=b(s,{withFileTypes:!0}).filter(e=>e.isFile()&&t(e.name).toLowerCase()===`.mp4`).map(e=>e.name);if(c.length>0){let t=`${a}-${e}-${i}`;n.push({path:s,date:new Date(t),displayPath:`${a}/${e}/${i}`,videoFile:c[0]})}}}}}catch(e){return c.error(`Error reading directories: ${e}`),[]}return n.sort((e,t)=>t.date.getTime()-e.date.getTime()).slice(0,8)}async function B(e=a.cwd()){let n=z(e);n.length===0&&(c.error(`No dated folders with video files found.`),c.info(`Expected structure: YYYY/MM/DD/*.mp4`),a.exit(1));let i=n.map(e=>({label:`${e.displayPath} (${e.videoFile})`,value:e.path,hint:`Process video from ${e.displayPath}`}));i.push({label:`Custom date`,value:`custom`,hint:`Enter a specific date`});let o=await u({message:`Select a content folder with video:`,options:i});if(s(o)&&(c.error(`Operation cancelled.`),a.exit(0)),o===`custom`){let n=await f({message:`Enter date (YYYY-MM-DD):`,placeholder:w(new Date,`yyyy-MM-dd`),validate:e=>{if(!e)return`Date is required`;if(!/^\d{4}-\d{2}-\d{2}$/.test(e))return`Invalid date format. Please use YYYY-MM-DD`;try{let t=T(e,`yyyy-MM-dd`,new Date);if(Number.isNaN(t.getTime()))return`Invalid date`}catch{return`Invalid date`}}});s(n)&&(c.error(`Operation cancelled.`),a.exit(0));let i=T(n,`yyyy-MM-dd`,new Date),o=r(e,w(i,`yyyy`),w(i,`MM`),w(i,`dd`));_(o)||(c.error(`Folder does not exist: ${o}`),a.exit(1));let l=b(o,{withFileTypes:!0}).filter(e=>e.isFile()&&t(e.name).toLowerCase()===`.mp4`).map(e=>e.name);return l.length===0&&(c.error(`No .mp4 files found in: ${o}`),a.exit(1)),{folderPath:o,videoPath:r(o,l[0])}}let l=n.find(e=>e.path===o);l||(c.error(`Selected folder not found.`),a.exit(1));let d=r(l.path,l.videoFile);return{folderPath:l.path,videoPath:d}}async function V(){let e=await f({message:`Enter text for the thumbnail (or press Enter to skip):`,placeholder:`Optional thumbnail text`});if(typeof e!=`symbol`)return e.trim()||void 0}async function H(e){let t=F(e);if(t.length===0){c.error(`No templates found in ${e}`),c.info(`Please add SVG templates to ${e} before proceeding.`);return}let n=await u({message:`Choose a template:`,options:t.map(e=>({label:e,value:e}))});if(typeof n!=`symbol`)return n}function U(e,t){let n=`ffmpeg -i ${e} -q:a 0 -map a ${t}`;try{O(n,{stdio:`inherit`})}catch(e){console.error(`Error extracting audio:`,e)}}function W(e){try{let t=O(`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${e}"`,{encoding:`utf-8`});return Number.parseFloat(t.trim())}catch(e){throw console.error(`Error getting video duration:`,e),e}}function G(e,t,n=`first`){let r=``;r=n===`first`?`ffmpeg -i "${e}" -vf "select=eq(n\\,0)" -vframes 1 "${t}" -y`:n===`last`?`ffmpeg -sseof -0.1 -i "${e}" -vframes 1 "${t}" -y`:`ffmpeg -ss ${W(e)*(Number.parseInt(n)/100)} -i "${e}" -vframes 1 "${t}" -y`;try{O(r,{stdio:`inherit`})}catch(e){throw console.error(`Error extracting thumbnail:`,e),e}}async function K(e,t){return await new k({apiKey:t.openaiApiKey}).audio.transcriptions.create({file:g(e),model:t.model,language:t.language,response_format:`srt`})}async function q(e,t){let n=d();n.start(`Extracting audio...`);try{U(e,t),n.stop(`Audio extracted successfully`)}catch(e){n.stop(`Audio extraction failed`),c.error(`Error during audio extraction: ${e}`),a.exit(1)}}async function J(e,t,n){let r=d();r.start(`Generating transcription with OpenAI...`);try{C(t,await K(e,n)),r.stop(`Transcription generated successfully`)}catch(t){r.stop(`Transcription generation failed`),c.error(`Error during transcription generation: ${t}`);try{S(e)}catch{}a.exit(1)}}async function Y(e,t,n){let r=d(),i=n===`first`?`start`:n===`last`?`end`:`${n}%`;r.start(`Extracting thumbnail at ${i}...`);try{G(e,t,n),r.stop(`Thumbnail extracted successfully`)}catch(e){r.stop(`Thumbnail extraction failed`),c.error(`Error during thumbnail extraction: ${e}`),a.exit(1)}}async function X(e,t,n,r,i){let o=d();o.start(`Adding text to thumbnail...`);try{await R(e,t,n,r,i),o.stop(`Text added to thumbnail successfully`)}catch(e){o.stop(`Failed to add text to thumbnail`),c.error(`Error adding text to thumbnail: ${e}`),a.exit(1)}}function Z(e,t=`temporary file`){try{S(e)}catch{c.warn(`Could not delete ${t}: ${e}`)}}async function Q(e){o(`Video Toolkit - Add Template`);let t=await M(),n=i(e);_(n)||(c.error(`Source file not found: ${n}`),a.exit(1)),n.toLowerCase().endsWith(`.svg`)||(c.error(`Source file must be an SVG file`),a.exit(1));let r=await f({message:`Enter a name for this template:`,placeholder:`e.g., series-x, youtube-intro`,validate:e=>{if(!e||e.trim()===``)return`Template name is required`;if(!/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(e))return`Template name should start and end with lowercase letters or numbers, and can contain hyphens in between`}});typeof r==`symbol`&&(c.error(`Template name is required. Exiting.`),a.exit(1));let s=P(t.templatesDir);_(s)||(v(s,{recursive:!0}),c.success(`Created templates directory: ${s}`));let u=i(s,`${r}.svg`);_(u)&&c.warn(`Template '${r}' already exists. Overwriting...`);try{h(n,u),l(`✓ Template '${r}' added successfully at ${u}`)}catch(e){let t=e instanceof Error?e.message:String(e);c.error(`Failed to copy template: ${t}`),a.exit(1)}}const $=ee(`video-toolkit`);async function te(t){let n=await M();n.openaiApiKey||(c.error(`OpenAI API key is required. Please set it in:`),c.error(` - Config file: ~/.video-toolkitrc`),a.exit(1));let{videoPath:r,audioPath:i,srtPath:s,thumbnailTempPaths:u,thumbnailPaths:d}=N(t);o(`Video Toolkit - Processing: ${e(r)}`),await q(r,i),await J(i,s,n),Z(i,`audio file`);let f=await V();if(f){let e=await H(n.templatesDir);if(!e){c.warn(`No template selected. Skipping thumbnail generation.`),l(`✓ Transcription saved to: ${s}`);return}let t=[`first`,`25`,`50`,`75`,`last`];for(let i=0;i<t.length;i++)await Y(r,u[i],t[i]),await X(u[i],d[i],f,e,n.templatesDir),Z(u[i],`temporary thumbnail`);l(`✓ Transcription saved to: ${s}\n✓ Thumbnails saved:\n - ${d.join(`
3
- - `)}`)}else l(`✓ Transcription saved to: ${s}`)}$.command(`[path]`,`Process a video from dated content folders`).option(`--path <path>`,`Base path for content directory (defaults to current directory)`).action(async(e,t)=>{let n=e||t?.path||a.cwd();o(`Video Toolkit - Process video from content folder`);let{videoPath:r}=await B(n);await te(r)}),$.command(`add-template <file>`,`Add an SVG template to the templates directory`).action(async e=>{await Q(e)}),$.help(),$.version(A),$.parse();export{};
2
+ import{basename as e,dirname as t,extname as n,isAbsolute as r,join as i,resolve as a}from"node:path";import o from"node:process";import{intro as s,isCancel as c,log as l,outro as u,select as d,spinner as f,text as p}from"@clack/prompts";import{cac as ee}from"cac";import{homedir as te}from"node:os";import{loadConfig as m}from"c12";import{copyFileSync as h,createReadStream as g,existsSync as _,mkdirSync as v,readFileSync as y,readdirSync as b,statSync as x,unlinkSync as S,writeFileSync as C}from"node:fs";import{format as w,parse as T}from"date-fns";import{Buffer as E}from"node:buffer";import{ofetch as D}from"ofetch";import O from"sharp";import{execSync as k}from"node:child_process";import A from"openai";var j=`0.9.0`;const M={openaiApiKey:``,language:`fr`,model:`whisper-1`,templatesDir:i(te(),`.config`,`video-toolkit`,`templates`)};async function N(){let{config:e}=await m({name:`video-toolkit`,defaults:M,globalRc:!0});return{openaiApiKey:e.openaiApiKey||``,language:e.language||`fr`,model:e.model||`whisper-1`,templatesDir:e.templatesDir}}function P(t){let r=a(t),o=e(r,n(r)),s=a(r,`..`),c=i(s,`${o}_audio.mp3`),l=i(s,`${o}.srt`),u=i(s,`thumbnails`);_(u)||v(u,{recursive:!0});let d=[`first`,`25`,`50`,`75`,`last`];return{videoPath:r,audioPath:c,srtPath:l,thumbnailTempPaths:d.map(e=>i(u,`${o}_thumbnail_${e}_temp.png`)),thumbnailPaths:d.map(e=>i(u,`${o}_thumbnail_${e}.png`))}}function F(e){return r(e)?e:a(o.cwd(),e)}function I(e){let t=F(e);if(!_(t))return[];try{return b(t,{withFileTypes:!0}).filter(e=>e.isFile()&&n(e.name).toLowerCase()===`.svg`).map(e=>e.name.replace(/\.svg$/i,``))}catch{return[]}}function L(e,t){return i(F(t),`${e}.svg`)}async function R(e,t){try{let n=await D(`https://fonts.googleapis.com/css2?family=${e.replace(/\s+/g,`+`)}:wght@${t}&display=swap`),r=n.match(/url\((https:\/\/fonts\.gstatic\.com\/[^)]+)\)/),i=n.match(/format\('([^']+)'\)/);if(!r||!i)return null;let a=r[1],o=i[1],s=await D(a,{responseType:`arrayBuffer`});return{base64:E.from(s).toString(`base64`),format:o,mimeType:{woff2:`font/woff2`,woff:`font/woff`,truetype:`font/ttf`,opentype:`font/otf`}[o]||`font/woff2`}}catch(n){return console.warn(`Could not download font "${e}" weight ${t} from Google Fonts:`,n),null}}function z(e,t){let n=new Set,r=RegExp(`<text[^>]*font-family="${t.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}"[^>]*>`,`g`),i=r.exec(e);for(;i!==null;){let t=i[0].match(/font-weight="(\d+)"/);t?n.add(t[1]):n.add(`400`),i=r.exec(e)}return n.size>0?Array.from(n):[`400`]}async function B(r,a){let o=t(a),s=/font-family="([^"]+)"/g,c=new Set,l=s.exec(r);for(;l!==null;)c.add(l[1]),l=s.exec(r);if(c.size===0)return r;let u=``;for(let t of c){let a=t.replace(/\s+/g,``),s=!1;try{let r=b(o,{withFileTypes:!0});for(let c of r){if(!c.isFile())continue;let r=c.name,l=n(r).toLowerCase();if([`.ttf`,`.woff`,`.woff2`].includes(l)&&e(r,l).toLowerCase().includes(a.toLowerCase())){s=!0;let e=y(i(o,r)).toString(`base64`),n={".ttf":`font/ttf`,".woff":`font/woff`,".woff2":`font/woff2`}[l]||`font/ttf`,a=`400`,c=r.match(/-(\d{3})/);c?a=c[1]:r.toLowerCase().includes(`light`)?a=`300`:r.toLowerCase().includes(`bold`)?a=`700`:r.toLowerCase().includes(`medium`)&&(a=`500`),u+=`
3
+ @font-face {
4
+ font-family: '${t}';
5
+ font-weight: ${a};
6
+ src: url(data:${n};base64,${e}) format('${l.slice(1)}');
7
+ }`}}}catch(e){console.warn(`Could not load fonts from ${o}:`,e)}if(!s){console.warn(`Font "${t}" not found locally, attempting to download from Google Fonts...`);let e=z(r,t);for(let n of e){let e=await R(t,n);e&&(u+=`
8
+ @font-face {
9
+ font-family: '${t}';
10
+ font-weight: ${n};
11
+ src: url(data:${e.mimeType};base64,${e.base64}) format('${e.format}');
12
+ }`,console.warn(`Successfully downloaded "${t}" weight ${n} from Google Fonts`))}}}return u?r.includes(`<defs>`)?r.replace(`<defs>`,`<defs><style>${u}\n </style>`):r.includes(`<svg`)?r.replace(`<svg`,`<svg><defs><style>${u}\n </style></defs>`).replace(`><defs>`,`>
13
+ <defs>`):r:r}async function V(e,t,n){let i=r(e)?e:L(e,n);if(!_(i))throw Error(`SVG template not found at ${i}. Please ensure the template exists in ${F(n)}.`);let a;try{a=y(i,`utf-8`)}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Failed to load SVG template from ${i}: ${t}`)}let o=t.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`).replace(/'/g,`&apos;`);return await B(a.replace(/\{\{text\}\}/g,o),i)}async function H(e,t,n,r,i){try{let a=await V(r,n,i),o=await O(e).metadata();if(!o.width||!o.height)throw Error(`Could not determine image dimensions`);let s=await O(E.from(a),{density:300}).resize(o.width,o.height,{fit:`fill`}).png().toBuffer();await O(e).composite([{input:s,blend:`over`}]).toFile(t)}catch(e){throw console.error(`Error adding text to image:`,e),e}}function U(e=o.cwd()){let t=[];try{let r=b(e).filter(t=>x(i(e,t)).isDirectory()&&/^\d{4}$/.test(t));for(let a of r){let r=i(e,a),o=b(r).filter(e=>x(i(r,e)).isDirectory()&&/^\d{2}$/.test(e));for(let e of o){let o=i(r,e),s=b(o).filter(e=>x(i(o,e)).isDirectory()&&/^\d{2}$/.test(e));for(let r of s){let s=i(o,r),c=b(s,{withFileTypes:!0}).filter(e=>e.isFile()&&n(e.name).toLowerCase()===`.mp4`).map(e=>e.name);if(c.length>0){let n=`${a}-${e}-${r}`;t.push({path:s,date:new Date(n),displayPath:`${a}/${e}/${r}`,videoFile:c[0]})}}}}}catch(e){return l.error(`Error reading directories: ${e}`),[]}return t.sort((e,t)=>t.date.getTime()-e.date.getTime()).slice(0,8)}async function W(e=o.cwd()){let t=U(e);t.length===0&&(l.error(`No dated folders with video files found.`),l.info(`Expected structure: YYYY/MM/DD/*.mp4`),o.exit(1));let r=t.map(e=>({label:`${e.displayPath} (${e.videoFile})`,value:e.path,hint:`Process video from ${e.displayPath}`}));r.push({label:`Custom date`,value:`custom`,hint:`Enter a specific date`});let a=await d({message:`Select a content folder with video:`,options:r});if(c(a)&&(l.error(`Operation cancelled.`),o.exit(0)),a===`custom`){let t=await p({message:`Enter date (YYYY-MM-DD):`,placeholder:w(new Date,`yyyy-MM-dd`),validate:e=>{if(!e)return`Date is required`;if(!/^\d{4}-\d{2}-\d{2}$/.test(e))return`Invalid date format. Please use YYYY-MM-DD`;try{let t=T(e,`yyyy-MM-dd`,new Date);if(Number.isNaN(t.getTime()))return`Invalid date`}catch{return`Invalid date`}}});c(t)&&(l.error(`Operation cancelled.`),o.exit(0));let r=T(t,`yyyy-MM-dd`,new Date),a=i(e,w(r,`yyyy`),w(r,`MM`),w(r,`dd`));_(a)||(l.error(`Folder does not exist: ${a}`),o.exit(1));let s=b(a,{withFileTypes:!0}).filter(e=>e.isFile()&&n(e.name).toLowerCase()===`.mp4`).map(e=>e.name);return s.length===0&&(l.error(`No .mp4 files found in: ${a}`),o.exit(1)),{folderPath:a,videoPath:i(a,s[0])}}let s=t.find(e=>e.path===a);s||(l.error(`Selected folder not found.`),o.exit(1));let u=i(s.path,s.videoFile);return{folderPath:s.path,videoPath:u}}async function G(){let e=await p({message:`Enter text for the thumbnail (or press Enter to skip):`,placeholder:`Optional thumbnail text`});if(typeof e!=`symbol`)return e.trim()||void 0}async function K(e){let t=I(e);if(t.length===0){l.error(`No templates found in ${e}`),l.info(`Please add SVG templates to ${e} before proceeding.`);return}let n=await d({message:`Choose a template:`,options:t.map(e=>({label:e,value:e}))});if(typeof n!=`symbol`)return n}function q(e,t){let n=`ffmpeg -i ${e} -q:a 0 -map a ${t}`;try{k(n,{stdio:`inherit`})}catch(e){console.error(`Error extracting audio:`,e)}}function J(e){try{let t=k(`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${e}"`,{encoding:`utf-8`});return Number.parseFloat(t.trim())}catch(e){throw console.error(`Error getting video duration:`,e),e}}function Y(e,t,n=`first`){let r=``;r=n===`first`?`ffmpeg -i "${e}" -vf "select=eq(n\\,0)" -vframes 1 "${t}" -y`:n===`last`?`ffmpeg -sseof -0.1 -i "${e}" -vframes 1 "${t}" -y`:`ffmpeg -ss ${J(e)*(Number.parseInt(n)/100)} -i "${e}" -vframes 1 "${t}" -y`;try{k(r,{stdio:`inherit`})}catch(e){throw console.error(`Error extracting thumbnail:`,e),e}}async function X(e,t){return await new A({apiKey:t.openaiApiKey}).audio.transcriptions.create({file:g(e),model:t.model,language:t.language,response_format:`srt`})}async function Z(e,t){let n=f();n.start(`Extracting audio...`);try{q(e,t),n.stop(`Audio extracted successfully`)}catch(e){n.stop(`Audio extraction failed`),l.error(`Error during audio extraction: ${e}`),o.exit(1)}}async function ne(e,t,n){let r=f();r.start(`Generating transcription with OpenAI...`);try{C(t,await X(e,n)),r.stop(`Transcription generated successfully`)}catch(t){r.stop(`Transcription generation failed`),l.error(`Error during transcription generation: ${t}`);try{S(e)}catch{}o.exit(1)}}async function re(e,t,n){let r=f(),i=n===`first`?`start`:n===`last`?`end`:`${n}%`;r.start(`Extracting thumbnail at ${i}...`);try{Y(e,t,n),r.stop(`Thumbnail extracted successfully`)}catch(e){r.stop(`Thumbnail extraction failed`),l.error(`Error during thumbnail extraction: ${e}`),o.exit(1)}}async function ie(e,t,n,r,i){let a=f();a.start(`Adding text to thumbnail...`);try{await H(e,t,n,r,i),a.stop(`Text added to thumbnail successfully`)}catch(e){a.stop(`Failed to add text to thumbnail`),l.error(`Error adding text to thumbnail: ${e}`),o.exit(1)}}function Q(e,t=`temporary file`){try{S(e)}catch{l.warn(`Could not delete ${t}: ${e}`)}}async function ae(e){s(`Video Toolkit - Add Template`);let t=await N(),n=a(e);_(n)||(l.error(`Source file not found: ${n}`),o.exit(1)),n.toLowerCase().endsWith(`.svg`)||(l.error(`Source file must be an SVG file`),o.exit(1));let r=await p({message:`Enter a name for this template:`,placeholder:`e.g., series-x, youtube-intro`,validate:e=>{if(!e||e.trim()===``)return`Template name is required`;if(!/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(e))return`Template name should start and end with lowercase letters or numbers, and can contain hyphens in between`}});typeof r==`symbol`&&(l.error(`Template name is required. Exiting.`),o.exit(1));let i=F(t.templatesDir);_(i)||(v(i,{recursive:!0}),l.success(`Created templates directory: ${i}`));let c=a(i,`${r}.svg`);_(c)&&l.warn(`Template '${r}' already exists. Overwriting...`);try{h(n,c),u(`✓ Template '${r}' added successfully at ${c}`)}catch(e){let t=e instanceof Error?e.message:String(e);l.error(`Failed to copy template: ${t}`),o.exit(1)}}const $=ee(`video-toolkit`);async function oe(t){let n=await N();n.openaiApiKey||(l.error(`OpenAI API key is required. Please set it in:`),l.error(` - Config file: ~/.video-toolkitrc`),o.exit(1));let{videoPath:r,audioPath:i,srtPath:a,thumbnailTempPaths:c,thumbnailPaths:d}=P(t);s(`Video Toolkit - Processing: ${e(r)}`),await Z(r,i),await ne(i,a,n),Q(i,`audio file`);let f=await G();if(f){let e=await K(n.templatesDir);if(!e){l.warn(`No template selected. Skipping thumbnail generation.`),u(`✓ Transcription saved to: ${a}`);return}let t=[`first`,`25`,`50`,`75`,`last`];for(let i=0;i<t.length;i++)await re(r,c[i],t[i]),await ie(c[i],d[i],f,e,n.templatesDir),Q(c[i],`temporary thumbnail`);u(`✓ Transcription saved to: ${a}\n✓ Thumbnails saved:\n - ${d.join(`
14
+ - `)}`)}else u(`✓ Transcription saved to: ${a}`)}$.command(`[path]`,`Process a video from dated content folders`).option(`--path <path>`,`Base path for content directory (defaults to current directory)`).action(async(e,t)=>{let n=e||t?.path||o.cwd();s(`Video Toolkit - Process video from content folder`);let{videoPath:r}=await W(n);await oe(r)}),$.command(`add-template <file>`,`Add an SVG template to the templates directory`).action(async e=>{await ae(e)}),$.help(),$.version(j),$.parse();export{};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@barbapapazes/video-toolkit",
3
3
  "type": "module",
4
- "version": "0.8.2",
4
+ "version": "0.9.0",
5
5
  "author": "Estéban Soubiran <esteban@soubiran.dev>",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/Barbapapazes",