diff --git a/packages/gatsby-transformer-video/src/binaries.js b/packages/gatsby-transformer-video/src/binaries.js index 6bf804606ed5d..009977713367d 100644 --- a/packages/gatsby-transformer-video/src/binaries.js +++ b/packages/gatsby-transformer-video/src/binaries.js @@ -2,6 +2,7 @@ import { resolve } from "path" import execa from "execa" import { access, ensureDir } from "fs-extra" +import reporter from "gatsby-cli/lib/reporter" export async function libsInstalled({ platform }) { try { @@ -31,7 +32,7 @@ export async function downloadLibs({ binariesDir, platform }) { switch (platform) { case `win32`: - console.log( + reporter.info( `Downloading FFMPEG && FFPROBE (Note: This script is not yet tested on windows)` ) await execa( @@ -45,33 +46,34 @@ export async function downloadLibs({ binariesDir, platform }) { execaConfig ) - console.log(`Unzipping FFMPEG && FFPROBE`) + reporter.info(`Unzipping FFMPEG && FFPROBE`) await execa(`tar`, [`-xf`, `ffmpeg.zip`], execaConfig) - console.log(`Cleanup`) + reporter.info(`Cleanup`) await execa(`mv`, [`bin/*`, `.`], execaConfig) await execa(`rm`, [`-rf`, `ffmpeg-latest-win64-static`], execaConfig) break case `linux`: - console.log(`Downloading FFMPEG && FFPROBE`) + reporter.info(`Downloading FFMPEG && FFPROBE`) await execa( `wget`, [ `-O`, + `-nv`, `ffmpeg.zip`, `https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz`, ], execaConfig ) - console.log(`Unzipping FFMPEG && FFPROBE`) + reporter.info(`Unzipping FFMPEG && FFPROBE`) await execa(`tar`, [`-xf`, `ffmpeg.zip`, `--strip`, `1`], execaConfig) - console.log(`Cleanup`) + reporter.info(`Cleanup`) await execa(`rm`, [`ffmpeg.zip`], execaConfig) break case `darwin`: - console.log(`Downloading FFMPEG`) + reporter.info(`Downloading FFMPEG`) await execa( `curl`, @@ -85,7 +87,7 @@ export async function downloadLibs({ binariesDir, platform }) { execaConfig ) - console.log(`Downloading FFPROBE`) + reporter.info(`Downloading FFPROBE`) await execa( `curl`, [ @@ -97,11 +99,11 @@ export async function downloadLibs({ binariesDir, platform }) { execaConfig ) - console.log(`Unzipping...`) + reporter.info(`Unzipping...`) await execa(`unzip`, [`-o`, `ffmpeg.zip`], execaConfig) await execa(`unzip`, [`-o`, `ffprobe.zip`], execaConfig) - console.log(`Cleanup...`) + reporter.info(`Cleanup...`) await execa(`rm`, [`ffmpeg.zip`, `ffprobe.zip`], execaConfig) break default: diff --git a/packages/gatsby-transformer-video/src/ffmpeg.js b/packages/gatsby-transformer-video/src/ffmpeg.js index 9b00c30dd1f57..fab4c36c9e850 100644 --- a/packages/gatsby-transformer-video/src/ffmpeg.js +++ b/packages/gatsby-transformer-video/src/ffmpeg.js @@ -11,6 +11,7 @@ import imageminMozjpeg from "imagemin-mozjpeg" import PQueue from "p-queue" import sharp from "sharp" import { createFileNodeFromBuffer } from "gatsby-source-filesystem" +import reporter from "gatsby-cli/lib/reporter" import { cacheContentfulVideo } from "./helpers" @@ -53,14 +54,17 @@ export default class FFMPEG { }) // Execute FFMMPEG and log progress - executeFfmpeg = async ({ ffmpegSession, cachePath, loggingPrefix }) => { + executeFfmpeg = async ({ ffmpegSession, cachePath }) => { let startTime let lastLoggedPercent = 0.1 + const { name } = parse(cachePath) + return new Promise((resolve, reject) => { ffmpegSession .on(`start`, commandLine => { - console.log(`${loggingPrefix} Executing:\n\n${commandLine}\n`) + reporter.info(`${name} - converting`) + reporter.verbose(`${name} - executing:\n\n${commandLine}\n`) startTime = performance.now() }) .on(`progress`, progress => { @@ -74,18 +78,19 @@ export default class FFMPEG { const loggedTimeLeft = estTimeLeft !== Infinity && ` (~${estTimeLeft}s)` - console.log(`${loggingPrefix} ${percent}%${loggedTimeLeft}`) + reporter.info(`${name} - ${percent}%${loggedTimeLeft}`) lastLoggedPercent = progress.percent } }) .on(`error`, (err, stdout, stderr) => { - console.log(`\n---\n`, stdout, stderr, `\n---\n`) - console.log(`${loggingPrefix} An error occurred:`) + reporter.info(`\n---\n`, stdout, stderr, `\n---\n`) + reporter.info(`${name} - An error occurred:`) console.error(err) reject(err) }) .on(`end`, () => { - console.log(`${loggingPrefix} 100%`) + reporter.verbose(`${name} - 100%`) + reporter.info(`${name} - converted`) resolve() }) .save(cachePath) @@ -129,11 +134,8 @@ export default class FFMPEG { } // Queue video for conversion - queueConvertVideo = async (...args) => { - const videoData = await this.queue.add(() => this.convertVideo(...args)) - - return videoData - } + queueConvertVideo = async (...args) => + this.queue.add(() => this.convertVideo(...args)) // Converts a video based on a given profile, populates cache and public dir convertVideo = async ({ @@ -147,7 +149,6 @@ export default class FFMPEG { const alreadyExists = await pathExists(cachePath) if (!alreadyExists) { - const loggingPrefix = `[FFMPEG]` const ffmpegSession = ffmpeg().input(sourcePath) const filters = this.createFilters({ fieldArgs, info }).join(`,`) const videoStreamMetadata = this.parseVideoStream(info.streams) @@ -155,7 +156,7 @@ export default class FFMPEG { profile({ ffmpegSession, filters, fieldArgs, videoStreamMetadata }) this.enhanceFfmpegForFilters({ ffmpegSession, fieldArgs }) - await this.executeFfmpeg({ ffmpegSession, cachePath, loggingPrefix }) + await this.executeFfmpeg({ ffmpegSession, cachePath }) } // If public file does not exist, copy cached file @@ -176,6 +177,10 @@ export default class FFMPEG { return { publicPath } } + // Queue take screenshots + queueTakeScreenshots = (...args) => + this.queue.add(() => this.takeScreenshots(...args)) + takeScreenshots = async ( video, fieldArgs, @@ -224,10 +229,12 @@ export default class FFMPEG { ffmpeg(path) .on(`filenames`, function (filenames) { screenshotRawNames = filenames - console.log(`[FFMPEG] Taking ${filenames.length} screenshots`) + reporter.info( + `${name} - Taking ${filenames.length} ${width}px screenshots` + ) }) .on(`error`, (err, stdout, stderr) => { - console.log(`[FFMPEG] Failed to take screenshots:`) + reporter.info(`${name} - Failed to take ${width}px screenshots:`) console.error(err) reject(err) }) @@ -244,8 +251,6 @@ export default class FFMPEG { const screenshotNodes = [] - console.log({ screenshotRawNames, tmpDir }) - for (const screenshotRawName of screenshotRawNames) { try { const rawScreenshotPath = resolve(tmpDir, screenshotRawName) @@ -254,7 +259,7 @@ export default class FFMPEG { try { await access(rawScreenshotPath) } catch { - console.warn(`Screenshot ${rawScreenshotPath} could not be found!`) + reporter.warn(`Screenshot ${rawScreenshotPath} could not be found!`) continue } @@ -280,7 +285,7 @@ export default class FFMPEG { screenshotNodes.push(node) } catch (err) { - console.log(`Failed to take screenshots:`) + reporter.info(`${name} - failed to take screenshots:`) console.error(err) throw err } diff --git a/packages/gatsby-transformer-video/src/gatsby-node.js b/packages/gatsby-transformer-video/src/gatsby-node.js index 354834a44a926..0d5769b104c44 100644 --- a/packages/gatsby-transformer-video/src/gatsby-node.js +++ b/packages/gatsby-transformer-video/src/gatsby-node.js @@ -3,6 +3,7 @@ import os from "os" import { ensureDir } from "fs-extra" import { GraphQLString, GraphQLInt, GraphQLFloat } from "gatsby/graphql" +import reporter from "gatsby-cli/lib/reporter" import FFMPEG from "./ffmpeg" @@ -303,14 +304,14 @@ exports.createResolvers = async ( exports.onPreInit = async ({ store }, { downloadBinaries = true }) => { if (!downloadBinaries) { - console.log(`Skipped download of FFMPEG & FFPROBE binaries`) + reporter.verbose(`Skipped download of FFMPEG & FFPROBE binaries`) return } const alreadyInstalled = await libsInstalled({ platform }) if (alreadyInstalled) { - console.log(`FFMPEG && FFPROBE are already available on this machine`) + reporter.verbose(`FFMPEG && FFPROBE are already available on this machine`) return } @@ -322,14 +323,14 @@ exports.onPreInit = async ({ store }, { downloadBinaries = true }) => { try { await libsAlreadyDownloaded({ binariesDir }) - console.log(`FFMPEG & FFPROBE binaries already downloaded`) + reporter.verbose(`FFMPEG & FFPROBE binaries already downloaded`) } catch { try { - console.log(`FFMPEG & FFPROBE getting binaries for ${platform}@${arch}`) + reporter.info(`FFMPEG & FFPROBE getting binaries for ${platform}@${arch}`) await downloadLibs({ binariesDir, platform }) - console.log( + reporter.info( `Finished. This system is ready to convert videos with GatsbyJS` ) } catch (err) { diff --git a/packages/gatsby-transformer-video/src/helpers.js b/packages/gatsby-transformer-video/src/helpers.js index 5a25493f7fb79..3f067c0593c9c 100644 --- a/packages/gatsby-transformer-video/src/helpers.js +++ b/packages/gatsby-transformer-video/src/helpers.js @@ -4,6 +4,7 @@ import { resolve } from "path" import PQueue from "p-queue" import axios from "axios" +import reporter from "gatsby-cli/lib/reporter" const downloadQueue = new PQueue({ concurrency: 3 }) @@ -26,14 +27,13 @@ export async function cacheContentfulVideo({ video, cacheDir }) { try { await access(path, fs.constants.R_OK) - - console.log(`Already downloaded ${url}`) + reporter.verbose(`Already downloaded ${url}`) downloadCache[url] = path return downloadCache[url] } catch { if (url in downloadCache) { - console.log(`Already downloading ${url}`) + // Already downloading return downloadCache[url] } @@ -44,7 +44,7 @@ export async function cacheContentfulVideo({ video, cacheDir }) { while (!downloaded) { try { await downloadQueue.add(async () => { - console.log(`Downloading https:${url}`) + reporter.info(`Downloading ${url}`) const response = await axios({ method: `get`, @@ -52,8 +52,6 @@ export async function cacheContentfulVideo({ video, cacheDir }) { responseType: `stream`, }) - console.log(`Writing ${fileName} to disk`) - await new Promise((resolve, reject) => { const file = fs.createWriteStream(path) @@ -69,11 +67,11 @@ export async function cacheContentfulVideo({ video, cacheDir }) { if (tries === 3) { throw new Error( - `Download of https:${url} failed after three times:\n\n${e}` + `Download of ${url} failed after three times:\n\n${e}` ) } - console.log( - `Unable to download https:${url}\n\nRetrying again after 1s (${tries}/3)` + reporter.info( + `Unable to download ${url}\n\nRetrying again after 1s (${tries}/3)` ) console.error(e) console.log(Object.keys(e), e.message) @@ -81,6 +79,8 @@ export async function cacheContentfulVideo({ video, cacheDir }) { } } + reporter.info(`Downloaded: ${url}`) + return path }