Skip to content

Commit

Permalink
refactor: consolidate ffmpeg-related files in third_party/ffmpeg (#3815)
Browse files Browse the repository at this point in the history
This patch:
- moves ffmpeg binaries from `//bin/` to `//third_party/ffmpeg`
- adds [COPYING.GPLv3](https://github.com/FFmpeg/FFmpeg/blob/master/COPYING.GPLv3)
ffmpeg license
- changes npm packaging to include `//third_party/ffmpeg` only in `playwright` and `playwrihgt-chromium` a
  • Loading branch information
aslushnikov authored Sep 9, 2020
1 parent 6c83266 commit 8687400
Show file tree
Hide file tree
Showing 10 changed files with 904 additions and 224 deletions.
221 changes: 1 addition & 220 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -1,221 +1,2 @@
# Binary Files

<!-- GEN:toc-top-level -->
- [Building `PrintDeps.exe`](#building-printdepsexe)
- [Building `ffmpeg-mac`](#building-ffmpeg-mac)
- [Building `ffmpeg-win64.exe`](#building-ffmpeg-win64exe)
- [Building `ffmpeg-win32.exe`](#building-ffmpeg-win32exe)
<!-- GEN:stop -->

## Building `PrintDeps.exe`

See instructions at [`//browser_patches/tools/PrintDepsWindows/README.md`](../browser_patches/tools/PrintDepsWindows/README.md)

## Building `ffmpeg-mac`

> FFMPEG: [`n4.3.1`](https://github.com/FFmpeg/FFmpeg/releases/tag/n4.3.1)
> libvpx: [`v1.9.0`](https://github.com/webmproject/libvpx/releases/tag/v1.9.0)
I mostly followed steps at https://trac.ffmpeg.org/wiki/CompilationGuide/macOS

1. Clone repo & checkout release tag (we're building release v4.3.1)

```sh
~$ git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
~$ cd ffmpeg
~/ffmpeg$ git checkout n4.3.1
```

2. Install brew dependencies

```sh
~/ffmpeg$ brew install automake fdk-aac git lame libass libtool libvorbis libvpx \
opus sdl shtool texi2html theora wget x264 x265 xvid nasm
```

3. Prepare output folders

```sh
~/ffmpeg$ mkdir -p output/bin
```

4. Configure with vpx

```sh
~/ffmpeg$ ./configure --prefix=$PWD/output \
--pkg-config-flags="--static" \
--bindir=$PWD/output/bin \
--disable-everything \
--enable-ffmpeg \
--enable-protocol=pipe \
--enable-protocol=file \
--enable-parser=mjpeg \
--enable-decoder=mjpeg \
--enable-demuxer=image2pipe \
--enable-filter=pad \
--enable-filter=crop \
--enable-filter=scale \
--enable-muxer=webm \
--enable-encoder=libvpx_vp8 \
--enable-libvpx \
--enable-static
```

5. Make & install to the `output/bin`

```sh
~/ffmpeg$ make && make install
```

## Building `ffmpeg-win64.exe`

> FFMPEG: [`n4.3.1`](https://github.com/FFmpeg/FFmpeg/releases/tag/n4.3.1)
> libvpx: [`d1a7897`](https://github.com/webmproject/libvpx/commit/d1a78971ebcfd728c9c73b0cfbee69f470d4dc72)
1. Install `MSYS2` from `https://www.msys2.org` and install to `c:\msys64`

2. Launch `c:\msys64\mingw64` to launch a shell with a proper environment.

3. `MSYS2` uses `pacman` to install dependencies. Run the following commands to update & install packages:

```sh
$ pacman -Syu
$ pacman -Su
$ pacman -S make pkgconf diffutils yasm
$ pacman -S mingw-w64-x86_64-nasm mingw-w64-x86_64-gcc mingw-w64-x86_64-SDL2
```

notes:
- `yasm` is needed for `libvpx`
- the rest is a general compilation toolchain

4. Clone `libvpx` of proper version, compile manually and install to a well-known location (prefix):

```sh
$ cd /c/
$ git clone https://chromium.googlesource.com/webm/libvpx
$ cd libvpx
$ git checkout d1a78971ebcfd728c9c73b0cfbee69f470d4dc72
$ ./configure --prefix=/eee64 --target=x86_64-win64-gcc --enable-static --disable-shared --disable-docs --disable-tools --disable-unit-tests --disable-examples
$ make && make install
```

Note: `libvpx` has a useful readme: https://chromium.googlesource.com/webm/libvpx/+/master/README

5. Once `libvpx` is compiled, compile `ffmpeg` using the just-compiled `libvpx`:

```sh
$ cd /c/
$ git clone git://source.ffmpeg.org/ffmpeg.git
$ cd ffmpeg
$ git checkout n4.3.1
$ ./configure --extra-cflags="-I/eee64/include" \
--extra-ldflags="-L/eee64/lib -static" \
--prefix=/eee64 \
--pkg-config-flags="--static" \
--bindir=$PWD/output/bin \
--disable-everything \
--enable-ffmpeg \
--enable-protocol=pipe \
--enable-protocol=file \
--enable-parser=mjpeg \
--enable-decoder=mjpeg \
--enable-demuxer=image2pipe \
--enable-filter=pad \
--enable-filter=crop \
--enable-filter=scale \
--enable-muxer=webm \
--enable-libvpx \
--enable-static \
--enable-encoder=libvpx_vp8 \
--disable-pthreads \
--disable-zlib \
--disable-iconv \
--disable-bzlib \
--disable-w32threads
$ make && make install
```

note: the following resources helped me to deal with some dynamic dependencies in the resulting `ffmpeg.exe`:
- https://stackoverflow.com/questions/13768515/how-to-do-static-linking-of-libwinpthread-1-dll-in-mingw

## Building `ffmpeg-win32.exe`

> FFMPEG: [`n4.3.1`](https://github.com/FFmpeg/FFmpeg/releases/tag/n4.3.1)
> libvpx: [`d1a7897`](https://github.com/webmproject/libvpx/commit/d1a78971ebcfd728c9c73b0cfbee69f470d4dc72)
> NOTE: these steps assume that `ffmpeg-win64.exe` was just built on the machine.
1. Launch `c:\msys64\mingw32` to launch a shell with a proper environment. Not sure if this is required or everything could be done from `mingw64` - but it worked.

2. Update libraries for mingw32

```sh
$ pacman -Syu
$ pacman -Su
```

3. Uninstall the `x86_64` compilers that we installed to build win64 version:

```sh
$ pacman -R mingw-w64-x86_64-nasm mingw-w64-x86_64-gcc mingw-w64-x86_64-SDL2
```

4. Install the i686 compilers instead of their x86_64 counterparts:

```sh
$ pacman -S mingw-w64-i686-nasm mingw-w64-i686-gcc mingw-w64-i686-SDL2
```

5. Remove all old source folders - we'll re-clone everything later for simplicity

```sh
$ rm -rf /c/ffmpeg && rm -rf /c/libvpx
```

6. Clone & compile libvpx. Notice a change: `--target=x86-win32-gcc`

```sh
$ cd /c/
$ git clone https://chromium.googlesource.com/webm/libvpx
$ cd libvpx
$ ./configure --prefix=/eee32 --target=x86-win32-gcc --enable-static --disable-shared --disable-docs --disable-tools --disable-unit-tests --disable-examples
$ make && make install
```

7. Clone & compile ffmpeg

```sh
$ cd /c/
$ git clone git://source.ffmpeg.org/ffmpeg.git
$ cd ffmpeg
$ git checkout n4.3.1
$ ./configure --extra-cflags="-I/eee32/include" \
--extra-ldflags="-L/eee32/lib -static" \
--prefix=/eee32 \
--pkg-config-flags="--static" \
--bindir=$PWD/output/bin \
--disable-everything \
--enable-ffmpeg \
--enable-protocol=pipe \
--enable-protocol=file \
--enable-parser=mjpeg \
--enable-decoder=mjpeg \
--enable-demuxer=image2pipe \
--enable-filter=pad \
--enable-filter=crop \
--enable-filter=scale \
--enable-muxer=webm \
--enable-libvpx \
--enable-static \
--enable-encoder=libvpx_vp8 \
--disable-pthreads \
--disable-zlib \
--disable-iconv \
--disable-bzlib \
--disable-w32threads
$ make && make install
```

note: using `-j` for make somehow breaks compilation - so I'd suggest compiling everything in one thread.
See building instructions at [`//browser_patches/tools/PrintDepsWindows/README.md`](../browser_patches/tools/PrintDepsWindows/README.md)

12 changes: 9 additions & 3 deletions packages/build_package.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ const SCRIPT_NAME = path.basename(__filename);
const ROOT_PATH = path.join(__dirname, '..');

const PLAYWRIGHT_CORE_FILES = ['bin', 'lib', 'types', 'NOTICE', 'LICENSE'];
const FFMPEG_FILES = ['third_party/ffmpeg'];

const PACKAGES = {
'playwright': {
description: 'A high-level API to automate web browsers',
browsers: ['chromium', 'firefox', 'webkit'],
// We copy README.md additionally for Playwright so that it looks nice on NPM.
files: [...PLAYWRIGHT_CORE_FILES, 'README.md'],
files: [...PLAYWRIGHT_CORE_FILES, ...FFMPEG_FILES, 'README.md'],
},
'playwright-core': {
description: 'A high-level API to automate web browsers',
Expand All @@ -55,13 +56,13 @@ const PACKAGES = {
'playwright-chromium': {
description: 'A high-level API to automate Chromium',
browsers: ['chromium'],
files: PLAYWRIGHT_CORE_FILES,
files: [...PLAYWRIGHT_CORE_FILES, ...FFMPEG_FILES],
},
'playwright-electron': {
version: '0.4.0', // Manually manage playwright-electron version.
description: 'A high-level API to automate Electron',
browsers: [],
files: [...PLAYWRIGHT_CORE_FILES, 'electron-types.d.ts'],
files: [...PLAYWRIGHT_CORE_FILES, ...FFMPEG_FILES, 'electron-types.d.ts'],
},
};

Expand Down Expand Up @@ -184,6 +185,11 @@ async function writeToPackage(fileName, content) {

async function copyToPackage(fromPath, toPath) {
console.error(`- copying: //${path.relative(ROOT_PATH, fromPath)} -> //${path.relative(ROOT_PATH, toPath)}`);
try {
fs.mkdirSync(path.dirname(toPath), { recursive: true });
} catch (e) {
// the folder might exist already
}
await cpAsync(fromPath, toPath);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/common/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
lib/server/injected/
# Include all binaries that we ship with the package.
!bin/*
# Include FFMPEG
!third_party/ffmpeg/*
# Include generated types and entrypoint.
!types/*
!index.d.ts
Expand Down
2 changes: 1 addition & 1 deletion src/server/chromium/videoRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class VideoRecorder {
const progress = this._progress;

let ffmpegPath = 'ffmpeg';
const binPath = path.join(__dirname, '../../../bin/');
const binPath = path.join(__dirname, '../../../third_party/ffmpeg/');
if (os.platform() === 'win32')
ffmpegPath = path.join(binPath, os.arch() === 'x64' ? 'ffmpeg-win64.exe' : 'ffmpeg-win32.exe');
else if (os.platform() === 'darwin')
Expand Down
Loading

0 comments on commit 8687400

Please sign in to comment.