Local-Voice/cleanup_recordings.py
2025-09-25 11:25:16 +08:00

71 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
录音文件清理工具
用于清理过期的录音文件,管理磁盘空间
"""
import sys
import os
import argparse
from audio_processes import (
cleanup_recordings,
cleanup_recordings_by_count,
list_recordings,
print_recording_summary
)
def main():
parser = argparse.ArgumentParser(description='录音文件清理工具')
parser.add_argument('--list', '-l', action='store_true', help='列出所有录音文件')
parser.add_argument('--summary', '-s', action='store_true', help='显示录音文件摘要')
parser.add_argument('--cleanup-time', '-t', type=int, default=24,
help='按时间清理保留最近N小时的文件默认24小时')
parser.add_argument('--cleanup-count', '-c', type=int, default=50,
help='按数量清理保留最新的N个文件默认50个')
parser.add_argument('--dry-run', '-d', action='store_true',
help='模拟运行,不实际删除文件')
args = parser.parse_args()
if args.list:
print("📋 录音文件列表:")
recordings = list_recordings()
if not recordings:
print(" 未找到录音文件")
else:
for i, recording in enumerate(recordings, 1):
print(f" {i}. {recording['filename']}")
print(f" 大小: {recording['size_mb']:.2f} MB")
print(f" 创建时间: {recording['created_time']}")
print(f" 修改时间: {recording['modified_time']}")
print(f" 文件年龄: {recording['age_hours']:.1f} 小时")
print()
return
if args.summary:
print_recording_summary()
return
if args.cleanup_time > 0:
print(f"🧹 按时间清理录音文件(保留 {args.cleanup_time} 小时内的文件)")
cleanup_recordings(retention_hours=args.cleanup_time, dry_run=args.dry_run)
if args.cleanup_count > 0:
print(f"🧹 按数量清理录音文件(保留最新的 {args.cleanup_count} 个文件)")
cleanup_recordings_by_count(max_files=args.cleanup_count, dry_run=args.dry_run)
if not any([args.list, args.summary, args.cleanup_time > 0, args.cleanup_count > 0]):
print("📖 录音文件清理工具")
print("使用 --help 查看帮助信息")
print()
print("常用命令:")
print(" python cleanup_recordings.py --summary # 显示摘要")
print(" python cleanup_recordings.py --list # 列出所有文件")
print(" python cleanup_recordings.py --cleanup-time 24 # 保留24小时内的文件")
print(" python cleanup_recordings.py --cleanup-count 30 # 保留最新的30个文件")
print(" python cleanup_recordings.py -t 24 -c 30 -d # 模拟运行")
if __name__ == "__main__":
main()