Ranger添加自定义操作
起因
平时使用ranger就是遍历一下目录,选择打开一下文件。对于其他操作不太顺手。 这次想选择文件并执行一个自定义操作,比如用ffmpeg把flv无损转为mp4。
步骤
定义操作
修改~/.config/ranger/commands.py
,添加
from ranger.api.commands import Command
import os
class flv_to_mp4(Command):
"""
:flv_to_mp4
使用 ffmpeg 将选中的 .flv 文件转为 .mp4(使用 -c copy )
"""
def execute(self):
files = [f for f in self.fm.thistab.get_selection() if f.path.endswith(".flv")]
if not files:
self.fm.notify("未选中 .flv 文件", bad=True)
return
for f in files:
input_path = f.path
output_path = os.path.splitext(input_path)[0] + ".mp4"
# 如果输出文件已存在,则跳过
if os.path.exists(output_path):
self.fm.notify(f"已存在,跳过: {output_path}")
continue
cmd = f"ffmpeg -y -i '{input_path}' -c copy '{output_path}'"
self.fm.run(cmd)
self.fm.notify(f"转换完成: {output_path}")
绑定按键
在~/.config/ranger/rc.conf
中添加绑定fm
键为自定义操作。
map fm flv_to_mp4
使用
在ranger中选中文件,然后按fm
键执行转换操作。
最后修改于 2025-06-22