videosort.py:基于 mplayer 交互式选择或删除视频文件
首先,创建 ~/.mplayer/input.conf,内容如下:
videosort_input.conf
DEL run "rm '${path}' && killall mplayer"
INS run "mkdir -p $(dirname '${path}')/selected && mv '${path}' $(dirname '${path}')/selected && killall mplayer"这将为 DEL 和 INS 创建键绑定,分别用于删除文件或将文件移动到 selected 目录。
注意,被删除的文件不会移到回收站,而是永久删除。如果你认为有必要,请备份你的文件
然后,创建脚本 videosort.py:
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()运行时,它将一个接一个地对文件运行 mplayer,允许你按 DEL 删除文件或按 INS 将其移动到 selected 目录。你也可以按 q 跳到下一个文件而不删除或移动它。
许多命令行选项允许你自定义脚本的行为,如排序顺序、播放速度、全屏模式等。
videosort_usage.txt
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 outputCheck out similar posts by category:
Audio/Video, Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow