You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AutoSub与Python处理SRT字幕:移除空字幕条目报错解决

解决SubtitlesClip空字幕条目报错问题

问题根源

你遇到的TypeError: cannot unpack non-iterable NoneType object,是因为autosub生成的SRT文件包含无文本内容的空字幕条目,SubtitlesClip解析这类条目时无法处理,直接触发报错。手动删除空条目后恢复正常,说明只需自动化过滤这类无效条目即可解决问题。

自动化清理SRT空条目代码

在调用SubtitlesClip之前,加入以下函数处理SRT文件:

def clean_empty_subtitles(srt_path):
    # 读取原SRT文件内容
    with open(srt_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
    cleaned_lines = []
    current_entry = []
    
    for line in lines:
        stripped_line = line.strip()
        current_entry.append(line)
        
        # SRT条目以空行分隔,此时检查当前条目是否有有效文本
        if stripped_line == '':
            # 判断条目内是否存在非序号、非时间轴的有效文本
            has_valid_text = any(
                len(line.strip()) > 0 
                and not line.strip().isdigit() 
                and '-->' not in line 
                for line in current_entry
            )
            if has_valid_text:
                cleaned_lines.extend(current_entry)
            current_entry = []
    
    # 处理文件末尾无空行的最后一个条目
    if current_entry:
        has_valid_text = any(
            len(line.strip()) > 0 
            and not line.strip().isdigit() 
            and '-->' not in line 
            for line in current_entry
        )
        if has_valid_text:
            cleaned_lines.extend(current_entry)
    
    # 写入清理后的内容
    with open(srt_path, 'w', encoding='utf-8') as f:
        f.writelines(cleaned_lines)

整合到你的代码中

把上述函数放在os.system(command)之后、SubtitlesClip之前,位置如下:

command = r'C:\Users\tript\Downloads\autosub-0.5.7-alpha-win-x64-pyinstaller\autosub_pyinstaller\autosub.exe -i "c:\Users\tript\OneDrive\Documents\Coding Projects\Auto_YT_Shorts_Gen\voiceover.mp3" -o "c:\Users\tript\OneDrive\Documents\Coding Projects\Auto_YT_Shorts_Gen" -S en -y'
os.system(command)

# 新增:自动清理空字幕条目
def clean_empty_subtitles(srt_path):
    with open(srt_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
    cleaned_lines = []
    current_entry = []
    
    for line in lines:
        stripped_line = line.strip()
        current_entry.append(line)
        
        if stripped_line == '':
            has_valid_text = any(
                len(line.strip()) > 0 
                and not line.strip().isdigit() 
                and '-->' not in line 
                for line in current_entry
            )
            if has_valid_text:
                cleaned_lines.extend(current_entry)
            current_entry = []
    
    if current_entry:
        has_valid_text = any(
            len(line.strip()) > 0 
            and not line.strip().isdigit() 
            and '-->' not in line 
            for line in current_entry
        )
        if has_valid_text:
            cleaned_lines.extend(current_entry)
    
    with open(srt_path, 'w', encoding='utf-8') as f:
        f.writelines(cleaned_lines)

clean_empty_subtitles("voiceover.en.srt")

# 注意:调整my_clip加载顺序,解决size引用报错问题
my_clip = VideoFileClip("sample.mp4")
# 修正size的写法,元组不能直接减数字,需分别处理宽高
generator_output = lambda txt: TextClip(txt, font='Arial', fontsize=72, color='white', method="subtitle", size=(my_clip.size[0]-100, my_clip.size[1]-100))

sub = SubtitlesClip("voiceover.en.srt", generator_output)
# ... 后续代码保持不变

额外修正提示

你原代码中generator_output用到my_clip.size时,my_clip还未定义,会触发新的报错,同时size=my_clip.size-100的写法错误(元组无法直接减数字),上面的代码已经同步修正了这两个问题。

内容的提问来源于stack exchange,提问作者Triptox123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:00:21