padmafcp/fcp2srt.py

44 lines
1.2 KiB
Python
Raw Normal View History

2021-11-05 10:00:24 +00:00
#!/usr/bin/env python3
2021-11-05 09:50:32 +00:00
from __future__ import division
2018-09-09 12:09:55 +00:00
import lxml
import lxml.etree
import ox
import os
import json
import sys
source = sys.argv[1]
target = '%s.srt' % os.path.splitext(source)[0]
fps = 25
data = []
tree = lxml.etree.parse(source)
2021-11-05 09:50:32 +00:00
for g in tree.xpath('//generatoritem'):
2018-09-09 12:09:55 +00:00
#start/end = relative position of the clip in the parent sequence.
#in/out indicate the portion of the source media file to reference.
_in = int(g.findall('in')[0].text)
2021-11-05 09:50:32 +00:00
_out = int(g.findall('out')[0].text)
2018-09-09 12:09:55 +00:00
_start = int(g.findall('start')[0].text)
_end = int(g.findall('end')[0].text)
2021-11-05 09:50:32 +00:00
effect = g.findall('effect')
assert len(effect) == 1
for parameter in effect[0].findall('parameter'):
if parameter.findall('parameterid')[0].text == 'str':
value = parameter.findall('value')[0].text
2018-09-09 12:09:55 +00:00
2021-11-05 09:50:32 +00:00
if _start == -1 and _end == -1:
_start = _in
_end = _out
if _start == -1:
_start = 0
#print _in, _out, _start, _end, value
value = '\n'.join([v.strip() for v in value.strip().split('\r')])
value = value.replace('\n\n', '<br><br>\n')
data.append({
'in': _start/fps, 'out': (_end-1)/fps, 'value': value
})
2021-11-05 10:00:24 +00:00
with open(target, 'wb') as f:
2018-09-09 12:09:55 +00:00
f.write(ox.srt.encode(data))