Вопрос

I need to find the start date of some video files on my hard drive. Date modified, or filename, etc won't help me - the true start time is in the closed captions.

Using CCExtractor and some Python Popen...

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

This generates a closed captions .srt file and what I want is definitely there:

1
00:00:00,000 --> 00:00:06,772
4756.9585N, 12905.8976W, 1885   
2013-06-20 16:50:29, Hdg: 54                

2
00:00:06,774 --> 00:00:07,373
2013-06-20 16:50:29, Hdg: 54               
4756.9585N, 12905.8976W, 1883   

...

But the problem is that these video files are hundreds of GB, and CCExtractor generates the entire captions file. All I need is the start time, which is in the first entry.

Is there an obscure undocumented option on CCExtractor or perhaps another (free) tool that will allow me to just get the first entry?

The only alternative I can think of is to start CCExtractor, spawn a thread to read the captions file that's being generated, and then kill the CCExtractor process and the read thread. Not too bad, but I want to see if there's a better way out there first.

Это было полезно?

Решение

Instead of using process.communicate(), which blocks until all data is read from the application, read the result line by line as a stream. Then, you can kill the underlying process after you've read as much as you care to. You'll also need to redirect output from ccextractorwin.exe to STDOUT, using the flag -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()

This sends SIGKILL to the application, which may (of course) work differently on windows than on Linux or OSX. If there's a problem, see here for alternative solutions on killing it: In Python 2.5, how do I kill a subprocess?

Hope that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top