Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Quote file paths in calls to ffmpeg #132

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ tmp_packages_for_build/
ffmpeg.exe
ffprobe.exe
apps/single-app/app/expectedPackages.json_smartbull
.ffmpeg/
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
"lint": "lerna exec --parallel --no-bail -- eslint . --ext .ts,.tsx",
"lintfix": "yarn lint --fix",
"lint:changed": "lerna exec --since origin/master --include-dependents -- eslint . --ext .js,.jsx,.ts,.tsx",
"test": "lerna run test --stream",
"test:ci": "lerna run test --stream",
"test:changed": "lerna run --since origin/master --include-dependents test",
"test:update": "lerna run test -- -u",
"test:update:changed": "lerna run --since origin/master --include-dependents test -- -u",
"test": "yarn test:prepare && lerna run test --stream",
"test:ci": "yarn test:prepare && lerna run test --stream",
"test:changed": "yarn test:prepare && lerna run --since origin/master --include-dependents test",
"test:update": "yarn test:prepare && lerna run test -- -u",
"test:update:changed": "yarn test:prepare && lerna run --since origin/master --include-dependents test -- -u",
"test:prepare": "node scripts/prepare-for-tests.mjs",
"typecheck": "lerna exec -- tsc --noEmit",
"typecheck:changed": "lerna exec --since origin/master --include-dependents -- tsc --noEmit",
"build-win32": "node scripts/prepare-for-build32.js && lerna run build-win32 --stream && node scripts/cleanup-after-build32.mjs",
Expand All @@ -47,14 +48,16 @@
"devDependencies": {
"@sofie-automation/code-standard-preset": "^2.5.1",
"@types/jest": "^29.2.5",
"@types/rimraf": "^3.0.0",
"deep-extend": "^0.6.0",
"find": "^0.3.0",
"fs-extra": "^11.1.0",
"glob": "^8.1.0",
"jest": "^29.3.1",
"lerna": "^6.6.1",
"json-schema-to-typescript": "^10.1.5",
"lerna": "^6.6.1",
"mkdirp": "^2.1.3",
"node-fetch": "^2.6.9",
"pkg": "^5.8.0",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
Expand Down
83 changes: 83 additions & 0 deletions scripts/prepare-for-tests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* This is a small helper to download and extract a set of ffmpeg binaries, so that we can
* test package-manager with multiple versions of ffmpeg.
* It reads the file `./tests/ffmpegReleases.json` to see what versions should be downloaded,
* and puts them into `.ffmpeg/` at the root of the repository.
*/

import fs from 'fs/promises'
import { pipeline } from 'node:stream'
import { promisify } from 'node:util'
import { createWriteStream } from 'node:fs'
import path from 'path'
import cp from 'child_process'
import fetch from 'node-fetch'

const targetVersions = JSON.parse(await fs.readFile('./tests/ffmpegReleases.json'))

const toPosix = (str) => str.split(path.sep).join(path.posix.sep)

const streamPipeline = promisify(pipeline)

const ffmpegRootDir = './.ffmpeg'
await fs.mkdir(ffmpegRootDir).catch(() => null)

async function pathExists(path) {
try {
await fs.stat(path)
return true
} catch (e) {
return false
}
}

const platformInfo = `${process.platform}-${process.arch}`
const platformVersions = targetVersions[platformInfo]

if (platformVersions) {
for (const version of platformVersions) {
const versionPath = path.join(ffmpegRootDir, version.id)
const dirStat = await pathExists(versionPath)
if (!dirStat) {
console.log(`Fetching ${version.url}`)
// Download it

const fileExtension = version.url.endsWith('.tar.xz') ? '.tar.xz' : version.url.endsWith('.zip') ? '.zip' : ''
const tmpPath = path.resolve(path.join(ffmpegRootDir, 'tmp' + fileExtension))

// eslint-disable-next-line no-undef
const response = await fetch(version.url)
if (!response.ok) throw new Error(`unexpected response ${response.statusText}`)
await streamPipeline(response.body, createWriteStream(tmpPath))

// Extract it
if (fileExtension === '.tar.xz') {
await fs.mkdir(versionPath).catch(() => null)
cp.execSync(`tar -xJf ${toPosix(tmpPath)} --strip-components=1 -C ${toPosix(versionPath)}`)
} else if (fileExtension === '.zip') {
if (process.platform === 'win32') {
cp.execSync(`tar -xf ${toPosix(tmpPath)}`, {
cwd: ffmpegRootDir,
})

const list = cp.execSync(`tar -tf ${toPosix(tmpPath)}`).toString()
const mainFolder = list
.split('\n')[0]
.trim() // "ffmpeg-4.3.1-win64-static/"
.replace(/[\/\\]*$/, '') // remove trailing slash
await fs.rename(path.join(ffmpegRootDir, mainFolder), versionPath)
} else {
cp.execSync(`unzip ${toPosix(tmpPath)} -d ${toPosix(ffmpegRootDir)}`)
const dirname = path.parse(version.url).name
await fs.rename(path.join(ffmpegRootDir, dirname), versionPath)
}

await fs.rm(tmpPath)
} else {
throw new Error(`Unhandled file extension: ${version.url}`)
}
}
}
} else {
throw new Error(`No FFMpeg binaries have been defined for "${platformInfo}" yet`)
}
8 changes: 8 additions & 0 deletions shared/packages/api/src/__tests__/filePath.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { escapeFilePath } from '../filePath'

describe('filePath', () => {
test('checkPath', () => {
expect(escapeFilePath('test/path')).toBe(process.platform === 'win32' ? '"test/path"' : 'test/path')
expect(escapeFilePath('C:\\test\\path')).toBe(process.platform === 'win32' ? '"C:\\test\\path"' : 'C:\\test\\path')
})
})
10 changes: 10 additions & 0 deletions shared/packages/api/src/filePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Escape spaces in file path with double quotes on windows.
*
* @param {string} path File path to be escaped.
* @returns {string} Escaped file path.
* @see {@link https://ffmpeg.org/ffmpeg-utils.html#Quoting-and-escaping}
*/
export function escapeFilePath(path: string): string {
return process.platform === 'win32' ? `"${path}"` : path
}
1 change: 1 addition & 0 deletions shared/packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './adapterClient'
export * from './adapterServer'
export * from './appContainer'
export * from './config'
export * from './filePath'
export * from './expectationApi'
export * from './inputApi'
export * from './HelpfulEventEmitter'
Expand Down
6 changes: 4 additions & 2 deletions shared/packages/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "yarn rimraf dist && yarn build:main",
"build:main": "tsc -p tsconfig.json",
"__test": "jest"
"test": "jest"
},
"engines": {
"node": ">=14.18.0"
Expand All @@ -18,7 +18,8 @@
"devDependencies": {
"@types/deep-diff": "^1.0.0",
"@types/node-fetch": "^2.5.8",
"@types/tmp": "~0.2.2"
"@types/tmp": "~0.2.2",
"jest-mock-extended": "^3.0.5"
},
"dependencies": {
"@parcel/watcher": "^2.3.0",
Expand All @@ -31,6 +32,7 @@
"node-fetch": "^2.6.1",
"tmp": "~0.2.1",
"tv-automation-quantel-gateway-client": "3.1.7",
"type-fest": "3.13.1",
"windows-network-drive": "^4.0.1",
"xml-js": "^1.6.11"
},
Expand Down
51 changes: 51 additions & 0 deletions shared/packages/worker/src/__tests__/ffmpegHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import path from 'path'
import { LocalFolderAccessorHandle } from '../worker/accessorHandlers/localFolder'
import { overrideFFMpegExecutables, spawnFFMpeg } from '../worker/workers/windowsWorker/expectationHandlers/lib/ffmpeg'

export const SamplesDir = path.join(__dirname, '../../../../../tests/samples')

export async function callSpawnFFmpeg(args: string[], targetHandle: LocalFolderAccessorHandle<any>): Promise<void> {
let resolve = () => {}
let reject = (_err: Error) => {}
const result = new Promise<void>((resolve2, reject2) => {
resolve = resolve2
reject = reject2
})

const ffmpegProcess = await spawnFFMpeg(
args,
targetHandle,
async () => resolve(),
async (err) => reject(err)
)
expect(ffmpegProcess).toBeTruthy()

// Wait for process to complete
await result
}

export function runForEachFFMpegRelease(runForFFmpegRelease: () => void) {
const ffprobeFilename = process.platform === 'win32' ? 'bin/ffprobe.exe' : 'ffprobe'
const ffmpegFilename = process.platform === 'win32' ? 'bin/ffmpeg.exe' : 'ffmpeg'

const ffmpegRootPath = path.join(__dirname, '../../../../../.ffmpeg')

// eslint-disable-next-line @typescript-eslint/no-var-requires
const targetVersions = require('../../../../../tests/ffmpegReleases.json')

for (const version of targetVersions[`${process.platform}-${process.arch}`]) {
describe(`FFmpeg ${version.id}`, () => {
beforeEach(() => {
overrideFFMpegExecutables({
ffmpeg: path.join(ffmpegRootPath, version.id, ffmpegFilename),
ffprobe: path.join(ffmpegRootPath, version.id, ffprobeFilename),
})
})
afterAll(() => {
overrideFFMpegExecutables(null)
})

runForFFmpegRelease()
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import path from 'path'
import { copyFile, mkdtemp, readdir } from 'fs/promises'
import { runForEachFFMpegRelease, SamplesDir } from '../../../__tests__/ffmpegHelper'
import rimraf from 'rimraf'
import { convertAudio, countFrames, createTGASequence, getStreamIndicies } from '../atem'
import { tmpdir } from 'os'

async function copyToTmpDir(inputFile: string): Promise<{ tmpDir: string; copiedFile: string }> {
const tmpDir = await mkdtemp(path.join(tmpdir(), 'package-manager-atem-'))
const copiedFile = path.join(tmpDir, 'input_file')
await copyFile(inputFile, copiedFile)

return { tmpDir, copiedFile }
}

runForEachFFMpegRelease(() => {
describe('name with spaces.mov', () => {
const clipPath = path.join(SamplesDir, 'name with spaces.mov')

let tmpDir: string
let copiedFile: string

beforeEach(async () => {
const res = await copyToTmpDir(clipPath)
tmpDir = res.tmpDir
copiedFile = res.copiedFile

const dirListBefore = await readdir(tmpDir)
expect(dirListBefore).toHaveLength(1)
})

afterEach(async () => {
rimraf.sync(tmpDir)
})

it('createTGASequence', async () => {
const result = await createTGASequence(copiedFile)
expect(result).toBe('')

const dirListAfter = await readdir(tmpDir)
expect(dirListAfter).toHaveLength(51)
})

// TODO: convertFrameToRGBA

it('convertAudio', async () => {
const result = await convertAudio(copiedFile)
expect(result).toBe('')

const dirListAfter = await readdir(tmpDir)
expect(dirListAfter).toHaveLength(2)
})

it('countFrames', async () => {
const result = await countFrames(copiedFile)
expect(result).toBe(50)
})

it('getStreamIndicies', async () => {
const videoResult = await getStreamIndicies(copiedFile, 'video')
expect(videoResult).toEqual([0])

const audioResult = await getStreamIndicies(copiedFile, 'audio')
expect(audioResult).toEqual([1])
})
})
})
Loading
Loading