33 lines
3.6 KiB
JavaScript
33 lines
3.6 KiB
JavaScript
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import {ffmpeg,run,probe} from './media.mjs';
|
|
import {duration} from './project.mjs';
|
|
|
|
// Sampling is evidence for a person to review, never an automatic semantic approval.
|
|
export async function auditExport(project,file,options={},signal,progress){
|
|
file=path.resolve(file);
|
|
const meta=await probe(file,signal),v=meta.streams.find(s=>s.codec_type==='video');
|
|
if(!v)throw Error('导出文件没有视频流');
|
|
const fps=project.settings.fps,total=Number(meta.format.duration),count=Number(v.nb_frames)||Math.round(total*fps);
|
|
const shots=project.clips.filter(c=>c.track==='video').sort((a,b)=>a.start-b.start);
|
|
const samples=shots.flatMap((c,i)=>{
|
|
const first=Math.round(c.start*fps),last=Math.min(count-1,Math.round((c.start+duration(c))*fps)-1);
|
|
return [first,Math.floor((first+last)/2),last].map((frame,j)=>({clip:i+1,clipId:c.id,position:['起帧','中帧','尾帧'][j],frame,time:frame/fps,sourceTime:c.reverse?c.out-(frame/fps-c.start)*c.speed:c.in+(frame/fps-c.start)*(c.speed||1),mediaId:c.mediaId}));
|
|
}).filter(s=>s.frame>=0&&s.frame<count);
|
|
if(!samples.length)samples.push(...[0,Math.floor(count/2),count-1].map(frame=>({clip:0,position:'全片',frame,time:frame/fps})));
|
|
const dir=await fs.mkdtemp(file+'.frames-'),raw=path.join(dir,'frames');await fs.mkdir(raw);
|
|
const unique=[...new Set(samples.map(s=>s.frame))].sort((a,b)=>a-b);
|
|
const font=(process.platform==='win32'?path.join(process.env.WINDIR||'C:/Windows','Fonts','consola.ttf'):'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf').replaceAll('\\','/').replaceAll(':','\\:');
|
|
// Batch expressions to stay below Windows process command line limits on large projects.
|
|
let serial=1;for(let begin=0;begin<unique.length;begin+=160){
|
|
const frames=unique.slice(begin,begin+160);await run(ffmpeg,['-y','-v','error',...(options.acceleration==='gpu'?['-hwaccel','cuda']:[]),'-i',file,'-vf',`select='${frames.map(n=>`eq(n,${n})`).join('+')}',scale=384:216,drawtext=fontfile='${font}':text='%{pts\\:hms}':fontsize=19:fontcolor=white:box=1:boxcolor=black@0.8:x=5:y=5`,'-fps_mode','vfr','-start_number',String(serial),'-frames:v',String(frames.length),path.join(raw,'%06d.jpg')],{signal});serial+=frames.length;progress?.(.5*serial/unique.length,'提取成片起帧、中帧、尾帧');
|
|
}
|
|
const pages=[];
|
|
for(let i=0;i<samples.length;i++){const s=samples[i];await fs.copyFile(path.join(raw,`${String(unique.indexOf(s.frame)+1).padStart(6,'0')}.jpg`),path.join(dir,`sample-${String(i+1).padStart(6,'0')}.jpg`));}
|
|
for(let offset=0;offset<samples.length;offset+=18){const size=Math.min(18,samples.length-offset),page=path.join(dir,`检查页-${pages.length+1}.jpg`);await run(ffmpeg,['-y','-v','error','-start_number',String(offset+1),'-i',path.join(dir,'sample-%06d.jpg'),'-vf',`tile=3x${Math.ceil(size/3)}:nb_frames=${size}:padding=2:margin=2:color=0x152030`,'-frames:v','1',page],{signal});pages.push(page);progress?.(.5+.5*(offset+size)/samples.length,'生成抽帧对照页');}
|
|
const report={status:'needs_visual_review',notice:'每行是一个片段的起帧、中帧、尾帧。时间码来自最终成片。抽帧成功不等于内容、动作或剧情已正确;须人工逐页核对。',video:file,duration:total,fps,frames:count,clips:shots.length,sampleCount:samples.length,samples,pages};
|
|
const reportPath=path.join(dir,'抽帧报告.json');await fs.writeFile(reportPath,JSON.stringify(report,null,2));
|
|
await fs.writeFile(path.join(dir,'检查说明.txt'),report.notice+'\n源片段映射见抽帧报告.json。');
|
|
return {reportPath,pages,sampleCount:samples.length,notice:report.notice};
|
|
}
|