basyx commited on
Commit
a412151
·
verified ·
1 Parent(s): 57dc47e

Update utils/render.py

Browse files
Files changed (1) hide show
  1. utils/render.py +50 -109
utils/render.py CHANGED
@@ -1,132 +1,73 @@
1
- import subprocess
2
  import os
 
3
  import uuid
 
4
 
 
 
5
 
6
- # =====================================================
7
- # PUBLIC API (USED BY MAIN.PY)
8
- # =====================================================
9
 
10
- def render_subtitles(video_path: str, srt_data: str) -> str:
 
 
 
 
11
  """
12
- Universal subtitle renderer for all pipeline versions (V1–V7)
13
 
14
  Supports:
15
- - Raw SRT string input (from generate_srt)
16
- - Temporary SRT file generation
17
- - FFmpeg subtitle burn-in
18
-
19
- Returns:
20
- Output video path (mp4)
21
  """
22
 
23
- if not video_path or not os.path.exists(video_path):
24
- raise ValueError("Invalid video path provided")
25
-
26
- # -------------------------------------------------
27
- # CREATE OUTPUT PATH
28
- # -------------------------------------------------
29
- output_path = _build_output_path(video_path)
30
-
31
- # -------------------------------------------------
32
- # CREATE TEMP SRT FILE
33
- # -------------------------------------------------
34
- srt_path = _write_srt_file(video_path, srt_data)
35
-
36
- # -------------------------------------------------
37
- # RUN FFmpeg RENDER
38
- # -------------------------------------------------
39
- success = _run_ffmpeg(video_path, srt_path, output_path)
40
-
41
- # -------------------------------------------------
42
- # FALLBACK SAFETY
43
- # -------------------------------------------------
44
- if not success or not os.path.exists(output_path):
45
- return video_path # safe fallback (prevents pipeline crash)
46
-
47
- return output_path
48
-
49
-
50
- # =====================================================
51
- # OUTPUT PATH BUILDER
52
- # =====================================================
53
-
54
- def _build_output_path(video_path: str) -> str:
55
- base, ext = os.path.splitext(video_path)
56
- return f"{base}_rendered_{uuid.uuid4().hex[:6]}.mp4"
57
-
58
-
59
- # =====================================================
60
- # SRT FILE WRITER
61
- # =====================================================
62
-
63
- def _write_srt_file(video_path: str, srt_data: str) -> str:
64
- """
65
- Writes SRT string into temporary file
66
- """
67
 
68
- if not srt_data:
69
- srt_data = ""
 
70
 
71
- srt_path = video_path.replace(".mp4", ".srt")
72
 
73
  with open(srt_path, "w", encoding="utf-8") as f:
74
- f.write(srt_data)
75
-
76
- return srt_path
77
-
78
 
79
- # =====================================================
80
- # FFmpeg EXECUTION CORE
81
- # =====================================================
82
 
83
- def _run_ffmpeg(video_path: str, srt_path: str, output_path: str) -> bool:
84
- """
85
- Executes subtitle burn-in using FFmpeg
86
- """
87
 
88
- try:
 
 
 
 
 
 
 
89
 
90
- cmd = [
91
- "ffmpeg",
92
- "-y",
93
 
94
- # INPUT
95
- "-i", video_path,
 
 
 
 
96
 
97
- # SUBTITLES FILTER
98
- "-vf", f"subtitles={srt_path}",
 
99
 
100
- # AUDIO COPY (faster, avoids re-encode issues)
101
- "-c:a", "copy",
102
 
103
- # OUTPUT
104
- output_path
105
- ]
106
-
107
- process = subprocess.run(
108
- cmd,
109
- stdout=subprocess.PIPE,
110
- stderr=subprocess.PIPE
111
- )
112
-
113
- # FFmpeg success check
114
- return process.returncode == 0
115
-
116
- except Exception:
117
- return False
118
-
119
-
120
- # =====================================================
121
- # OPTIONAL: SAFE RENDER (NO CRASH VERSION)
122
- # =====================================================
123
-
124
- def safe_render_subtitles(video_path: str, srt_data: str) -> str:
125
- """
126
- Crash-proof version for unstable environments (HF Spaces safe)
127
- """
128
 
129
- try:
130
- return render_subtitles(video_path, srt_data)
131
- except Exception:
132
- return video_path
 
 
1
  import os
2
+ import subprocess
3
  import uuid
4
+ from utils.logger import logger
5
 
6
+ OUTPUT_DIR = "jobs"
7
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
8
 
 
 
 
9
 
10
+ def render_subtitles(
11
+ video_path: str,
12
+ srt_text: str,
13
+ output_path: str | None = None,
14
+ ):
15
  """
16
+ Universal subtitle renderer for V7.
17
 
18
  Supports:
19
+ - API render
20
+ - UI render
21
+ - Batch jobs
22
+ - Worker queue
 
 
23
  """
24
 
25
+ if output_path is None:
26
+ output_path = os.path.join(
27
+ OUTPUT_DIR,
28
+ f"{uuid.uuid4()}_render.mp4"
29
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # --------------------------------------------------
32
+ # Write SRT file
33
+ # --------------------------------------------------
34
 
35
+ srt_path = output_path.replace(".mp4", ".srt")
36
 
37
  with open(srt_path, "w", encoding="utf-8") as f:
38
+ f.write(srt_text)
 
 
 
39
 
40
+ logger.info(f"[RENDER] SRT saved → {srt_path}")
 
 
41
 
42
+ # --------------------------------------------------
43
+ # FFmpeg Subtitle Burn
44
+ # --------------------------------------------------
 
45
 
46
+ cmd = [
47
+ "ffmpeg",
48
+ "-y",
49
+ "-i", video_path,
50
+ "-vf", f"subtitles={srt_path}",
51
+ "-c:a", "copy",
52
+ output_path,
53
+ ]
54
 
55
+ logger.info("[RENDER] Running ffmpeg render")
 
 
56
 
57
+ process = subprocess.run(
58
+ cmd,
59
+ stdout=subprocess.PIPE,
60
+ stderr=subprocess.PIPE,
61
+ text=True
62
+ )
63
 
64
+ if process.returncode != 0:
65
+ logger.error(process.stderr)
66
+ raise Exception("FFmpeg render failed")
67
 
68
+ if not os.path.exists(output_path):
69
+ raise Exception("Rendered file missing")
70
 
71
+ logger.info(f"[RENDER] Output → {output_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ return output_path