媒体处理实用程序的集合。
pressurecooker的Python项目详细描述
压力锅
各种媒体处理功能和实用程序的库
设置您的环境
Install ffmpeg如果你还没有。
Install ImageMagick如果你还没有。(如果运行的是windows,则需要添加指向imagemagick目录的
MAGICK_HOME
环境变量)Install GhostScript如果你还没有。
Install poppler-utils如果您还没有的话
转换标题文件
pressurecooker
库包含用于从几个不同的
格式化为首选的VTT
格式。当前支持的格式包括:
Within
pressurecooker
, the term "captions" and "subtitles" are used interchangeably. All of the classes and functions handling conversion use the "subtitles" term.
语言代码
DFXP
、SAMI
和TTML
格式可以在一个文件中封装多种语言的标题内容。
SCC
、SRT
和VTT
格式通常仅限于在
文件(这个规则的VTT
可能是一个异常,但是我们的转换器没有检测到它的语言)。
因此,当转换这些文件时,我们无法知道我们使用的是什么语言
而必须使用常量LANGUAGE_CODE_UNKNOWN
来提取转换后的字幕。
还要注意,字幕文件中使用的语言代码可能与
在le-utils
中定义的le内部语言代码。
从文件创建转换器
要从本地文件路径创建字幕转换器,请使用以下命令:
frompressurecooker.subtitlesimportbuild_subtitle_converter_from_fileconverter=build_subtitle_converter_from_file('/path/to/file.srt')
从字符串创建转换器
如果已经将标题加载到字符串变量中, 您可以像这样创建转换器:
frompressurecooker.subtitlesimportbuild_subtitle_convertercaptions_str=''# In this example, `captions_str` holds the caption contentsconverter=build_subtitle_converter(captions_str)
转换字幕
对于不包含语言代码信息的SCC
、SRT
和VTT
字幕格式,
在
提取转换字幕的时间:
frompressurecooker.subtitlesimportbuild_subtitle_converter_from_filefrompressurecooker.subtitlesimportLANGUAGE_CODE_UNKNOWNconverter=build_subtitle_converter_from_file('/path/to/file.srt')# Option A: Obtain the contents of the converted VTT file as a stringoutput_str=converter.convert(LANGUAGE_CODE_UNKNOWN)# Option B: Write the converted subtitles to a local pathconverter.write("/path/to/file.vtt",LANGUAGE_CODE_UNKNOWN)
常量是内部表示。
用于表示未知语言代码中的字幕。这将是默认值
只有用于SCC
、SRT
和VTT
字幕转换器的语言代码。
如果你不确定格式,但你知道文件的语言,
用这种语言有条件地替换LANGUAGE_CODE_UNKNOWN
比较安全:
frompressurecooker.subtitlesimportbuild_subtitle_converter_from_filefrompressurecooker.subtitlesimportLANGUAGE_CODE_UNKNOWNconverter=build_subtitle_converter_from_file('/path/to/file')# Replace unknown language code if presentifconverter.has_language(LANGUAGE_CODE_UNKNOWN):converter.replace_unknown_language('en')assertconverter.has_language('en'),'Must have English after replace'output_str=converter.convert('en')
一个演示如何处理诸如DFXP
、SAMI
和TTML
等字幕格式的示例,
有多种语言,如下所示:
frompressurecooker.subtitlesimportbuild_subtitle_converter_from_filefrompressurecooker.subtitlesimportLANGUAGE_CODE_UNKNOWN,InvalidSubtitleLanguageErrorconverter=build_subtitle_converter_from_file('/path/to/file')forlang_codeinconverter.get_language_codes():# `some_logic` would be your decisions on whether to use this languageifsome_logic(lang_code):converter.write("/path/to/file-{}.vtt".format(lang_code),lang_code)eliflang_code==LANGUAGE_CODE_UNKNOWN:raiseInvalidSubtitleLanguageError('Unexpected unknown language')