Skip to content

Commit

Permalink
feat: use new reporter API
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Nov 2, 2020
1 parent 242e99e commit dd80e1a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
22 changes: 12 additions & 10 deletions packages/gatsby-transformer-video/src/binaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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`,
Expand All @@ -85,7 +87,7 @@ export async function downloadLibs({ binariesDir, platform }) {
execaConfig
)

console.log(`Downloading FFPROBE`)
reporter.info(`Downloading FFPROBE`)
await execa(
`curl`,
[
Expand All @@ -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:
Expand Down
43 changes: 24 additions & 19 deletions packages/gatsby-transformer-video/src/ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 => {
Expand All @@ -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)
Expand Down Expand Up @@ -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 ({
Expand All @@ -147,15 +149,14 @@ 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)

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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
})
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions packages/gatsby-transformer-video/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions packages/gatsby-transformer-video/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand All @@ -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]
}

Expand All @@ -44,16 +44,14 @@ 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`,
url: `https:${url}`,
responseType: `stream`,
})

console.log(`Writing ${fileName} to disk`)

await new Promise((resolve, reject) => {
const file = fs.createWriteStream(path)

Expand All @@ -69,18 +67,20 @@ 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)
await new Promise(resolve => setTimeout(resolve, 1000))
}
}

reporter.info(`Downloaded: ${url}`)

return path
}

Expand Down

0 comments on commit dd80e1a

Please sign in to comment.