Skip to main content

Image and video files compression

Write something at the front

As a content manager, I should learn about the impact from different size of images. Hence, I will try to standardize my image size in the future.

Clean my first weeek of oversized images

My previous assignment is way too large and it seems I am in the 6th place of largest repo at the last time. I need to make my repo small.

image1

  1. Find my files that occupy high memory. I went through all my folders and look for images over 200kb and remembered them. For example:

image2

  1. I will reduce memory to turn these ".png" images into ".jpg" images. At the same time, I want to ensure all my images have the same width, where I wanted to make it 600px.

I use a tool called "Squoosh", which can meet my requirements. It can also show me what is the image going to look like after the conversion:

image3

The 152 KB is way more less than 3.2 MB.

image4

I delete and check how the image look like at the orginal place: all seems fine.

  1. I changed all the images that are above 200 KB and the folder is much smaller:

image5

Standardize my images in the future

  • I think the 600px width images are fine. The length should be set as "auto".
  • And I decide all my images should be under 200 KB.
  • I should create image folder and named as every week, since I sense there will be a lot of images in the future.

image6

Compress MP4 file

ffmpeg is a great tool for compressing the size of video.

I download it from the official website and run these commend:

$ sudo mv ffmpeg /usr/local/bin/   //move the ffmpeg Unix file into /usr/local/bin/
$ sudo chmod +x /usr/local/bin/ffmpeg //call it

The common comment is:

ffmpeg -i input.mp4 -vf "scale=300:600" -c:v libx264 -crf 23 -preset fast output.mp4

where:

  • -i input.mp4: Specifies the input file.
  • -vf "scale=300:-1": Sets the video filter to change the width of the video to 300 pixels. The height is set to -1, which means it will be calculated automatically to maintain the aspect ratio of the original video.
  • -c:v libx264: Uses the libx264 encoder, a highly efficient H.264 video codec.
  • -crf 23: Sets the Constant Rate Factor (CRF), where 23 is a balance point that offers a reasonable trade-off between quality and file size. Adjusting this value can lead to higher quality or smaller file sizes.
  • -preset fast: This option controls the trade-off between encoding speed and compression efficiency. fast provides a faster encoding speed. Depending on your needs, you can choose from ultrafast, superfast, veryfast, faster, medium (the default), slow, slower, or veryslow.
  • output.mp4: Specifies the name of the output file.

image7

Works really great!