11// Builds the final mp4 inside S3: part 1 carries the stitched header, the other parts copy each
22// chunk's mdat payload with UploadPartCopy, so almost no video byte passes through the function.
3- // Chunks under S3's 5 MiB part minimum cannot be copied; their parts can go to helper invocations.
43
54import {
65 type CompletedPart ,
@@ -13,7 +12,6 @@ import { buildStitchedHeader, readMp4Layout } from "@hyperframes/producer/distri
1312import { multipartUpload , type MultipartTarget , parseS3Uri } from "./s3Transport.js" ;
1413
1514const MIN_PART_BYTES = 5 * 1024 * 1024 ;
16- // One helper's share: ~0.5 s each way on a Lambda's ~80 MB/s link.
1715const HELPER_BATCH_BYTES = 32 * 1024 * 1024 ;
1816
1917/** Bytes `[start, end)` of an S3 object. */
@@ -27,16 +25,13 @@ export type Part = { copy: RangeSegment } | { upload: Segment[] };
2725
2826const segmentSize = ( s : Segment ) : number => ( "bytes" in s ? s . bytes . length : s . end - s . start ) ;
2927
30- /** An upload part made only of S3 ranges, so another invocation can build it. */
3128export interface RangePart {
3229 partNumber : number ;
3330 ranges : RangeSegment [ ] ;
3431}
35- /** Uploads `parts` into `target` elsewhere and resolves their ETags in order. */
3632export type SendRangeParts = ( target : MultipartTarget , parts : RangePart [ ] ) => Promise < string [ ] > ;
3733export interface StitchHelper {
3834 send : SendRangeParts ;
39- /** Invocations the helper function can run at once (its reserved concurrency). */
4035 maxInvocations : number ;
4136}
4237
@@ -119,10 +114,7 @@ const rangesOnly = (part: Part): RangeSegment[] | undefined =>
119114const rangeBytes = ( ranges : readonly RangeSegment [ ] ) =>
120115 ranges . reduce ( ( total , r ) => total + segmentSize ( r ) , 0 ) ;
121116
122- /**
123- * Groups the range-only upload parts into batches of about `HELPER_BATCH_BYTES`, grown so there
124- * are never more than `maxBatches`: every closed batch holds at least total / maxBatches bytes.
125- */
117+ // Every closed batch holds at least total / maxBatches bytes, so there are never more than maxBatches.
126118export function helperBatches ( parts : readonly Part [ ] , maxBatches = Infinity ) : RangePart [ ] [ ] {
127119 const total = parts . reduce ( ( sum , part ) => sum + rangeBytes ( rangesOnly ( part ) ?? [ ] ) , 0 ) ;
128120 const batchBytes = Math . max ( HELPER_BATCH_BYTES , Math . ceil ( total / maxBatches ) ) ;
@@ -143,7 +135,6 @@ export function helperBatches(parts: readonly Part[], maxBatches = Infinity): Ra
143135 return batches ;
144136}
145137
146- /** A helper's side: builds each range part and returns the ETags in order. */
147138export async function sendRangeParts (
148139 s3 : S3Client ,
149140 target : MultipartTarget ,
@@ -204,8 +195,8 @@ export interface StitchResult {
204195 fileSize : number ;
205196 parts : number ;
206197 downloadedBytes : number ;
207- /** Helper invocations that built the range-only parts (0 without a helper). */
208198 helpers : number ;
199+ helperFallbacks : number ;
209200}
210201
211202/** One faststart mp4 at `destUri` from the stitched mp4 chunks at `chunkUris`, plus `audioUri`'s track when given. */
@@ -235,12 +226,24 @@ export async function stitchMp4InS3(
235226 } ) ) ,
236227 ] ;
237228 const parts = planParts ( segments , minPartBytes ) ;
238- await uploadParts ( s3 , parts , destUri , "video/mp4" , helper ) ;
229+ let helperFallbacks = 0 ;
230+ const helperOrLocal = helper && {
231+ maxInvocations : helper . maxInvocations ,
232+ // A throttled helper (another render may hold the reservation) costs time, not the render.
233+ send : ( target : MultipartTarget , batch : RangePart [ ] ) =>
234+ helper . send ( target , batch ) . catch ( ( error : unknown ) => {
235+ helperFallbacks ++ ;
236+ console . warn ( `[s3Stitch] helper failed, building ${ batch . length } parts here:` , error ) ;
237+ return sendRangeParts ( s3 , target , batch ) ;
238+ } ) ,
239+ } ;
240+ await uploadParts ( s3 , parts , destUri , "video/mp4" , helperOrLocal ) ;
239241 const uploaded = parts . flatMap ( ( part ) => ( "upload" in part ? part . upload : [ ] ) ) ;
240242 return {
241243 fileSize : segments . reduce ( ( total , s ) => total + segmentSize ( s ) , 0 ) ,
242244 parts : parts . length ,
243245 downloadedBytes : uploaded . reduce ( ( total , s ) => total + ( "uri" in s ? segmentSize ( s ) : 0 ) , 0 ) ,
244246 helpers : helper ? helperBatches ( parts , helper . maxInvocations ) . length : 0 ,
247+ helperFallbacks,
245248 } ;
246249}
0 commit comments