Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supported media file types / extensions #332

Open
PatMyron opened this issue Feb 22, 2019 · 18 comments
Open

Supported media file types / extensions #332

PatMyron opened this issue Feb 22, 2019 · 18 comments
Labels
enhancement help wanted pinned prevents stalebot from marking an issue as stale

Comments

@PatMyron
Copy link

PatMyron commented Feb 22, 2019

These seem like the current supported file extensions:

https://github.com/dabutvin/ImgBot/blob/392f7b20e4117e363b78e2537e4d915cabefcc26/Common/KnownImgPatterns.cs#L5

Would love additional formats like .svg, .pdf, and audio+video formats like .mp4

@dabutvin
Copy link
Contributor

hey @PatMyron
Would love to see imgbot expanding out like this.

I had this thought the other day about svgs, but couldn't find a good dotnet optimizer for them and I naively started my own (https://github.com/dabutvin/SvgClean)

I bet we can find existing optimizers for videos + pdfs though! I hadn't considered them before.
Do you have any suggestions for optimizers for any of these file types?

@PatMyron
Copy link
Author

PatMyron commented Feb 22, 2019

FFmpeg?

@geeknoid
Copy link

geeknoid commented Mar 3, 2019

I recommend integrating with https://github.com/svg/svgo which does an excellent job in crunching down SVGs. It'd be great to have ImgBot take care of those.

@dabutvin
Copy link
Contributor

dabutvin commented Mar 3, 2019

@geeknoid that would be really cool. I wonder what the best way to fit that into the rest of the compression workflow would be, given that svgo is all JavaScript

@geeknoid
Copy link

geeknoid commented Mar 4, 2019

svgo can run on a specific file, on a specific directory, or on a specific directory recursively.

I don't know how ImgBot is put together, but if I were to hack this together, I'd just kick off "svgo -r -f " as an external command. Any file it modifies, you include in the PR you post.

Now to run svgo, you would need to install it (npm install svgo -g) and then use the npx tool with it (npx svgo ). If this isn't working for some reason, an alternate solution is to create a docker image that contains svgo and use 'docker run' to execute it.

@dabutvin
Copy link
Contributor

dabutvin commented Mar 5, 2019

makes sense @geeknoid 👍

I just opened #341 which would make this a lot more feasible - along with some potential performance improvements

@dabutvin
Copy link
Contributor

dabutvin commented Apr 5, 2019

It would be great to get svgo runnable as an executable, alternatively we would have to install node on the container - might not be so bad

@dabutvin dabutvin mentioned this issue May 13, 2019
@dabutvin
Copy link
Contributor

just opened #386 to start adding svg support with svgo.
There's some more work to do, but it's a start

@stale
Copy link

stale bot commented Jul 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the goneStale label Jul 21, 2019
@stale stale bot closed this as completed Jul 28, 2019
@PatMyron
Copy link
Author

PatMyron commented Jul 28, 2019

@dabutvin re-open?

@dabutvin dabutvin reopened this Jul 29, 2019
@stale stale bot removed the goneStale label Jul 29, 2019
@guntbert guntbert added the pinned prevents stalebot from marking an issue as stale label Aug 18, 2019
@dabutvin
Copy link
Contributor

SVG support is in!!!!

@dabutvin
Copy link
Contributor

see #488 for an example of an issue for adding a new compression library.
We can use the same template for adding whole new file extensions.

@gleich
Copy link

gleich commented Jan 17, 2020

Any update of pdf compression? It would be really nice for people that use LaTex.

@dabutvin
Copy link
Contributor

Hi @Matt-Gleich !

I'm totally cool with adding pdf compression. You recommend any libraries or tools that we should look at?

@gleich
Copy link

gleich commented Jan 17, 2020

A quick DuckDuckGo search brought up some useful tutorials on how to compress PDFs in C#
https://duckduckgo.com/?q=how+to+compress+pdfs+with+c%23&atb=v181-1&ia=web

As far as anything that I have first-hand experience with, no I have done no work with C#.

@karlhorky
Copy link

@dabutvin is this project still being actively developed in 2023? I would be interested in the PDF compression/optimization support - is this already an accepted feature and just needs someone to take time to implement PDF compression in C#? Would guess that there would be libraries for this too... 🤔

@karlhorky
Copy link

karlhorky commented Mar 24, 2023

Alternative 1: I've also created an issue in the GitHub issues for Calibre Image Actions:

@karlhorky
Copy link

karlhorky commented Mar 24, 2023

Alternative 2

A separate GitHub Actions workflow to do this using ghostscript (generate an access token for this and add it as a GitHub repo secret called PDF_COMPRESSION_GITHUB_TOKEN):

name: Compress PDFs
on:
  pull_request:
    # Run only when PDF files are added or changed
    paths:
      - '**.pdf'

jobs:
  build:
    # Only run on Pull Requests within the same repository, and not from forks.
    if: github.event.pull_request.head.repo.full_name == github.repository
    name: Compress PDFs
    runs-on: ubuntu-latest
    steps:
      - name: Run apt update
        run: sudo apt update
      - name: Install Ghostscript for compression
        run: sudo apt install -y ghostscript
      - name: Checkout Repo
        uses: actions/checkout@v3
        with:
          ref: ${{ github.head_ref }}

      - name: Compress PDFs
        run: |
          find . -type f -iname '*.pdf' -not -path "./node_modules/*" | while read file; do
            echo "Compressing $file"
            # Compress the file for screen using Ghostscript
            gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile="./temp.pdf" "$file"
            mv ./temp.pdf "$file"
          done
      - name: Commit files
        run: |
          git config user.email github-actions@github.com
          git config user.name github-actions

          # Discard modified compressed files that are not more
          # than 10KB smaller than the previous version
          git ls-files -z --modified '*.pdf' | while IFS= read -r -d '' file; do
            old_size=$(git cat-file -s "HEAD:$file")
            new_size=$(wc -c < "$file" | tr -d ' ')
            if [[ $(($old_size - $new_size)) -lt 10000 ]]; then
              echo "Discarding changes to $file, compressed file size $new_size is not >10KB smaller than old size $old_size"
              git checkout -- "$file"
              continue
            fi
            echo "Compressed $file from $old_size to $new_size bytes"
          done

          git add **/*.pdf
          if [ -z "$(git status --porcelain)" ]; then
            exit 0
          fi
          git commit -m "Compress PDFs"
          git push origin HEAD:${{ github.head_ref }}
        env:
          GITHUB_TOKEN: ${{ secrets.PDF_COMPRESSION_GITHUB_TOKEN }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted pinned prevents stalebot from marking an issue as stale
Projects
None yet
Development

No branches or pull requests

7 participants
@guntbert @karlhorky @dabutvin @PatMyron @geeknoid @gleich and others