Recently I needed to cut a fragment out of a video without re-encoding, and, disappointed with LosslessCut once again, I went looking for alternatives.
On Reddit I came across a post claiming that the mpv-cut script could be used for this — it works without an external graphical interface or extra programs.
On closer inspection, mpv-cut turned out to be a Lua script that let you cut a piece out of a video right while watching it in the mpv player. I had somehow never assumed that the mpv player, for all its minimalism, supports scripts that extend its functionality. So I decided to dig into the matter thoroughly.
Using mpv-cut
Let's start with the script itself: how to install it and how to use it.
mpv has a configuration folder. On Linux it is located at ~/.config/mpv, on Windows at %AppData%\Roaming\mpv. Create a scripts directory in that folder (if there isn't one there already) and clone the Git repository into it:
git clone -b release --single-branch "https://github.com/familyfriendlymikey/mpv-cut.git" ~/.config/mpv/scripts/mpv-cut
ffmpeg must also be installed and added to the PATH environment variable.
If you then start watching a file in the mpv player, the script launches automatically. When you reach the fragment you need, press "c" (on an English-layout keyboard) to start copying the video. Press "c" again to finish copying. After that the video is saved to the same directory as the source file, with a name roughly like this: COPY_1_video_FROM_00-00-07-457_TO_00-00-21-332.mkv.
You can play it back and check that all the streams present in the source file are also present in the same format — we just cut a piece out. Shift-I (I for Info) prints track information to the screen.
At this point you can look at the main.lua file in the mpv-cut folder, but it is probably too complex for a first script. So let's first write a simple Lua script for mpv that prints 'Hello, world!' to the terminal.
Hello, world for the mpv player.
Open the file hello.lua in the scripts folder in your favorite text editor and write the following code into it:
function hello_world()
print("Hello, World!")
end
mp.add_key_binding("h", "hello_world", hello_world)
For mpv scripts to work, you don't need to install Lua locally. The Lua interpreter is built into the player itself.
mpv has no graphical menu, so we bind functions to keypress events. In this case, if you press the "h" key, the message "Hello, World!" is printed to the terminal.
Here I've placed a short video clip for testing, taken from Sintel, a free and freely distributed cartoon from blender.org. Later I will use it to show how to print a list of subtitles to the terminal. For now, play this or any other file, press the "h" key a few times, and make sure the "Hello, World!" message really does get printed to the terminal.
Subtitle list
Now let's build an example that shows how to pull information out of the video that is playing. Instead of "Hello, World!", we will print the list of subtitles to the terminal.
function show_subtitles()
print("Список субтитров:")
print("ID Язык Формат")
local track_list = mp.get_property_native("track-list")
for _, track in ipairs(track_list) do
if track.type == "sub" then
print(track.id, track.lang, track.codec)
end
end
end
mp.add_key_binding("h", "show-subtitles", show_subtitles)
The mp.get_property_native("track-list") function in mpv returns the list of all tracks in the current video file. This list includes video, audio and subtitles with their metadata, such as ID, language, codec type and other properties.
This time we bind the "show_subtitles" function to pressing the "h" key.
Our script prints the subtitles like this:
[hello] Список субтитров:
[hello] ID Язык Формат
[hello] 1 ger subrip
[hello] 2 eng subrip
[hello] 3 spa subrip
[hello] 4 fre subrip
...
The mpv player has its own "j" shortcut (the key next to "h") that cycles the selected subtitle track and prints information about the current subtitles to the terminal. By pressing "h" and "j" and comparing the results, you can verify that our script works correctly.
Where to find information
That was really the point: to show that mpv supports Lua scripting and to give a couple of examples. So I will just say where you can find additional material and documentation.
You can read about Lua scripting in mpv in the official documentation.
Examples of Lua scripts that ship with the mpv package are located on Linux at: /usr/share/doc/mpv/examples/lua/. These same scripts are available in the mpv Git repository.
A large collection of user scripts is available at: https://github.com/mpv-player/mpv/wiki/User-Scripts
On mpv's configurability
All of this tunability and configurability in the mpv video player is simply striking and opens up many possibilities for users.
When learning languages it is convenient to display subtitles in two languages at once (frankly, mpv supports this even without scripts). With scripts you can do much more: for example, build Anki cards straight from a video, do light editing such as cropping the video, and much more. And my first example — the ability to cut out and save a fragment right during video (or music) playback — is, in my view, quite illustrative as well.
Take another look at the collection of user scripts — perhaps there is already a solution for what you need, or a hint about how it can be done.