Convert any video to general purpose .mp4 format with constant video quality

By ValkaTR, 7 September, 2023

It is convinient to have a quick script that let convert zoo of various different formats like .webm, .mkv, .avi and other to .mp4, that is playable and supported on majority of devices. Like when you downloaded some videomeme and want to share it with friends in telegram, but telegram doesn't support .webm that well, especially on mobile, and after upload it appears as a file instead of conviniently playable video.

Here I present a simple script I created to make it easier to do this conversion. Normally I would open the video in kdenlive or avidemux, configure it to output proper format and render, but that takes way too much of clicks.



if test "$1" = "$filename"
zenity --text --title "MP4ify — Convert any video to general purpose mp4 with \"-crf 20\" quality (by ValkaTR)" --text="error: input file has no extension"
exit -1


# input.avi is the input file.
# -c:v libx264 selects the video encoder libx264, which is a H.264 encoder.
# -preset slow selects the slow x264 encoding preset. Default preset is medium. Use the slowest preset that you have patience for.
# -crf 20 selects a CRF value of 20 which will result in a high quality output. Default value is 23. A lower value is a higher quality. Use the highest value that gives an acceptable quality.
# -c:a aac selects the audio encoder aac, which is the built-in FFmpeg AAC encoder.
# -b:a 160k encodes the audio with a bitrate of 160k.
# -vf format=yuv420p chooses YUV 4:2:0 chroma-subsampling which is recommended for H.264 compatibility.
# -movflags +faststart is an option for MP4 output that move some data to the beginning of the file after encoding is finished. This allows the video to begin playing faster if it is watched via progressive download playback.
# output.mp4 is the output file.

xfce4-terminal --hold --command="ffmpeg -i \"$1\" -c:v libx264 -preset slow -crf 20 -c:a aac -b:a 256k -vf format=yuv420p -movflags +faststart \"$newfilename\""


The script uses xfce4-terminal to display the process of conversion. The script alone is not enought to make conversion proccess comfortable yet. Imagine, you'd have to type commandline every time. Thankfully, my favorite file manager Thunar has option to add 'special actions' on files. Let's add my script:

  1. Goto View -> Special Actions


  1. Add new 'special action'


  1. Configure the new 'special action' to appear only on video files and save changes


  1. Right click on video file that needed to be converted and select our tool


  1. Watch the conversion proccess or do other thing while it works in background


  1. When the conversion is completed, close the terminal


  1. Enjoy the result

Video: Half-Life 2 OST — Lab Practicum (Extended)_reenc.mp4 (117,7 MiB).


In this guide I used my own system with Manjaro Linux distribution running XFCE desktop environment with Thunar file manager. I believe it should be possible to adapt this script on KDE/Dolphin or GNOME/Nautilus or other advanced environments.

The script might be improved by adding support for multiple file selection, sound and/or visual notification when the conversion is completed. Thank you for your attention ^^