Paul
Paul

Reputation: 4450

Reduce image gif size to send on Discord

I'm trying to compress to reduce the size of a gif to send it on Discord, below is the code I wrote.

But I would like to find a solution to reduce the gif without compromising the quality, such as the colors, too much.

To send a file on Discord it must be under or equal to 10 MB.

name: Send GIF to Discord

on:
  workflow_dispatch:
    inputs:
      gif_url:
        description: 'GIF URL'
        required: true

jobs:
  send_gif:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Download the GIF
        run: curl -L "${{ github.event.inputs.gif_url }}" -o image.gif

      - name: Check the file size
        id: check_size
        run: |
          file_size=$(stat -c %s "image.gif")
          max_size=10485760  # 10 MB in bytes
          echo "File size: $(echo "scale=2; $file_size / 1024 / 1024" | bc) MB"
          if [ "$file_size" -gt "$max_size" ]; then
            echo "compress=true" >> $GITHUB_ENV
          else
            echo "compress=false" >> $GITHUB_ENV
          fi

      - name: Install gifsicle
        run: |
          sudo apt-get update
          sudo apt-get install -y gifsicle

      - name: Reduce the GIF size (if necessary)
        if: ${{ env.compress == 'true' }}
        run: |
          echo "Reducing the GIF size to fit under 10 MB"
          gifsicle -O1 --colors 256 image.gif > compressed_image.gif
          file_size_reduce=$(stat -c %s "compressed_image.gif")
          echo "File size reduced: $(echo "scale=2; $file_size_reduce / 1024 / 1024" | bc) MB"

      - name: Send GIF to Discord via Webhook
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
        run: |
          if [ "${{ env.compress }}" == "true" ]; then
            file_to_send="compressed_image.gif"
          else
            file_to_send="image.gif"
          fi

          curl -X POST "$DISCORD_WEBHOOK" \
            -H "Content-Type: multipart/form-data" \
            -F "file=@$file_to_send" \

Upvotes: -3

Views: 32

Answers (0)

Related Questions