Created
February 16, 2026 10:24
-
-
Save Akagi201/cd2fef330e585479c98ce85606025a00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def run_ffmpeg_video_process( | |
| input_path: str, | |
| output_path: str, | |
| srt_path: str | None = None, | |
| trim_seconds: float = 3.0, | |
| ) -> bool: | |
| """Process video with FFmpeg: trim, overlay text, and burn subtitles. | |
| Args: | |
| input_path: Path to the input video file. | |
| output_path: Path where the output video should be saved. | |
| srt_path: Optional path to SRT subtitle file to burn in. | |
| trim_seconds: Seconds to trim from end (default: 3.0). | |
| Returns: | |
| True if processing succeeded, False otherwise. | |
| """ | |
| if not shutil.which("ffmpeg"): | |
| print("Error: 'ffmpeg' command not found. Please install FFmpeg.") | |
| return False | |
| # 1. Get duration via ffprobe | |
| try: | |
| cmd_dur = [ | |
| "ffprobe", | |
| "-v", | |
| "error", | |
| "-show_entries", | |
| "format=duration", | |
| "-of", | |
| "default=noprint_wrappers=1:nokey=1", | |
| input_path, | |
| ] | |
| duration_str = subprocess.check_output(cmd_dur).decode().strip() | |
| duration = float(duration_str) | |
| except Exception as e: | |
| print(f"Error probing video duration: {e}") | |
| return False | |
| target_duration = max(0, duration - trim_seconds) | |
| # 2. Build filter chain | |
| filters = [] | |
| # Text overlay (bottom right, black box, white text) | |
| font_info = _find_chinese_font() | |
| font_opt = "" | |
| font_name = "" | |
| if font_info: | |
| font_path, font_name = font_info | |
| escaped_font = _escape_ffmpeg_path(font_path) | |
| font_opt = f":fontfile='{escaped_font}'" | |
| drawtext = ( | |
| f"drawtext=text='{VIDEO_WATERMARK_TEXT}'{font_opt}" | |
| ":fontsize=24:fontcolor=white" | |
| ":box=1:boxcolor=black@0.8:boxborderw=5" | |
| ":x=w-tw-15:y=h-th-33" | |
| ) | |
| filters.append(drawtext) | |
| if srt_path and os.path.exists(srt_path): | |
| escaped_srt = _escape_ffmpeg_path(srt_path) | |
| font_style = f"FontName={font_name}," if font_name else "" | |
| subtitles = ( | |
| f"subtitles='{escaped_srt}'" | |
| f":force_style='{font_style}FontSize=16,MarginV=25,Outline=1,Shadow=0'" | |
| ) | |
| filters.append(subtitles) | |
| filter_complex = ",".join(filters) | |
| # 3. Build FFmpeg command | |
| cmd = [ | |
| "ffmpeg", | |
| "-y", | |
| "-ss", | |
| "0", | |
| "-t", | |
| str(target_duration), | |
| "-i", | |
| input_path, | |
| ] | |
| if filter_complex: | |
| cmd.extend(["-vf", filter_complex]) | |
| cmd.extend( | |
| [ | |
| "-c:v", | |
| "libx264", | |
| "-preset", | |
| "medium", | |
| "-crf", | |
| "23", | |
| "-c:a", | |
| "aac", | |
| "-b:a", | |
| "128k", | |
| output_path, | |
| ] | |
| ) | |
| # 4. Run | |
| print(f"Processing video: {input_path}") | |
| print(f"Duration: {duration:.2f}s → {target_duration:.2f}s") | |
| try: | |
| subprocess.run(cmd, check=True) | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"FFmpeg failed: {e}") | |
| return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment