@@ -11,6 +11,7 @@ import imageminMozjpeg from "imagemin-mozjpeg"
11
11
import PQueue from "p-queue"
12
12
import sharp from "sharp"
13
13
import { createFileNodeFromBuffer } from "gatsby-source-filesystem"
14
+ import reporter from "gatsby-cli/lib/reporter"
14
15
15
16
import { cacheContentfulVideo } from "./helpers"
16
17
@@ -53,14 +54,17 @@ export default class FFMPEG {
53
54
} )
54
55
55
56
// Execute FFMMPEG and log progress
56
- executeFfmpeg = async ( { ffmpegSession, cachePath, loggingPrefix } ) => {
57
+ executeFfmpeg = async ( { ffmpegSession, cachePath } ) => {
57
58
let startTime
58
59
let lastLoggedPercent = 0.1
59
60
61
+ const { name } = parse ( cachePath )
62
+
60
63
return new Promise ( ( resolve , reject ) => {
61
64
ffmpegSession
62
65
. on ( `start` , commandLine => {
63
- console . log ( `${ loggingPrefix } Executing:\n\n${ commandLine } \n` )
66
+ reporter . info ( `${ name } - converting` )
67
+ reporter . verbose ( `${ name } - executing:\n\n${ commandLine } \n` )
64
68
startTime = performance . now ( )
65
69
} )
66
70
. on ( `progress` , progress => {
@@ -74,18 +78,19 @@ export default class FFMPEG {
74
78
const loggedTimeLeft =
75
79
estTimeLeft !== Infinity && ` (~${ estTimeLeft } s)`
76
80
77
- console . log ( `${ loggingPrefix } ${ percent } %${ loggedTimeLeft } ` )
81
+ reporter . info ( `${ name } - ${ percent } %${ loggedTimeLeft } ` )
78
82
lastLoggedPercent = progress . percent
79
83
}
80
84
} )
81
85
. on ( `error` , ( err , stdout , stderr ) => {
82
- console . log ( `\n---\n` , stdout , stderr , `\n---\n` )
83
- console . log ( `${ loggingPrefix } An error occurred:` )
86
+ reporter . info ( `\n---\n` , stdout , stderr , `\n---\n` )
87
+ reporter . info ( `${ name } - An error occurred:` )
84
88
console . error ( err )
85
89
reject ( err )
86
90
} )
87
91
. on ( `end` , ( ) => {
88
- console . log ( `${ loggingPrefix } 100%` )
92
+ reporter . verbose ( `${ name } - 100%` )
93
+ reporter . info ( `${ name } - converted` )
89
94
resolve ( )
90
95
} )
91
96
. save ( cachePath )
@@ -129,11 +134,8 @@ export default class FFMPEG {
129
134
}
130
135
131
136
// Queue video for conversion
132
- queueConvertVideo = async ( ...args ) => {
133
- const videoData = await this . queue . add ( ( ) => this . convertVideo ( ...args ) )
134
-
135
- return videoData
136
- }
137
+ queueConvertVideo = async ( ...args ) =>
138
+ this . queue . add ( ( ) => this . convertVideo ( ...args ) )
137
139
138
140
// Converts a video based on a given profile, populates cache and public dir
139
141
convertVideo = async ( {
@@ -147,15 +149,14 @@ export default class FFMPEG {
147
149
const alreadyExists = await pathExists ( cachePath )
148
150
149
151
if ( ! alreadyExists ) {
150
- const loggingPrefix = `[FFMPEG]`
151
152
const ffmpegSession = ffmpeg ( ) . input ( sourcePath )
152
153
const filters = this . createFilters ( { fieldArgs, info } ) . join ( `,` )
153
154
const videoStreamMetadata = this . parseVideoStream ( info . streams )
154
155
155
156
profile ( { ffmpegSession, filters, fieldArgs, videoStreamMetadata } )
156
157
157
158
this . enhanceFfmpegForFilters ( { ffmpegSession, fieldArgs } )
158
- await this . executeFfmpeg ( { ffmpegSession, cachePath, loggingPrefix } )
159
+ await this . executeFfmpeg ( { ffmpegSession, cachePath } )
159
160
}
160
161
161
162
// If public file does not exist, copy cached file
@@ -176,6 +177,10 @@ export default class FFMPEG {
176
177
return { publicPath }
177
178
}
178
179
180
+ // Queue take screenshots
181
+ queueTakeScreenshots = ( ...args ) =>
182
+ this . queue . add ( ( ) => this . takeScreenshots ( ...args ) )
183
+
179
184
takeScreenshots = async (
180
185
video ,
181
186
fieldArgs ,
@@ -224,10 +229,12 @@ export default class FFMPEG {
224
229
ffmpeg ( path )
225
230
. on ( `filenames` , function ( filenames ) {
226
231
screenshotRawNames = filenames
227
- console . log ( `[FFMPEG] Taking ${ filenames . length } screenshots` )
232
+ reporter . info (
233
+ `${ name } - Taking ${ filenames . length } ${ width } px screenshots`
234
+ )
228
235
} )
229
236
. on ( `error` , ( err , stdout , stderr ) => {
230
- console . log ( `[FFMPEG] Failed to take screenshots:`)
237
+ reporter . info ( ` ${ name } - Failed to take ${ width } px screenshots:`)
231
238
console . error ( err )
232
239
reject ( err )
233
240
} )
@@ -244,8 +251,6 @@ export default class FFMPEG {
244
251
245
252
const screenshotNodes = [ ]
246
253
247
- console . log ( { screenshotRawNames, tmpDir } )
248
-
249
254
for ( const screenshotRawName of screenshotRawNames ) {
250
255
try {
251
256
const rawScreenshotPath = resolve ( tmpDir , screenshotRawName )
@@ -254,7 +259,7 @@ export default class FFMPEG {
254
259
try {
255
260
await access ( rawScreenshotPath )
256
261
} catch {
257
- console . warn ( `Screenshot ${ rawScreenshotPath } could not be found!` )
262
+ reporter . warn ( `Screenshot ${ rawScreenshotPath } could not be found!` )
258
263
continue
259
264
}
260
265
@@ -280,7 +285,7 @@ export default class FFMPEG {
280
285
281
286
screenshotNodes . push ( node )
282
287
} catch ( err ) {
283
- console . log ( `Failed to take screenshots:`)
288
+ reporter . info ( ` ${ name } - failed to take screenshots:`)
284
289
console . error ( err )
285
290
throw err
286
291
}
0 commit comments