[ create a new paste ] login | about

Link: http://codepad.org/CA30FHUm    [ raw code | output | fork ]

Python, pasted on Mar 28:
import bpy
import os


"""
export_edl.py -- Export of Edit Description List ("EDL") files.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

# TimeCode class by Campbell Barton
class TimeCode:
    """
    Simple timecode class
    also supports conversion from other time strings used by EDL
    """
    __slots__ = (
        "fps",
        "hours",
        "minutes",
        "seconds",
        "frame",
    )

    def __init__(self, data, fps):
        self.fps = fps
        if type(data) == str:
            self.from_string(data)
            frame = self.as_frame()
            self.from_frame(frame)
        else:
            self.from_frame(data)

    def from_string(self, text):
        # hh:mm:ss:ff
        # No dropframe support yet

        if text.lower().endswith("mps"):  # 5.2mps
            return self.from_frame(int(float(text[:-3]) * self.fps))
        elif text.lower().endswith("s"):  # 5.2s
            return self.from_frame(int(float(text[:-1]) * self.fps))
        elif text.isdigit():  # 1234
            return self.from_frame(int(text))
        elif ":" in text:  # hh:mm:ss:ff
            text = text.replace(";", ":").replace(",", ":").replace(".", ":")
            text = text.split(":")

            self.hours = int(text[0])
            self.minutes = int(text[1])
            self.seconds = int(text[2])
            self.frame = int(text[3])
            return self
        else:
            print("ERROR: could not convert this into timecode %r" % text)
            return self

    def from_frame(self, frame):

        if frame < 0:
            frame = -frame
            neg = True
        else:
            neg = False

        fpm = 60 * self.fps
        fph = 60 * fpm

        if frame < fph:
            self.hours = 0
        else:
            self.hours = int(frame / fph)
            frame = frame % fph

        if frame < fpm:
            self.minutes = 0
        else:
            self.minutes = int(frame / fpm)
            frame = frame % fpm

        if frame < self.fps:
            self.seconds = 0
        else:
            self.seconds = int(frame / self.fps)
            frame = frame % self.fps

        self.frame = frame

        if neg:
            self.frame = -self.frame
            self.seconds = -self.seconds
            self.minutes = -self.minutes
            self.hours = -self.hours

        return self

    def as_frame(self):
        abs_frame = self.frame
        abs_frame += self.seconds * self.fps
        abs_frame += self.minutes * 60 * self.fps
        abs_frame += self.hours * 60 * 60 * self.fps

        return abs_frame

    def as_string(self):
        self.from_frame(int(self))
        return "%.2d:%.2d:%.2d:%.2d" % (self.hours, self.minutes, self.seconds, self.frame)

    def __repr__(self):
        return self.as_string()

    # Numeric stuff, may as well have this
    def __neg__(self):
        return TimeCode(-int(self), self.fps)

    def __int__(self):
        return self.as_frame()

    def __sub__(self, other):
        return TimeCode(int(self) - int(other), self.fps)

    def __add__(self, other):
        return TimeCode(int(self) + int(other), self.fps)

    def __mul__(self, other):
        return TimeCode(int(self) * int(other), self.fps)

    def __div__(self, other):
        return TimeCode(int(self) // int(other), self.fps)

    def __abs__(self):
        return TimeCode(abs(int(self)), self.fps)

    def __iadd__(self, other):
        return self.from_frame(int(self) + int(other))

    def __imul__(self, other):
        return self.from_frame(int(self) * int(other))

    def __idiv__(self, other):
        return self.from_frame(int(self) // int(other))
# end timecode

#EDLBlock Copyright (C) 2015 William R. Zwicky <wrzwicky@pobox.com>
class EDLBlock:
    def __init__(self):
        self.id = 0
        """Num, 3 digits, officially 001-999. Non-num makes row a comment."""
        self.reel = None
        """Reference to media file or tape.
           Officially: 4-digit num, optional B; or BL for black,
           or AX for aux source.
           Unofficially: any string."""
        self.channels = None
        """A=audio1,V=video1,B=A+V,A2=audio2,A2/V=A2+V,AA=A1+A2,AA/V=A1+A2+V"""
        self.transition = None
        """C=cut,
           D=dissolve,
           Wxxx=wipe type xxx,
           KB=key background,
           K=key foreground,
           KO=key foreground mask"""
        self.transDur = None
        """3-digit duration, in frames, or lone F"""
        self.srcIn = None
        """timecode (hh:mm:ss:ff)"""
        self.srcOut = None
        """timecode (hh:mm:ss:ff)"""
        self.recIn = None
        """timecode (hh:mm:ss:ff)"""
        self.recOut = None
        """timecode (hh:mm:ss:ff). Either out-time or duration.
           Ignored on read; clip length is srcOut-srcIn."""
        self.file = None
        """filename and extention, but no path"""  
                
#EDL Class Copyright (C) 2015 William R. Zwicky <wrzwicky@pobox.com>
class EDL(list):
    def __init__(self):
        self.title = None
        self.dropframe = False
        self.reels = {}
        #self.edits = []

    def load(filename):
        pass

    def savePremiere(self):
        # CMX 3600:
        #   111^^222^^3333^^4444^555^666666666666^777777777777^888888888888^999999999999^
        # Old Lightworks converter:
        #   003  E00706EU  V     D    030 00:00:26:29 00:00:32:10 00:00:01:02 00:00:07:13
        #   111^^22222222^3333^4444^555^^66666666666^77777777777^88888888888^99999999999
        # Export from Premiere:
        # 003  AX       AA    C        00:00:00:10 00:02:03:24 00:00:53:25 00:02:57:09
        # * FROM CLIP NAME: Ep6_Sc2 - Elliot tries again with Tiff.mp4
        s=""
        if not not self.title:
            s="TITLE: " + self.title+"\n"
        if self.dropframe:
            s += "FCM: DROP FRAME"+"\n"
        else:
            s += "FCM: NON DROP FRAME"+"\n"
            
        s += "FRAMERATE: "+str(scene.render.fps)+"\n\n"

        for block in self:
            s += "%03d  %-8s %-4s %-4s %03s  %-11s %-11s %-11s %-11s" \
                % (block.id, block.reel, block.channels,
                   block.transition, block.transDur,
                   block.srcIn, block.srcOut, block.recIn, block.recOut)+ "\n"
            if block.file !="":
                s += "* FROM CLIP NAME: " + block.file + "\n\n"
        print(s)
        return(s)


context = bpy.context
scene = context.scene
vse = scene.sequence_editor

edl_fps=scene.render.fps
id_count=1

e = EDL()
e.title = "Test script"
e.dropframe=False

def start(strip):
    return strip.frame_final_start 

def end(strip):
    return strip.frame_final_start + strip.frame_final_duration

def channel(strip):
    return strip.channel

seq_strips = bpy.context.scene.sequence_editor.sequences
strips_by_start = sorted(seq_strips, key=start)
strips_by_start_and_channel = sorted(strips_by_start, key=channel, reverse=True)

for strip in strips_by_start_and_channel:
    
    b = EDLBlock()
    b.id = id_count

    if strip.type in ['MOVIE']:
        reelname = bpy.path.basename(strip.filepath)
        b.file=reelname
        reelname = os.path.splitext(reelname)[0]        
        b.reel = ((reelname+"        ")[0:8])                
        b.channels = "V   "
        b.transition = "C   "
        b.transDur = "   "
        b.srcIn = TimeCode(strip.frame_offset_start,edl_fps)
        b.srcOut = TimeCode(strip.frame_offset_start+strip.frame_final_duration,edl_fps)
        b.recIn = TimeCode(strip.frame_final_start,edl_fps)
        b.recOut = TimeCode(strip.frame_final_end,edl_fps)                
        e.append(b)  
        id_count=id_count+1                  
#    elif strip.type in ['CROSS']:
#        # 1. Clip in the transition        
#        reelname = bpy.path.basename(strip.input_1.filepath)
#        b.file=""#reelname
#        reelname = os.path.splitext(reelname)[0]        
#        b.reel = ((reelname+"        ")[0:8])        
#        b.channels = "V  "         
#        b.transition = "C   "[0:4]
#        b.transDur = "   "                                       
#        b.srcIn = TimeCode(strip.input_1.frame_final_end,edl_fps)
#        b.srcOut = TimeCode(strip.input_1.frame_final_end,edl_fps)#TimeCode(strip.frame_start+strip.frame_final_duration,edl_fps)
#        b.recIn = TimeCode(strip.input_1.frame_final_end,edl_fps)
#        b.recOut = TimeCode(strip.input_1.frame_final_end,edl_fps)#TimeCode(strip.frame_final_end,edl_fps)                
#        e.append(b)        
#        # 2. Clip in the transition
#        b = EDLBlock()
#        #print(id_count)
#        b.id = id_count
#        reelname = bpy.path.basename(strip.input_2.filepath)
#        b.file= bpy.path.basename(strip.input_1.filepath) + "\n* TO CLIP NAME: "+reelname
#        reelname = os.path.splitext(reelname)[0]        
#        b.reel = ((reelname+"        ")[0:8])        
#        b.channels = "V  "         
#        b.transition = "D   "[0:4]  
#        b.transDur = (str(strip.frame_final_end-strip.frame_final_start)+"   ")[0:3]
#        b.srcIn = TimeCode(strip.input_1.frame_offset_start  - strip.input_2.frame_offset_start - strip.frame_final_duration, edl_fps)
#        b.srcOut = TimeCode(strip.input_1.frame_offset_start - strip.input_2.frame_offset_start, edl_fps)
#        b.recIn = TimeCode(strip.frame_final_start,edl_fps)
#        b.recOut = TimeCode(strip.frame_final_end,edl_fps)                
#        e.append(b)
#        id_count=id_count+1                                                 
#    else:
#        reelname = "BL" #bpy.path.basename(strip.filepath)
#        b.file=""#reelname
#        reelname = "BL"#os.path.splitext(reelname)[0]        
#        b.reel = ((reelname+"        ")[0:8])        
#        b.channels = "V   "         
#        b.transition = "C   "[0:4]
#        b.transDur = ""#(str(strip.frame_final_duration)+"   ")[0:3]
#        b.srcIn = TimeCode(strip.frame_offset_start,edl_fps)
#        b.srcOut = TimeCode(strip.frame_offset_start+strip.frame_final_duration,edl_fps)
#        b.recIn = TimeCode(strip.frame_final_start,edl_fps)
#        b.recOut = TimeCode(strip.frame_final_end,edl_fps)                
#        e.append(b)  
        
for strip in strips_by_start_and_channel:
    
    b = EDLBlock()
    b.id = id_count
    #id_count=id_count+1   
    if strip.type in ['SOUND']:
        reelname = bpy.path.basename(strip.sound.filepath)
        b.file=reelname
        reelname = os.path.splitext(reelname)[0]        
        b.reel = ((reelname+"        ")[0:8])        
        b.channels = "A   "
        b.transition = "C   "
        b.transDur = "   "
        b.srcIn = TimeCode(strip.frame_offset_start,edl_fps)
        b.srcOut = TimeCode(strip.frame_offset_start+strip.frame_final_duration,edl_fps)
        b.recIn = TimeCode(strip.frame_final_start,edl_fps)
        b.recOut = TimeCode(strip.frame_final_end,edl_fps)                
        e.append(b)                                      
        id_count=id_count+1

#write to a file
file_handle = open("C:/Users/User/Desktop/EDLBUG/mytest.edl","w")
file_handle.write(e.savePremiere())
file_handle.close()


Output:
1
2
3
4
Traceback (most recent call last):
  Line 1, in <module>
    import bpy
ImportError: No module named bpy


Create a new paste based on this one


Comments: