Running FFmpeg from CoolTweak


About this article

This technical tutorial is meant for developers who want to extend CoolTweak possibilities by using the FFmpeg library.

We are about to learn how to process your videos from CoolTweak with very practical examples.

If you're not already comfortable with custom commands, it's recommanded to read the introduction first.

It's also advised to read the ImageMagick tutorial which highlights additional concepts.

About FFmpeg

FFmpeg is a popular video processing library, used in many softwares and that can be easily called from a command line with arguments.
This way of working is perfect to embed it inside CoolTweak because we just need to reference the executable file and to provide it some parameters.

Visit the official FFmpeg website to learn more about it and go on zeranoe website to download the binary distribution of FFmpeg depending on your needs.

By default, download the lastest Static Realease version for your OS architecture (32 or 64 bit). There is no installer, just unzip the folder wherever you like. In the following examples, we chose to drop the folder at C:\FFmpeg, the main executable will thus be C:\FFmpeg\ffmpeg.exe.

Building a FFmpeg command

In all the following examples, we will define a common core configuration for the custom commands:
all basic settings will remain the same, we will just change the calling parameters depending on what we want to achieve.

Here are the core settings of our custom commands:
  • Command type: Executable file
  • Script path: C:\FFmpeg\ffmpeg.exe
  • Check option: Wait for the command to end before continuing
  • Select option: After the command ends, Reload the file from its current location

In this article, we just want a single action to be performed on the file, there is no need to make CoolTweak reload the file once it has been processed.

As you noticed, no calling parameters are defined yet, we will see how to set them from several handy and real life examples.

Ignoring error output

By default, FFmpeg writes its casual log messages to standard error output, that's not convenient and you have 2 choices to handle that:

  • Uncheck option: "Fail if a message is sent to standar error output (stderr)" to tell CoolTweak to totally ignore all these messages.
  • Let this option checked but add to your calling parameters the switches "-loglevel panic -hide_banner" that reduce the verbosity to only critical errors.
    This is the recommanded way to go.

Saving processed file

With the default settings, the processed video will be moved to a "Cooltweak" folder and will have the same name as the original file.

We can stay it this mode but we also can choose to manually handle the processed file saving by checking "Do not save the processed files" option from "Move and rename" menu of "Output options" tab.

In this case, CoolTweak will not be in charge of the storage of your processed files anymore, that's up to us to tell FFmpeg the location where to save the file from the command calling parameters.
To highlight this possibility, we decide to send the processed file back to its original directory but with a new name in the next examples.

Practical FFmpeg embedding examples

FFmpeg enables you to achieve many operations on your video files, here is an overview of its possibilities and how to trigger them from CoolTweak.

Converting a video to MP4

The original FFmpeg syntax is like:
ffmpeg.exe -i video.wmv -c:v libx264 -preset ultrafast newvideo.mp4

Calling parameters:
-i "{FilePath}" -c:v libx264 -preset ultrafast "{OriginalFileDirectoryPath}{OriginalFileNameWithoutExtension}.mp4"

These parameters tell FFmpeg to convert the current temporary file to mp4 and to move it to the original folder preserving the orginal file name but with mp4 extension.

Rotate a video of 90 degrees

The original FFmpeg syntax is like:
ffmpeg.exe -i video.mp4 -filter:v 'transpose=1' newvideo.mp4
(and 'transpose=2' for 90 degrees counterclockwise)

Calling parameters:
-i "{FilePath}" -vf 'transpose=1' -c:a copy "{OriginalFileDirectoryPath}{OriginalFileNameWithoutExtension}-rotated{OriginalFileNameExtension}"

These parameters tell FFmpeg to rotate the current temporary file of 90 degrees (clockwise) and then to move it to the orginal folder with the original file name having the -rotated suffix.

Resizing a video

The original FFmpeg syntax is like:
ffmpeg.exe -i video.mp4 -s 480x320 -c:a copy newvideo.mp4 (to 320p)

