Skip to content

Instantly share code, notes, and snippets.

@mh-firouzjah
Created May 31, 2023 15:14
Show Gist options
  • Save mh-firouzjah/be0d38298f2aa6f574ca8b40fcd40e83 to your computer and use it in GitHub Desktop.
Save mh-firouzjah/be0d38298f2aa6f574ca8b40fcd40e83 to your computer and use it in GitHub Desktop.
Zero-Pad this is a zsh script in order to zero-pad mp4 & srt files so media players will always play them in natural integer format.

Zero-Pad

When I use linux OS, SMplayer is my favorite vidoe player but this media player has very anoying bug in it, beside all the havy customaization available via a very nice and simple GUI,it has.

The bug is this media player will sort any files that their names astriting with digits in alphabetical order regardless of natural integer numbers order, so for example after 1. file-name.mp4 it will jump to 11. file-name.mp4 instead of 2. file-name.mp4.

  • this bug is reporeted so many times but it's still there and no fix have been issued yet.

But I've figured out that if somehow we rename our video files in order to zero-pad files names this problem will be passed. the order of 001. file-name.mp4 => 002. file-name.mp4 => ... => 011. file-name.mp4 is OK!.

In ZSH:

autoload -Uz zmv
zmv '(**/)(<->)(*.(mp4|srt))(#q.)' '$1${(l[4][0])2}$3'

In Python:

import subprocess
from pathlib import Path

cmd = r'find ./ -type f -regex "[^0].*\.\(mp4\|srt\)"'
exc = subprocess.check_output(cmd, shell=True)

pathes = list(map(bytes.decode, exc.splitlines()))
files = list(map(Path, pathes))

for file in files:
    if file.name[0].isdigit() and file.name[0] != "0":
        num = ""
        for char in file.name:
            if not char.isdigit():
                break
            num += char
        file.rename(file.with_name(file.name.replace(num, num.rjust(3, "0"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment