如何从视频文件中只获取第一个关闭字幕?

2024-05-19 01:45:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要找到硬盘上一些视频文件的开始日期。修改日期或文件名等都帮不了我-真正的开始时间是在隐藏的标题中。在

使用CCExtractor和一些{a2}。。。在

import subprocess
process = subprocess.Popen(['ccextractorwin.exe', 'mycaptions.srt', '-quiet',
        'myvideo.mpg'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = process.communicate()

这将生成一个封闭式字幕.srt文件,我想要的肯定是:

^{pr2}$

但问题是这些视频文件有数百GB,而CCExtractor会生成整个字幕文件。我只需要开始时间,它在第一个条目中。在

在CCExtractor上是否有一个模糊的未记录的选项,或者可能是另一个(免费)工具可以让我只获得第一个条目?在

我能想到的唯一选择是启动CCExtractor,生成一个线程来读取正在生成的标题文件,然后杀死CCExtractor进程和read线程。不错,但我想先看看有没有更好的办法。在


Tags: 文件importa2标题文件名时间条目线程
1条回答
网友
1楼 · 发布于 2024-05-19 01:45:02

不要使用process.communicate(),直到从应用程序中读取所有数据,而是将结果作为流逐行读取。然后,您可以在阅读了尽可能多的内容之后终止底层进程。您还需要使用标志-stdout将输出从ccextractorwin.exe重定向到STDOUT。在

import subprocess
process = subprocess.Popen(
    ['ccextractorwin.exe', '-stdout', '-quiet', 'myvideo.mpg'],
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
all_output = []
while True:
    out_line = process.stdout.readline()
    all_output.append(out_line)  # Add this line to the list of lines to keep
    if out_line == u'\n':  # We've hit an empty line, or whatever else you deem like a good stopping point
        break  # the while loop

# Now, kill the process dead in its tracks.
# This probably isn't great for open file handles, but whatever
process.kill()

这会将SIGKILL发送到应用程序,该应用程序(当然)在windows上的工作方式可能与在Linux或OSX上不同。如果有问题,请参阅此处以获取解决方法:In Python 2.5, how do I kill a subprocess?

希望有帮助。在

相关问题 更多 >

    热门问题