@@ -61,7 +61,11 @@ export default async (req, res) => {
6161 Math . min ( Math . max ( Number ( req . query . minDuration ) || 0 , 0.5 ) , 2 ) , // default: 0.5s before and after t, range: 0.5s ~ 2.0s
6262 Math . min ( Math . max ( Number ( req . query . maxDuration ) || 5 , 0.5 ) , 5 ) , // default: 5.0s before and after t, range: 0.5s ~ 5.0s
6363 ) ;
64- if ( ! scene ) return res . status ( 500 ) . send ( "Internal Server Error" ) ;
64+ if ( scene === null ) {
65+ return res . status ( 500 ) . send ( "Internal Server Error" ) ;
66+ }
67+
68+ const muted = "mute" in req . query ;
6569
6670 res . set ( "Cache-Control" , "max-age=86400" ) ;
6771 res . set ( "Content-Type" , "video/mp4" ) ;
@@ -71,27 +75,23 @@ export default async (req, res) => {
7175 res . set ( "x-video-duration" , scene . duration ) ;
7276 res . set ( "Access-Control-Expose-Headers" , "x-video-start, x-video-end, x-video-duration" ) ;
7377
74- const ffmpeg = generateVideoPreview (
75- videoFilePath ,
76- scene . start ,
77- scene . end ,
78- size ,
79- "mute" in req . query ,
80- ) ;
78+ const ffmpeg = generateVideoPreview ( videoFilePath , scene . start , scene . end , size , muted ) ;
8179
8280 ffmpeg . stdout . pipe ( res ) ;
8381
8482 await new Promise < void > ( ( resolve ) => {
8583 ffmpeg . on ( "close" , ( ) => resolve ( ) ) ;
8684 ffmpeg . on ( "error" , ( err ) => {
8785 console . log ( err ) ;
88- res . end ( ) ;
86+ res . end ( ) ; // terminate the stream to signal error to client
8987 resolve ( ) ;
9088 } ) ;
9189 } ) ;
9290 } catch ( e ) {
9391 console . log ( e ) ;
94- if ( ! res . headersSent ) res . status ( 500 ) . send ( "Internal Server Error" ) ;
92+ if ( ! res . headersSent ) {
93+ res . status ( 500 ) . send ( "Internal Server Error" ) ;
94+ }
9595 }
9696 req . app . locals . mediaQueue -- ;
9797} ;
0 commit comments