From 7b78da9ccdd89ba9ce1a00d48bbc8d781d3c4819 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Wed, 23 Nov 2022 18:41:27 +0000 Subject: [PATCH 1/4] fix image resize --- .github/workflows/ci.yaml | 117 ++------------------------ go/internal/image_processor/worker.go | 41 ++++++--- 2 files changed, 34 insertions(+), 124 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ee1e1c4d..17f08fe5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -40,118 +40,11 @@ jobs: cancel-in-progress: true steps: - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: 1.18.3 - - - uses: actions/setup-node@v3 - with: - node-version: "18" - - - name: Install Yarn - run: npm install -g yarn - - name: Checkout code uses: actions/checkout@v3 with: submodules: recursive - - id: cache-paths - run: | - echo "::set-output name=go-build::$(go env GOCACHE)" - echo "::set-output name=go-mod::$(go env GOMODCACHE)" - echo "::set-output name=pwd::$(pwd)" - - - name: Results Cache - uses: actions/cache@v3 - with: - path: ${{ steps.cache-paths.outputs.pwd }}/cpp/out - key: ${{ runner.os }}-results-${{ hashFiles('cpp/third-party/Makefile') }} - - - name: External Build Cache - uses: actions/cache@v3 - with: - path: ${{ steps.cache-paths.outputs.pwd }}/cpp/third-party/build - key: ${{ runner.os }}-external-build-${{ hashFiles('cpp/third-party/Makefile') }} - - - name: Go Build Cache - uses: actions/cache@v3 - with: - path: ${{ steps.cache-paths.outputs.go-build }} - key: ${{ runner.os }}-go-build-${{ hashFiles('go.sum') }} - - - name: Go Mod Cache - uses: actions/cache@v3 - with: - path: ${{ steps.cache-paths.outputs.go-mod }} - key: ${{ runner.os }}-go-mod-${{ hashFiles('go.sum') }} - - - name: GoLint Cache - uses: actions/cache@v3 - with: - path: ${{ env.GOLANGCI_LINT_CACHE }} - key: ${{ runner.os }}-go-lint-ci - - - name: Node Modules Cache - uses: actions/cache@v3 - with: - path: node_modules - key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock') }} - - - name: Setup | Rust - uses: ATiltedTree/setup-rust@v1 - with: - rust-version: stable - - - name: Install Development Dependencies - run: make dev_deps - - - name: Run Linter - run: make lint - - - name: Install Dependencies - run: | - sudo apt-get update - sudo apt-get install -y \ - ca-certificates \ - build-essential \ - curl \ - ninja-build \ - meson \ - git \ - nasm \ - openssl \ - pkg-config \ - cmake \ - libssl-dev \ - libpng-dev \ - zlib1g-dev \ - libx264-dev \ - libx265-dev \ - libvpx-dev \ - libopenjp2-7-dev \ - libssl-dev \ - gifsicle \ - optipng - - - name: Compile External Dependencies - run: make -C cpp external - - - name: Build CPP Applications - run: make -C cpp build - - - name: Move built files to /usr/local - run: | - sudo cp -r cpp/out/* /usr/local - sudo ldconfig - - - name: Run Tests - run: make test - - - name: Build App - run: make build - - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v1 if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }} @@ -182,11 +75,11 @@ jobs: if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }} with: context: . - file: docker/partial.Dockerfile - # cache-from: | - # type=registry,ref=gha - # cache-to: | - # type=registry,ref=gha,mode=max + file: docker/full.Dockerfile + cache-from: | + type=registry,ref=gha + cache-to: | + type=registry,ref=gha,mode=max tags: | ${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/image-processor:latest ${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/image-processor:${{ github.sha }} diff --git a/go/internal/image_processor/worker.go b/go/internal/image_processor/worker.go index b81191f8..a14774c8 100644 --- a/go/internal/image_processor/worker.go +++ b/go/internal/image_processor/worker.go @@ -730,23 +730,40 @@ func (Worker) resizeFrames(ctx global.Context, inputDir string, tmpDir string, t } if tsk.ResizeRatio == task.ResizeRatioNothing { - smwf := float64(tsk.SmallestMaxWidth) - wf := float64(width) - smhf := float64(tsk.SmallestMaxHeight) - hf := float64(height) + // Get the largest scale factor + scale := 1 + for _, s := range tsk.Scales { + if s > scale { + scale = s + } + } + + // Divide the width and height by the scale factor + // This is integer division, so it will round down + width /= scale + height /= scale - if smwf < wf { - hf *= smwf / wf - wf = smwf + // If the width is bigger than the max width, then scale it down + if width > tsk.SmallestMaxWidth { + width = tsk.SmallestMaxWidth + height = int(float64(height) * (float64(tsk.SmallestMaxWidth) / float64(width))) } - if smhf < hf { - wf *= smhf / hf - hf = smhf + // If the height is bigger than the max height, then scale it down + if height > tsk.SmallestMaxHeight { + height = tsk.SmallestMaxHeight + width = int(float64(width) * (float64(tsk.SmallestMaxHeight) / float64(height))) } - width = int(math.Round(wf)) - height = int(math.Round(hf)) + // If the width is 0, then set it to 1 + if width == 0 { + width = 1 + } + + // If the height is 0, then set it to 1 + if height == 0 { + height = 1 + } tsk.ResizeRatio = task.ResizeRatioStretch } else { From e49e0207ef3cf2a5636c9574148b527db2bfc585 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Wed, 23 Nov 2022 19:04:26 +0000 Subject: [PATCH 2/4] fix ci --- .github/workflows/ci.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 17f08fe5..f4884d11 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -77,9 +77,9 @@ jobs: context: . file: docker/full.Dockerfile cache-from: | - type=registry,ref=gha + type=gha cache-to: | - type=registry,ref=gha,mode=max + type=gha,mode=max tags: | ${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/image-processor:latest ${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/image-processor:${{ github.sha }} From 3aeef747978744c4af9d47d5cb809ab93e5150d4 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Wed, 23 Nov 2022 19:27:44 +0000 Subject: [PATCH 3/4] keep aspect ratio --- go/internal/image_processor/worker.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/go/internal/image_processor/worker.go b/go/internal/image_processor/worker.go index a14774c8..442bee2c 100644 --- a/go/internal/image_processor/worker.go +++ b/go/internal/image_processor/worker.go @@ -743,18 +743,18 @@ func (Worker) resizeFrames(ctx global.Context, inputDir string, tmpDir string, t width /= scale height /= scale - // If the width is bigger than the max width, then scale it down - if width > tsk.SmallestMaxWidth { - width = tsk.SmallestMaxWidth - height = int(float64(height) * (float64(tsk.SmallestMaxWidth) / float64(width))) - } - // If the height is bigger than the max height, then scale it down if height > tsk.SmallestMaxHeight { height = tsk.SmallestMaxHeight width = int(float64(width) * (float64(tsk.SmallestMaxHeight) / float64(height))) } + // If the width is bigger than the max width, then scale it down + if width > tsk.SmallestMaxWidth { + width = tsk.SmallestMaxWidth + height = int(float64(height) * (float64(tsk.SmallestMaxWidth) / float64(width))) + } + // If the width is 0, then set it to 1 if width == 0 { width = 1 From eb61f1723af46ca994909a3233d61a6517f6934e Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Thu, 24 Nov 2022 14:27:26 +0000 Subject: [PATCH 4/4] fix order of ops --- go/internal/image_processor/worker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/internal/image_processor/worker.go b/go/internal/image_processor/worker.go index 442bee2c..e9e0a79c 100644 --- a/go/internal/image_processor/worker.go +++ b/go/internal/image_processor/worker.go @@ -745,14 +745,14 @@ func (Worker) resizeFrames(ctx global.Context, inputDir string, tmpDir string, t // If the height is bigger than the max height, then scale it down if height > tsk.SmallestMaxHeight { - height = tsk.SmallestMaxHeight width = int(float64(width) * (float64(tsk.SmallestMaxHeight) / float64(height))) + height = tsk.SmallestMaxHeight } // If the width is bigger than the max width, then scale it down if width > tsk.SmallestMaxWidth { - width = tsk.SmallestMaxWidth height = int(float64(height) * (float64(tsk.SmallestMaxWidth) / float64(width))) + width = tsk.SmallestMaxWidth } // If the width is 0, then set it to 1