Skip to content

Commit

Permalink
chore: fix: featch-ffmpeg script for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Feb 6, 2024
1 parent 3c19e09 commit f909ed9
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions scripts/fetch-ffmpeg.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const platformInfo = `${process.platform}-${process.arch}`
const platformVersions = targetVersions[platformInfo]

if (platformVersions) {
const tmpPath = path.join(ffmpegRootDir, 'tmp')

for (const version of platformVersions) {
const versionPath = path.join(ffmpegRootDir, version.id)
Expand All @@ -37,24 +36,40 @@ if (platformVersions) {
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 (version.url.endsWith('.tar.xz')) {
if (fileExtension === '.tar.xz') {
await fs.mkdir(versionPath).catch(() => null)
cp.execSync(`tar -xJf ${toPosix(tmpPath)} --strip-components=1 -C ${toPosix(versionPath)}`)
} else if (version.url.endsWith('.zip')) {
cp.execSync(`unzip ${toPosix(tmpPath)} -d ${toPosix(ffmpegRootDir)}`)
} 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)

const dirname = path.parse(version.url).name
await fs.rename(path.join(ffmpegRootDir, dirname), versionPath)
} else {
throw new Error(`Unhandled file extension: ${version.url}`)
}
await fs.rm(tmpPath)
}
}
} else {
Expand Down

0 comments on commit f909ed9

Please sign in to comment.