78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
Generate caption files from word-level timestamps.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import json
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
# Add backend to path
|
||
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
||
|
|
|
||
|
|
from services.caption_generator import generate_srt, generate_vtt, generate_ass, save_captions
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
if len(sys.argv) < 2:
|
||
|
|
print("Usage: python caption_generator.py <command> [args...]", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
command = sys.argv[1]
|
||
|
|
|
||
|
|
try:
|
||
|
|
if command == "generate_srt":
|
||
|
|
if len(sys.argv) < 4:
|
||
|
|
print("Usage: python caption_generator.py generate_srt <words_json> [deleted_indices_json] [words_per_line]", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
words = json.loads(sys.argv[2])
|
||
|
|
deleted_indices = set(json.loads(sys.argv[3])) if len(sys.argv) > 3 and sys.argv[3] != "null" else None
|
||
|
|
words_per_line = int(sys.argv[4]) if len(sys.argv) > 4 else 8
|
||
|
|
|
||
|
|
result = generate_srt(words, deleted_indices, words_per_line)
|
||
|
|
print(json.dumps({"content": result}))
|
||
|
|
|
||
|
|
elif command == "generate_vtt":
|
||
|
|
if len(sys.argv) < 4:
|
||
|
|
print("Usage: python caption_generator.py generate_vtt <words_json> [deleted_indices_json] [words_per_line]", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
words = json.loads(sys.argv[2])
|
||
|
|
deleted_indices = set(json.loads(sys.argv[3])) if len(sys.argv) > 3 and sys.argv[3] != "null" else None
|
||
|
|
words_per_line = int(sys.argv[4]) if len(sys.argv) > 4 else 8
|
||
|
|
|
||
|
|
result = generate_vtt(words, deleted_indices, words_per_line)
|
||
|
|
print(json.dumps({"content": result}))
|
||
|
|
|
||
|
|
elif command == "generate_ass":
|
||
|
|
if len(sys.argv) < 4:
|
||
|
|
print("Usage: python caption_generator.py generate_ass <words_json> [deleted_indices_json] [words_per_line] [style_json]", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
words = json.loads(sys.argv[2])
|
||
|
|
deleted_indices = set(json.loads(sys.argv[3])) if len(sys.argv) > 3 and sys.argv[3] != "null" else None
|
||
|
|
words_per_line = int(sys.argv[4]) if len(sys.argv) > 4 else 8
|
||
|
|
style = json.loads(sys.argv[5]) if len(sys.argv) > 5 and sys.argv[5] != "null" else None
|
||
|
|
|
||
|
|
result = generate_ass(words, deleted_indices, words_per_line, style)
|
||
|
|
print(json.dumps({"content": result}))
|
||
|
|
|
||
|
|
elif command == "save_captions":
|
||
|
|
if len(sys.argv) != 4:
|
||
|
|
print("Usage: python caption_generator.py save_captions <content> <output_path>", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
content = sys.argv[2]
|
||
|
|
output_path = sys.argv[3]
|
||
|
|
|
||
|
|
result = save_captions(content, output_path)
|
||
|
|
print(json.dumps({"output_path": result}))
|
||
|
|
|
||
|
|
else:
|
||
|
|
print(f"Unknown command: {command}", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(json.dumps({"error": str(e)}), file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|