videosort.py to interactively select or delete video files, based on mplayer
First, create ~/.mplayer/input.conf
with the following content:
DEL run "rm '${path}' && killall mplayer"
INS run "mkdir -p $(dirname '${path}')/selected && mv '${path}' $(dirname '${path}')/selected && killall mplayer"
This will create a keybinding for DEL
and INS
to delete or move the file to a selected
directory, respectively.
Note that deleted files are not moved to the trash but permanently deleted. Backup your files if you deem it neccessary
Then, create a script videosort.py
:
#!/usr/bin/env python3
import argparse
import subprocess
from pathlib import Path
VIDEO_EXTENSIONS = {
'.mp4', '.avi', '.mkv', '.mov', '.wmv',
'.flv', '.webm', '.m4v', '.mpg', '.mpeg'
}
def get_sorted_files(directory, ascending=True, lexicographic=False):
# Get all files in directory
files = Path(directory).glob('*')
# Filter for video files only
files = [f for f in files if f.is_file() and
f.suffix.lower() in VIDEO_EXTENSIONS]
if lexicographic:
# Sort by filename
sorted_files = sorted(files, reverse=not ascending)
else:
# Sort by filesize
sorted_files = sorted(files,
key=lambda x: x.stat().st_size,
reverse=not ascending)
return sorted_files
def main():
parser = argparse.ArgumentParser(
description='Play files in directory sorted by size using mplayer')
parser.add_argument('directory', help='Directory containing files to play')
parser.add_argument('-a', '--ascending', action='store_true',
help='Sort by ascending size instead of descending')
parser.add_argument('-l', '--lexicographic', action='store_true',
help='Sort lexicographically instead of by size')
parser.add_argument('-f', '--fullscreen', action='store_true',
help='Play videos in fullscreen mode')
parser.add_argument('-s', '--speed', type=float, default=1.0,
help='Playback speed multiplier (default: 1.0)')
parser.add_argument('--half', action='store_true',
help='Play videos in half screen size')
parser.add_argument('-m', '--mute', action='store_true',
help='Mute audio output')
args = parser.parse_args()
if args.speed <= 0:
parser.error("Speed must be a positive number")
# Get sorted file list
files = get_sorted_files(args.directory,
args.ascending,
args.lexicographic)
# Get screen resolution if half screen mode is requested
if args.half:
try:
xrandr = subprocess.check_output(['xrandr']).decode()
resolution = [l for l in xrandr.splitlines() if ' connected' in l][0]
width = int(resolution.split()[2].split('x')[0])
height = int(resolution.split()[2].split('x')[1].split('+')[0])
geometry = f'{width//2}x{height//2}'
except:
print("Warning: Could not detect screen size, using default")
geometry = '960x540'
# Play each file
for file in files:
# -idle makes mplayer pause at end instead of exit
cmd = ['mplayer', '-loop', '0', '-fixed-vo']
if args.fullscreen:
cmd.append('-fs')
elif args.half:
cmd.extend(['-geometry', geometry])
if args.mute:
cmd.append('-ao')
cmd.append('null')
cmd.append('-speed')
cmd.append(str(args.speed))
# Input file
cmd.append(str(file))
try:
# Run mplayer and wait for it to complete
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
print(f"Error playing {file}: {e}")
except KeyboardInterrupt:
print("\nPlayback interrupted by user")
break
if __name__ == '__main__':
main()
When you run it, it will run mplayer
on one file after another, allowing you to press DEL
to delete the file or INS
to move it to a selected
directory. You can also press q
to skip to the next file without deleting or moving it.
The many command line options allow you to customize the behavior of the script, such as sorting order, playback speed, fullscreen mode, and more.
usage: videosort.py [-h] [-a] [-l] [-f] [-s SPEED] [--half] [-m] directory
Play files in directory sorted by size using mplayer
positional arguments:
directory Directory containing files to play
options:
-h, --help show this help message and exit
-a, --ascending Sort by ascending size instead of descending
-l, --lexicographic Sort lexicographically instead of by size
-f, --fullscreen Play videos in fullscreen mode
-s SPEED, --speed SPEED
Playback speed multiplier (default: 1.0)
--half Play videos in half screen size
-m, --mute Mute audio output
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow