@@ -28,8 +28,14 @@ def format_timestamp(seconds):
2828def create_srt_file (whisper_results , output_srt_path = "temp_subs.srt" ):
2929 """Parses raw Whisper segments and writes them to a standard SubRip (.srt) file in 3-word chunks."""
3030 with open (output_srt_path , "w" , encoding = "utf-8" ) as f :
31+ segments = whisper_results .get ("segments" , [])
32+ if not segments :
33+ # If no speech is detected, write a dummy blank subtitle to prevent FFmpeg crashes
34+ f .write ("1\n 00:00:00,000 --> 00:00:01,000\n \n \n " )
35+ return
36+
3137 counter = 1
32- for segment in whisper_results . get ( " segments" , []) :
38+ for segment in segments :
3339 words = segment .get ("words" , [])
3440 if not words :
3541 start_str = format_timestamp (segment ["start" ])
@@ -89,8 +95,10 @@ def burn_subtitles_to_video(
8995 """
9096 print ("STAGE:FINALIZING" , flush = True )
9197
92- # Sanitize Windows paths for FFmpeg's internal string parsing engine
93- srt_path_fixed = srt_input .replace ("\\ " , "/" ).replace (":" , "\\ :" )
98+ # FFmpeg subtitles filter on Windows frequently fails to parse absolute paths with drive letters.
99+ # Convert to a relative path and sanitize slashes to bypass the parsing bug safely.
100+ rel_srt = os .path .relpath (srt_input , os .getcwd ())
101+ srt_path_fixed = rel_srt .replace ("\\ " , "/" ).replace (":" , "\\ :" )
94102
95103 # Subtitle styling constraints (Impact font, White base color, shifted into the bottom blank space)
96104 sub_style = "FontName=Impact,Alignment=2,MarginV=80,FontSize=16,PrimaryColour=&H00FFFFFF&,Outline=2,Shadow=1,OutlineColour=&H00000000&"
@@ -100,8 +108,8 @@ def burn_subtitles_to_video(
100108 "[0:v]scale=-1:1920,crop=1080:1920,gblur=sigma=20[bg];"
101109 # 2. Webcam Slicing: Crop facecam dynamically, scale up for visibility
102110 f"[0:v]crop={ facecam_crop } ,scale=900:-1[cam];"
103- # 3. Gameplay Slicing: Center crop (1080x1080 at x=420) to keep crosshair & hotbar perfectly centered
104- "[0:v]crop=1080:1080:420:0 [game];"
111+ # 3. Gameplay Slicing: Center crop to keep crosshair & hotbar perfectly centered
112+ "[0:v]crop=ih:ih:(iw-ih)/2:0,scale=1080:1080 [game];"
105113 # 4. Compositing: Overlay gameplay centrally on background
106114 "[bg][game]overlay=(W-w)/2:(H-h)/2[bg_game];"
107115 # 5. Compositing: Overlay webcam near the top (y=50)
@@ -257,6 +265,74 @@ def detect_facecam(video_path):
257265 return f"{ cam_w } :{ cam_h } :{ final_x } :{ final_y } "
258266
259267
268+ def get_obs_facecam_crop ():
269+ """Reads OBS Studio profiles/scenes to find the exact facecam position."""
270+ import os
271+ import json
272+ try :
273+ appdata = os .getenv ("APPDATA" )
274+ if not appdata : return None
275+
276+ obs_dir = os .path .join (appdata , "obs-studio" )
277+
278+ current_collection = "Untitled"
279+ for ini_file in ["global.ini" , "user.ini" ]:
280+ ini_path = os .path .join (obs_dir , ini_file )
281+ if os .path .exists (ini_path ):
282+ with open (ini_path , "r" , encoding = "utf-8" ) as f :
283+ for line in f :
284+ if line .startswith ("SceneCollection=" ):
285+ current_collection = line .strip ().split ("=" , 1 )[1 ]
286+ break
287+
288+ json_path = os .path .join (obs_dir , "basic" , "scenes" , f"{ current_collection } .json" )
289+ if not os .path .exists (json_path ): return None
290+
291+ with open (json_path , "r" , encoding = "utf-8" ) as f :
292+ data = json .load (f )
293+
294+ current_scene = data .get ("current_program_scene" ) or data .get ("current_scene" )
295+ scene_source = next ((s for s in data .get ("sources" , []) if s .get ("name" ) == current_scene ), None )
296+ if not scene_source : return None
297+
298+ items = scene_source .get ("settings" , {}).get ("items" , [])
299+
300+ cam_item = None
301+ for item in items :
302+ name = item .get ("name" , "" ).lower ()
303+ if "cam" in name or "webcam" in name or "facecam" in name or "video capture device" in name :
304+ cam_item = item
305+ break
306+
307+ if not cam_item :
308+ print ("STAGE:OBS_PROFILE_NO_CAM_FOUND" , flush = True )
309+ return None
310+
311+ pos_x = int (cam_item .get ("pos" , {}).get ("x" , 0 ))
312+ pos_y = int (cam_item .get ("pos" , {}).get ("y" , 0 ))
313+
314+ scale_x = abs (cam_item .get ("scale" , {}).get ("x" , 1.0 ))
315+ scale_y = abs (cam_item .get ("scale" , {}).get ("y" , 1.0 ))
316+
317+ ref_x = cam_item .get ("scale_ref" , {}).get ("x" , 1920.0 )
318+ ref_y = cam_item .get ("scale_ref" , {}).get ("y" , 1080.0 )
319+
320+ crop_left = cam_item .get ("crop_left" , 0 )
321+ crop_right = cam_item .get ("crop_right" , 0 )
322+ crop_top = cam_item .get ("crop_top" , 0 )
323+ crop_bottom = cam_item .get ("crop_bottom" , 0 )
324+
325+ w = int ((ref_x - crop_left - crop_right ) * scale_x )
326+ h = int ((ref_y - crop_top - crop_bottom ) * scale_y )
327+
328+ print (f"STAGE:OBS_PROFILE_CAM_DETECTED:{ w } :{ h } :{ pos_x } :{ pos_y } " , flush = True )
329+ return f"{ w } :{ h } :{ pos_x } :{ pos_y } "
330+
331+ except Exception as e :
332+ print (f"STAGE:OBS_PROFILE_ERROR:{ e } " , flush = True )
333+ return None
334+
335+
260336def process_video_pipeline (input_video_path , facecam_config = "384:216:0:0" ):
261337 """
262338 The main execution pipeline container wrapper that runs transcription,
@@ -291,7 +367,9 @@ def process_video_pipeline(input_video_path, facecam_config="384:216:0:0"):
291367 # Resolve facecam config
292368 final_facecam_config = facecam_config
293369 if facecam_config == "auto" :
294- final_facecam_config = detect_facecam (input_video_path )
370+ final_facecam_config = get_obs_facecam_crop ()
371+ if not final_facecam_config :
372+ final_facecam_config = detect_facecam (input_video_path )
295373
296374 final_video_path = burn_subtitles_to_video (
297375 input_video_path , srt_file_path , final_facecam_config , video_output = output_path
0 commit comments