Skip to content

anbeime/skill / skills/ppt-roadshow-generator/ppt-roadshow-generator

ppt-roadshow-generator

Verified

产品经理与内容创作者在筹备产品发布或公司路演视频时,使用此技能可从文档解析到视频合成全链路自动化,一键生成15-100页带智能配音、精准字幕和转场动画的完整路演视频,将数天的制作周期缩短至几分钟。

scripts/subtitle_generator.py

View on GitHub
#!/usr/bin/env python3
"""
Subtitle Generator - Generate and synchronize subtitles for roadshow video.

This module generates subtitles from roadshow script and synchronizes
them with audio timing.
"""

import json
import re
from pathlib import Path
from typing import Any, Dict, List, Optional


# =============================================================================
# Constants
# =============================================================================

DEFAULT_OUTPUT_PATH = "subtitles.srt"
DEFAULT_WORDS_PER_SECOND = 3.0  # Average speaking rate


# =============================================================================
# Subtitle Generator
# =============================================================================

class SubtitleGenerator:
    """Generator for SRT subtitles from roadshow script."""

    def __init__(
        self,
        brand_style_path: Optional[str] = None,
        words_per_second: float = DEFAULT_WORDS_PER_SECOND
    ):
        """
        Initialize subtitle generator.

        Args:
            brand_style_path: Path to brand style JSON (optional).
            words_per_second: Average speaking rate for timing calculation.
        """
        self.brand_style = {}
        if brand_style_path and Path(brand_style_path).exists():
            with open(brand_style_path, 'r', encoding='utf-8') as f:
                self.brand_style = json.load(f)
            print(f"✓ 已加载品牌风格: {brand_style_path}")

        self.words_per_second = words_per_second

        print(f"字幕生成器已初始化")
        print(f"  语速: {words_per_second} 字/秒")

    def parse_script(self, script_path: str) -> List[Dict[str, Any]]:
        """
        Parse roadshow script into sections.

        Args:
            script_path: Path to script file.

        Returns:
            List of script sections with text and timing info.
        """
        with open(script_path, 'r', encoding='utf-8') as f:
            content = f.read()

        # Simple parsing: split by paragraphs
        paragraphs = [p.strip() for p in content.split('\n\n') if p.strip()]

        sections = []
        start_time = 0.0

        for idx, para in enumerate(paragraphs):
            # Calculate duration based on word count
            word_count = len(re.findall(r'[\w]+', para, re.UNICODE))
            duration = word_count / self.words_per_second

            sections.append({
                'index': idx + 1,
                'text': para,
                'start_time': start_time,
                'end_time': start_time + duration,
                'duration': duration
            })

            start_time += duration

        print(f"✓ 解析了 {len(sections)} 个段落")
        return sections

    def generate_srt(
        self,
        sections: List[Dict[str, Any]]
    ) -> str:
        """
        Generate SRT format subtitles from sections.

        Args:
            sections: List of script sections.

        Returns:
            SRT format subtitle string.
        """
        srt_lines = []

        for section in sections:
            # Format time as SRT timestamp
            start_srt = self._seconds_to_srt_time(section['start_time'])
            end_srt = self._seconds_to_srt_time(section['end_time'])

            # Add subtitle entry
            srt_lines.append(f"{section['index']}")
            srt_lines.append(f"{start_srt} --> {end_srt}")
            srt_lines.append(section['text'])
            srt_lines.append("")  # Empty line between entries

        return '\n'.join(srt_lines)

    def _seconds_to_srt_time(self, seconds: float) -> str:
        """
        Convert seconds to SRT time format.

        Args:
            seconds: Time in seconds.

        Returns:
            SRT time string (HH:MM:SS,mmm).
        """
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        secs = int(seconds % 60)
        millis = int((seconds % 1) * 1000)

        return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"

    def apply_brand_style(
        self,
        text: str
    ) -> str:
        """
        Apply brand style to subtitle text.

        Args:
            text: Original subtitle text.

        Returns:
            Styled text (if brand style is available).
        """
        if not self.brand_style:
            return text

        # Apply font style if specified
        # This is a simple implementation
        # More advanced styling can be added based on brand style
        return text


# =============================================================================
# Main Function
# =============================================================================

def main():
    """Main entry point."""
    import argparse

    parser = argparse.ArgumentParser(
        description='Generate subtitles for roadshow video'
    )
    parser.add_argument(
        '--script', '-s',
        required=True,
        help='Roadshow script file path'
    )
    parser.add_argument(
        '--style-brand', '-b',
        help='Brand style JSON file path'
    )
    parser.add_argument(
        '--output', '-o',
        default=DEFAULT_OUTPUT_PATH,
        help='Output SRT file path'
    )
    parser.add_argument(
        '--words-per-second', '-w',
        type=float,
        default=DEFAULT_WORDS_PER_SECOND,
        help='Average speaking rate (words per second)'
    )

    args = parser.parse_args()

    # Create generator
    generator = SubtitleGenerator(
        brand_style_path=args.style_brand,
        words_per_second=args.words_per_second
    )

    # Parse script
    print(f"解析演讲稿: {args.script}")
    sections = generator.parse_script(args.script)

    # Generate SRT
    print("生成 SRT 字幕...")
    srt_content = generator.generate_srt(sections)

    # Save to file
    output_path = Path(args.output)
    output_path.parent.mkdir(parents=True, exist_ok=True)

    with open(output_path, 'w', encoding='utf-8') as f:
        f.write(srt_content)

    print(f"✓ 字幕文件已保存: {output_path}")
    print(f"  字幕条数: {len(sections)}")
    print(f"  总时长: {sections[-1]['end_time']:.2f} 秒")


if __name__ == '__main__':
    main()
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall