From 89ef1b1fa2166f4ca736c1f043a5bbba52fce1dd Mon Sep 17 00:00:00 2001 From: yomguy Date: Thu, 13 Dec 2012 21:03:30 +0100 Subject: [PATCH] make a better class --- auto_fade.py | 12 +++++++++--- kdenlive/fade.py | 32 ++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/auto_fade.py b/auto_fade.py index 6a0e639..691c5ff 100644 --- a/auto_fade.py +++ b/auto_fade.py @@ -1,10 +1,16 @@ import sys -from kdenlive.fade import KdenliveAutoFade +from kdenlive.fade import AutoFade +from tempfile import TemporaryFile + if __name__ == '__main__': path_out = sys.argv[-1] path_in = sys.argv[-2] - fade = KdenliveAutoFade() - fade.run(path_in, path_out) + + fade = AutoFade(path_in) + data = fade.run() + f = open(path_out, 'w') + f.write(data) + f.close() diff --git a/kdenlive/fade.py b/kdenlive/fade.py index 82165ef..c1f1411 100644 --- a/kdenlive/fade.py +++ b/kdenlive/fade.py @@ -23,14 +23,22 @@ from xmltodict2 import * -class KdenliveAutoFade(object): +class AutoFade(object): + """ Automatically applies a fade in and a fade out trasitions between each segment of a KdenLive session. + Each video clip needs to be splitted into one video track and an audio one ("Split audio"), + so that an audio fade in/out is also applied. - def __init__(self): - self.n_frames = 2 + MLT files are also supported. + """ + + def __init__(self, path, frames=3): + self.frames = frames + self.path = path + self.session = xmltodict(self.path) def audio_fade_in(self, frame_out): child = {'attributes': {u'id': u'fadeout', - u'in': unicode(int(frame_out)-self.n_frames), + u'in': unicode(int(frame_out)-self.frames), u'out': unicode(frame_out)}, 'children': [{'attributes': {u'name': u'track'}, 'cdata': '0', @@ -65,7 +73,7 @@ class KdenliveAutoFade(object): def audio_fade_out(self, frame_in): child = {'attributes': {u'id': u'fadein', u'in': unicode(frame_in), - u'out': unicode(int(frame_in)+self.n_frames)}, + u'out': unicode(int(frame_in)+self.frames)}, 'children': [{'attributes': {u'name': u'track'}, 'cdata': '0', 'name': 'property'}, @@ -99,7 +107,7 @@ class KdenliveAutoFade(object): def video_fade_in(self, frame_out): child = {'attributes': {u'id': u'fade_to_black', - u'in': unicode(int(frame_out)-self.n_frames), + u'in': unicode(int(frame_out)-self.frames), u'out': unicode(frame_out)}, 'children': [{'attributes': {u'name': u'track'}, 'cdata': '0', @@ -129,7 +137,7 @@ class KdenliveAutoFade(object): def video_fade_out(self, frame_in): child = {'attributes': {u'id': u'fade_from_black', u'in': unicode(frame_in), - u'out': unicode(int(frame_in)+self.n_frames)}, + u'out': unicode(int(frame_in)+self.frames)}, 'children': [{'attributes': {u'name': u'track'}, 'cdata': '0', 'name': 'property'}, @@ -154,12 +162,11 @@ class KdenliveAutoFade(object): return child - def run(self, path_in, path_out): + def run(self): audio_count = 0 video_count = 0 - d = xmltodict(path_in) - for attr in d['children']: + for attr in self.session['children']: if 'playlist' in attr['name'] and 'children' in attr: for att in attr['children']: if 'producer' in att['attributes']: @@ -183,9 +190,6 @@ class KdenliveAutoFade(object): att['children'] = [self.video_fade_out(frame_in)] video_count += 1 - data = dicttoxml(d) - f = open(path_out, 'w') - f.write(data.encode('utf-8')) - f.close() + return dicttoxml(self.session).encode('utf-8') -- 2.39.5