SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

[Distribution] I made a tool to convert videos to GIFs and add watermarks for note - Python x OpenCV x Pillow for comfortable article creation by pasting screen recordings

Hello, this is Rcat.
I didn't really mind it in articles about programming, but the moment I started writing about 3D printers, I realized that it's hard to do with just still images.

Uploading videos to YouTube and pasting the links might be fine, but it consumes data and uploading is surprisingly troublesome.
However, there are images that animate, right? Yes, GIFs.
So, this time, I'm going to convert screen recordings to GIFs and add my copyright notice while I'm at it.

↓ Execution result (How to use this work)
It is loaded as an image like this. A copyright notice is embedded in the center.


How to use

First, I will introduce how to use this tool.
Distribution is at the bottom, so please get it from there.

The first step is to install Python.
If you are a beginner, please refer to the following article to install it.

Checking the distributed data

Once the installation is complete, the py file among the distributed data will change to the Python icon.

Distributed data

Building the execution environment

After installing, please double-click the gear icon named start.
A black screen will launch for a while and then close.
This work supports automatic environment construction, so the environment will be built with this.

When the black screen closes, there will be changes in the data.
First, a folder named VENV has been created. And a file named MP4Togif has also been created. Let's open "mp4togif.json" among these.

Changing the configuration file

It looks like this when opened with Windows Notepad. This is a configuration file. It currently contains the default settings, but please rewrite this as you like.

  • FPS
    Sets the number of frames per second for the GIF file.
    Increasing it makes the movement smoother, but the size will increase, so I recommend not increasing it too much.

  • watermark
    Adds a digital watermark.
    If you don't use it, please delete the text and leave it as "".You probably can't use Japanese.

  • size
    This is the image size. You can specify the height and width directly, specify only one side, or keep it within a certain range.

    • When specifying directly
      In the case of [1920,1080], the image will be resized to this size.

    • When setting one side to -1
      In the case of [500,-1], the size of the side with the number is prioritized. In this case, width 500, height ???.

    • When setting one side to 0
      In the case of [500,0], the longer or shorter side is prioritized. [Short side, Long side]. In the example case, it is resized so that the shorter side is 500.

    • When setting both to negative
      [-1000,-1000] is the default. It is resized to fit within this frame.

  • color
    Specifies the color of the watermark text in RGB.
    The values are between 0 and 255. In the case of [255,0,0], it will be bright red.

  • opacity
    Specifies the opacity. The smaller the value, the more transparent it is.

  • fontsize
    Specifies the font size. You can specify it as a percentage of the image height. This setting must be entered with a % sign.

  • position
    The position of the text. Specify it as a percentage.
    Positive values are from the left. Negative values are from the right. Values of 100 or more are adjusted to be centered. The default is centered.

In the default settings, the image is resized so that either the width or height is 1000 pixels or less, and the rcat copyright notice is added in gray text with 70% opacity, a font size of 10% of the height, and centered.
That's long...

If you make a mistake in writing the configuration file and it won't start, just delete it and it will be recreated with the defaults.

How to run

Once you have finished the configuration and environment setup,drag and drop the video you want to convert onto start.bat.
If the environment setup and configuration were done correctly,a black screen will appear, and after a while, a GIF file with the same name will be created in the folder where the video was.

Regarding the size of the finishedGIF file, it seems that about 3MB is the limit for uploading to note. If you try a few times and it fails, try changing the image size or lowering the FPS to make the file size smaller. Let's do it.

FAQ

I get an error with pillow

Did you build the environment using the included environment setup and batch files?
This project uses the 9.x version of pillow. It will not work with the latest version of the library.

Bonus: How to record your screen on Windows

I am using the tool introduced in this article to convert the videos, but you might be wondering how I recorded these videos in the first place.
As of Windows 11, there are two ways.

To capture a window

Win + Alt + R Pressing this will start recording immediately.
A small recording window will appear on the screen, so press that button when you want to finish.

If it doesn't start, press Win + G to open the Game Bar and check if you can start recording.

The save location is in Videos > Captures.


To capture a specific area

Win + Shift + R Pressing this will darken the screen like a screenshot, allowing you to select an area.
Once you select an area, a start button will appear on the screen, and you can press that button to start recording.

The save location is in Videos > Screen Recordings.

It has been confirmed that you can convert images with this tool using either method.


Explanation

From here on, this is more for those who are looking at programming-related topics than for general users.
If you just want to use it, please skip to the very end.
Since it's not that complicated this time, I will explain it to some extent.

Overview

The main source code fit into a single image.
Although it doesn't include the functions I created separately...

First, regarding the class instance part at the beginning, it only performs tasks like reading configuration files and outputting default settings.
Also, the color must be a tuple, otherwise an error will occur due to the program's requirements, so I am converting it to a tuple.

Reading and Conversion Part

This is the most important part, isn't it?
Here, I am reading the video while converting only the necessary frames into pillow objects.
In terms of programming style, I would like to separate video reading and GIF conversion, but this approach is faster and consumes less memory, so it is written this way.
Specifically, I determine how many frames to extract one frame from based on the difference between the video frame rate and the GIF frame rate, and extract them accordingly.
The extracted frames are converted into pillow objects, and if necessary according to the settings, resizing and watermarking are performed.
Once all image processing is finished, I follow the usual GIF output procedure.

However, it's truly amazing that Python can convert videos to GIFs with just this much code.
It seems there are also libraries for APNG, and although I've never seen them outside of LINE stickers, it seems they can also output APNG. I wonder which one is better.

Regarding resizing and watermarking, I am reusing source code that I also use in other paid works, so please download it and take a look.


Finally

Terms of Service

Please check the terms of service in advance when utilizing information or works.

About Comments

Please check the terms of service guidelines before commenting.

Distribution

It is distributed from the following URL.

https://script.google.com/macros/s/AKfycbxdcr8pnazR7RbjaSICTtaNWfN7h_rjQrKlZ3h9CZpPRFzRILk1OGc8mZqKbF-NXNO9/exec?name=MP4_GIF_Conversion

Please check below for the distribution information required for downloading.


いいなと思ったら応援しよう!

Rcat999 情報が役に立ったと思えば、僅かでも投げ銭していただけるとありがたいです。

この記事が参加している募集