name: Build ffmpeg macos

on:
  push:
    paths:
      - '.github/workflows/ffmpeg-macos.yml'
      - '.github/scripts/ffmpeg-macos/**'
    branches:
      - main
  pull_request:
    paths:
      - '.github/workflows/ffmpeg-macos.yml'
      - '.github/scripts/ffmpeg-macos/**'
  workflow_dispatch:

# Cancel previous runs of the same workflow on the same branch.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  FFMPEG_VERSION: '6.0'

jobs:
  build-ffmpeg-macos:
    name: Build ffmpeg for macos
    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          install: true
          platforms: linux/amd64
          driver-opts: |
            image=moby/buildkit:master
            network=host

      - name: Build ffmpeg
        run: |
          set -euxo pipefail
          cd .github/scripts/ffmpeg-macos
          docker build --build-arg FFMPEG_VERSION=$FFMPEG_VERSION -o . .
          export XZ_OPT='-T0 -9'
          tar -cJf "ffmpeg-${FFMPEG_VERSION}-x86_64.tar.xz" -C ffmpeg/x86_64 .
          tar -cJf "ffmpeg-${FFMPEG_VERSION}-arm64.tar.xz" -C ffmpeg/aarch64 .
          rm -rf ffmpeg

      - name: Publish ffmpeg x86_64
        uses: actions/upload-artifact@v3
        with:
          name: ffmpeg-${{ env.FFMPEG_VERSION }}-x86_64
          path: .github/scripts/ffmpeg-macos/ffmpeg-${{ env.FFMPEG_VERSION }}-x86_64.tar.xz
          if-no-files-found: error

      - name: Publish ffmpeg arm64
        uses: actions/upload-artifact@v3
        with:
          name: ffmpeg-${{ env.FFMPEG_VERSION }}-arm64
          path: .github/scripts/ffmpeg-macos/ffmpeg-${{ env.FFMPEG_VERSION }}-arm64.tar.xz
          if-no-files-found: error
