Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added subtitles cannot be fully displayed #2260

Open
stonemason10 opened this issue Nov 26, 2024 · 6 comments
Open

added subtitles cannot be fully displayed #2260

stonemason10 opened this issue Nov 26, 2024 · 6 comments
Labels
bug Issues that report (apparent) bugs. v2.x Issues based on MoviePy version 2.0 and upwards

Comments

@stonemason10
Copy link

Expected Behavior

Add subtitles normally

Actual Behavior

After upgrading to version 2.1.1 of Moviepy, it is not possible to wrap subtitles when adding them. Multiple subtitles can only display one line, and any excess will be truncated.

Steps to Reproduce the Problem

Specifications

  • Python Version: 3.11
  • MoviePy Version: 2.1.1
  • Platform Name: windows
  • Platform Version: windows10
@stonemason10 stonemason10 added the bug Issues that report (apparent) bugs. label Nov 26, 2024
@Implosiv3
Copy link

Could you please provide some code (the code you used and failed is ok) to be able to check and investigate about that? I've not used the subtitles functionality yet and I could try with your code. Thank you.

@stonemason10
Copy link
Author

Could you please provide some code (the code you used and failed is ok) to be able to check and investigate about that? I've not used the subtitles functionality yet and I could try with your code. Thank you.

def add_subtitles_to_video_textclip(video_clip, srt_path, subtitle_config, video_path):
"""Add subtitles to video using moviepy's TextClip"""
video_filename = os.path.basename(video_path)
logging.info(f"Adding subtitles to video using moviepy's TextClip: {video_filename}")
try:
subtitles = pysrt.open(srt_path)
subtitle_clips = []

    video_width, video_height = video_clip.w, video_clip.h
    subtitle_width = int(video_width * subtitle_config['width_ratio'])
    
    for subtitle in subtitles:
        start_time = subtitle.start.ordinal / 1000.0
        end_time = subtitle.end.ordinal / 1000.0
        text = subtitle.text
        
        # Create TextClip, set size to wrap text automatically
        text_clip = TextClip(
            text=text,
            font=subtitle_config['font_path'],
            font_size=subtitle_config['font_size'],
            color=subtitle_config['color'],
            size=(subtitle_width, None),
            method="caption",
            stroke_width=subtitle_config['stroke_width'],
            stroke_color=subtitle_config['stroke_color'],
            bg_color=None,
            transparent=True,
            interline=subtitle_config['interline'],
            text_align="center",
            horizontal_align="center",
        )
        
        # Get the actual height of the subtitle
        subtitle_height = text_clip.h 
        
        # Calculate available height
        margin_pixels = video_height * subtitle_config['margin_bottom']
        available_height = video_height - margin_pixels
        
        # If subtitle height exceeds available height, adjust position upwards
        if subtitle_height > available_height:
            y_position = video_height - margin_pixels - subtitle_height
            if y_position < 0:
                y_position = 0
        else:
            y_position = video_height - margin_pixels - subtitle_height
            if y_position < 0:
                y_position = 0
        
        # Set subtitle position
        text_clip = text_clip.with_position(('center', y_position))
        
        # Set subtitle duration
        text_clip = text_clip.with_duration(end_time - start_time).with_start(start_time)
        subtitle_clips.append(text_clip)
    
    # Combine video and subtitles, with subtitles on top
    final_clip = CompositeVideoClip([video_clip] + subtitle_clips)
    return final_clip
except Exception as e:
    logging.error(f"An error occurred while adding subtitles to video {video_filename}: {e}")
    traceback.print_exc()
    return video_clip

@stonemason10
Copy link
Author

subtitle_config:
font_size: 72
font_weight: "bold"
color: "#FFFF00"
font: "huangkaihuaLawyerfont-2.ttf" #SimHei "KaiTi"
font_path: "E:\ai\font\huangkaihuaLawyerfont-2.ttf"
margin_bottom: 0.25
width_ratio: 0.8
stroke_width: 1
stroke_color: "#000000"
bg_color: None
transparent: True
interline: 4

@stonemason10
Copy link
Author

@keikoro keikoro added the v2.x Issues based on MoviePy version 2.0 and upwards label Dec 8, 2024
@GreenJeff1
Copy link

I encountered the same issue. Did you find a solution?

@stonemason10
Copy link
Author

I have temporarily downgraded the version to 1.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues that report (apparent) bugs. v2.x Issues based on MoviePy version 2.0 and upwards
Projects
None yet
Development

No branches or pull requests

4 participants