@@ -193,58 +193,28 @@ export const processIdx = async (
193193 await $ `${ paths . ffmpeg } -f lavfi -i color=size=${ size } :duration=59.07:rate=${ fps } :color=black@0.0,format=rgba -i ${ filename } .rw.idx -filter_complex "[0:v][1:s]overlay[v]" -map "[v]" -f image2 -frame_pts true -c:s png -vsync 0 -frames:v ${ idx . paragraphs . length + 1 } ${ path . join ( dir , "fabscript" , "IMAGE%03d.png" ) } -y` . quiet ( ) ;
194194 await $ `rm ${ filename } .rw.idx ${ filename } .rw.sub ${ path . join ( dir , "fabscript" , "IMAGE000.png" ) } ` ;
195195
196+ const [ width , height ] = size . split ( "x" ) ;
196197 const out = path . join ( dir , "fabscript" , "Fab_Image_script.txt" ) ;
197198 await Bun . write (
198199 out ,
199200 idx . paragraphs
200- . map (
201- // WARNING: using the uncropped subtitles is experimental, not sure how it performs yet
202- ( paragraph , i ) =>
203- `IMAGE${ ( i + 1 ) . toString ( ) . padStart ( 3 , "0" ) } .png ${ secToTimestamp ( paragraph . startTime / 1000 , ";" ) } 0 0 0 0` ,
204- )
201+ . map ( ( paragraph , i ) => {
202+ // I don't know how to correctly determine subtitle duration
203+ // currently, so we're making a guess and avoiding overlaps.
204+ const nextStartTime = idx . paragraphs [ i + 1 ] ?. startTime ;
205+ const maxDuration = 6000 ;
206+ let nextTs = secToTimestamp (
207+ ( nextStartTime !== undefined
208+ ? Math . min ( paragraph . startTime + maxDuration , nextStartTime - 500 )
209+ : paragraph . startTime + maxDuration ) / 1000 ,
210+ ";" ,
211+ ) ;
212+
213+ return `IMAGE${ ( i + 1 ) . toString ( ) . padStart ( 3 , "0" ) } .png ${ secToTimestamp ( paragraph . startTime / 1000 , ";" ) } ${ nextTs } 0 0 ${ width } ${ height } ` ;
214+ } )
205215 . join ( "\n" ) ,
206216 ) ;
207217
208- // const glob = new Bun.Glob(`${filename}.frame*.png`);
209- // for await (const file of glob.scan()) {
210- // // TODO: crop each image - https://stackoverflow.com/a/62378527
211- // await $`${paths.ffmpeg} -loop 1 -i ${}`
212- // }
213-
214- // Extract single image from PES
215- // const file = await Bun.file("test/charliesub.sub").bytes();
216- // const buffers: Buffer[] = [];
217-
218- // let i = -1;
219- // for (const para of idx.paragraphs) {
220- // i += 1;
221- // const start = para.filePosition;
222-
223- // const nextPos = idx.paragraphs[i + 1]?.filePosition;
224- // const end = nextPos === undefined ? file.length : nextPos;
225- // const packet = file.slice(start, end);
226- // console.log({ len: packet.length, start, end });
227-
228- // // According to https://dvd.sourceforge.net/dvdinfo/pes-hdr.html,
229- // // the stream with ID 0xBD (189) contains the subpictures, but in my
230- // // testing with SubtitleEdit-generated .sub files, the only stream IDs I
231- // // encountered were ID 0xBA (186).
232- // const streamId = packet[3];
233- // // if (streamId === 0xba) {
234- // // console.log("subpicture stream");
235- // // }
236- // // I think this is right?
237- // const packetLength = Number(`${packet[4] ?? ""}${packet[5] ?? ""}`);
238- // const headerLength = packet[8];
239- // console.log({ streamId, packetLength, headerLength });
240-
241- // const firstByte = 5 + (headerLength ?? 0);
242- // const rest = packet.slice(firstByte, packetLength);
243- // if (i === 0) {
244- // await Bun.write(`test/${para.startTime}.bmp`, rest);
245- // }
246- // }
247-
248218 return out ;
249219} ;
250220
0 commit comments