Calling parameters:
-i "{FilePath}" -s 480x320 -c:a copy "{OriginalFileDirectoryPath}{OriginalFileNameWithoutExtension}-320p{OriginalFileNameExtension}"

These parameters tell FFmpeg to resize the current temporary file to 480x320px and then to move it to the orginal folder with the original file name having the -320p suffix.

Extracting audio from video

The original FFmpeg syntax is like:
ffmpeg.exe -i video.mp4 audio.mp3

Calling parameters:
-i "{FilePath}" "{OriginalFileDirectoryPath}{OriginalFileNameWithoutExtension}.mp3"

These parameters tell FFmpeg to extract the audio track from the current temporary file and to save it to the orginal folder with the original file name having the mp3 extension.

Snapshot of video at a given time

The original FFmpeg syntax is like:
ffmpeg.exe -ss 00:00:15 -i video.mp4 -vf scale=800:-1 -vframes 1 image.jpg
(here the snapshot is taken at 15s and is 800px wide)

Calling parameters:
-i "{FilePath}" -ss 00:00:15 -vf scale=800:-1 -vframes 1 "{OriginalFileDirectoryPath}{OriginalFileNameWithoutExtension}.jpg"

These parameters tell FFmpeg to extract the 800px large picture at the 15th second from the current temporary file and to save it to the orginal folder with the original file name having the jpg extension.

Snapshot of video every n seconds

The original FFmpeg syntax is like:
ffmpeg.exe -i video.mp4 -vf fps=1/5 frame_%04d.jpg
(here, a picture is extracted every 5s)

Calling parameters:
-i "{FilePath}" -vf fps=1/5 "{OriginalFileDirectoryPath}{OriginalFileNameWithoutExtension}_%04d.jpg"

These parameters tell FFmpeg to extract a snapshot every 5 seconds from the current temporary file and to save them to the orginal folder with a file name containing a incremented counter on 4 digits.

Converting video to animated gif

The original FFmpeg syntax is like:
ffmpeg.exe -i video.mp4 -vf scale=500:-1 -t 10 -r 10 image.gif
(here a gif with a width of 500px and a frame every 10s)

Calling parameters:
-i "{FilePath}" -vf scale=500:-1 -t 10 -r 10 "{OriginalFileDirectoryPath}{OriginalFileNameWithoutExtension}.gif"

These parameters tell FFmpeg to create a animated gif from the current temporary file and to save them to the orginal folder with the original file name having the gif extension.

FFmpeg from an event command

To end this article, we will see how to call FFmpeg on the whole set of input files (all the files to process) in order to create a video slideshow from them.
This will attest that embedding FFmpeg is also very easy from event commands.

In this example, we consider that there is a folder at C:\pictures containing a set of pictures numbered from picture1.jpg to picture5.jpg.

The original FFmpeg syntax is like:
ffmpeg.exe -r 1/5 -i img%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p slideshow.mp4

Calling parameters:
-r 1/5 -i "{FileDirectoryPathes}photo%01d.jpg" -c:v libx264 -r 30 -pix_fmt yuv420p "{FileDirectoryPathes}slideshow.mp4"

We are using the {FileDirectoryPathes} variable which contains the pathes of all the parent folders of the files to process.
In our case all files are under the same folder so its actual value will be resolved as C:\pictures.

We request FFmpeg to combine all the images using alphabetical order to build a video slideshow that will be saved in the same folder with the extension .mp4

And now ?

Now, you menu is ready to use, you just have to save your changes (with the lower right button of menu editor) to be able to run your commands from a single right click on your files and folders.

FFmpeg is a really mighty library, there are tons of possibilities to explore and to play with.
If you ever find crazy cool new features during your experiments and you consider they should be available natively in CoolTweak, don't hesitate to share with us, all your suggestions are always welcome.

Happy coding !