Skip to content

Commit

Permalink
Premiere: Additional tracks no longer need external wavs
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Feb 14, 2024
1 parent 8a943f7 commit 234ecdf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 127 deletions.
2 changes: 1 addition & 1 deletion auto_editor/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def edit_media(
if export["export"] == "premiere":
from auto_editor.formats.fcp7 import fcp7_write_xml

fcp7_write_xml(export["name"], ffmpeg, output, tl, log)
fcp7_write_xml(export["name"], output, tl, log)
return

if export["export"] in ("final-cut-pro", "resolve"):
Expand Down
190 changes: 64 additions & 126 deletions auto_editor/formats/fcp7.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from auto_editor.ffwrapper import FFmpeg, FileInfo, initFileInfo
from auto_editor.timeline import ASpace, TlAudio, TlVideo, VSpace, v3

from .utils import Validator, make_tracks_dir, show
from .utils import Validator, show

if TYPE_CHECKING:
from auto_editor.utils.log import Log
Expand All @@ -25,9 +25,6 @@
come back the way they started.
"""

PIXEL_ASPECT_RATIO = "square"
COLORDEPTH = "24"
ANAMORPHIC = "FALSE"
DEPTH = "16"


Expand Down Expand Up @@ -121,21 +118,6 @@ def read_tb_ntsc(tb: int, ntsc: bool) -> Fraction:
return Fraction(tb)


"""
(define/c (read-tb-ntsc [tb int?] [ntsc bool?] -> frac?)
(if ntsc
(cond
((= tb 24) 24000/1001)
((= tb 30) 30000/1001)
((= tb 60) 60000/1001)
(else (* tb 999/1000))
)
(* tb 1/1)
)
)
"""


def speedup(speed: float) -> Element:
fil = Element("filter")
effect = ET.SubElement(fil, "effect")
Expand Down Expand Up @@ -164,28 +146,6 @@ def speedup(speed: float) -> Element:
return fil


"""
(define/c (speedup [speed float?] -> element?)
(xml (filter (effect
(name "Time Remap")
(effectid "timeremap")
(parameter #:authoringApp "PremierePro"
(parameterid "variablespeed") (name "variablespeed")
(valuemin "0") (valuemax "1") (value "0")
)
(parameter #:authoringApp "PremierePro"
(parameterid "speed") (name "speed")
(valuemin "-100000") (valuemax "100000") (value (number->string speed))
)
(parameter #:authoringApp "PremierePro"
(parameterid "frameblending") (name "frameblending")
(value "FALSE")
)
)))
)
"""


SUPPORTED_EFFECTS = ("timeremap",)


Expand Down Expand Up @@ -403,47 +363,32 @@ def media_def(
ET.SubElement(rate, "ntsc").text = ntsc
ET.SubElement(vschar, "width").text = f"{tl.res[0]}"
ET.SubElement(vschar, "height").text = f"{tl.res[1]}"
ET.SubElement(vschar, "anamorphic").text = ANAMORPHIC
ET.SubElement(vschar, "pixelaspectratio").text = PIXEL_ASPECT_RATIO
ET.SubElement(vschar, "anamorphic").text = "FALSE"
ET.SubElement(vschar, "pixelaspectratio").text = "square"

if len(src.audios) > 0:
for aud in src.audios:
audiodef = ET.SubElement(mediadef, "audio")
aschar = ET.SubElement(audiodef, "samplecharacteristics")
ET.SubElement(aschar, "depth").text = DEPTH
ET.SubElement(aschar, "samplerate").text = f"{tl.sr}"
ET.SubElement(audiodef, "channelcount").text = f"{src.audios[0].channels}"
ET.SubElement(audiodef, "channelcount").text = f"{aud.channels}"


def fcp7_write_xml(name: str, ffmpeg: FFmpeg, output: str, tl: v3, log: Log) -> None:
def fcp7_write_xml(name: str, output: str, tl: v3, log: Log) -> None:
width, height = tl.res
timebase, ntsc = set_tb_ntsc(tl.tb)

src_to_url: dict[tuple[FileInfo, int], str] = {}
src_to_id: dict[tuple[FileInfo, int], str] = {}
src_to_src: dict[tuple[FileInfo, int], FileInfo] = {}
src_to_url: dict[FileInfo, str] = {}
src_to_id: dict[FileInfo, str] = {}

file_defs: set[str] = set() # Contains urls

for src in set(tl.sources):
path_resolve = f"{src.path.resolve()}"
the_id = f"file-{len(src_to_id)+1}"
src_to_url[src] = f"{src.path.resolve()}"
src_to_id[src] = the_id

src_to_url[(src, 0)] = path_resolve
src_to_id[(src, 0)] = the_id

if len(src.audios) > 1:
fold = make_tracks_dir(src)
for i in range(1, len(src.audios)):
newtrack = fold / f"{i}.wav"

ffmpeg.run(["-i", path_resolve, "-map", f"0:a:{i}", f"{newtrack}"])

new_src = initFileInfo(f"{newtrack}", ffmpeg, log)
src_to_url[(src, i)] = f"{newtrack.resolve()}"
src_to_id[(src, i)] = f"file-{len(src_to_id)+1}"
src_to_src[(src, i)] = new_src

xmeml = ET.Element("xmeml", version="4")
xmeml = ET.Element("xmeml", version="5")
sequence = ET.SubElement(xmeml, "sequence")
ET.SubElement(sequence, "name").text = name
ET.SubElement(sequence, "duration").text = f"{int(tl.out_len())}"
Expand All @@ -458,11 +403,9 @@ def fcp7_write_xml(name: str, ffmpeg: FFmpeg, output: str, tl: v3, log: Log) ->
rate = ET.SubElement(vschar, "rate")
ET.SubElement(rate, "timebase").text = f"{timebase}"
ET.SubElement(rate, "ntsc").text = ntsc
ET.SubElement(vschar, "fielddominance").text = "none"
ET.SubElement(vschar, "colordepth").text = COLORDEPTH
ET.SubElement(vschar, "width").text = f"{width}"
ET.SubElement(vschar, "height").text = f"{height}"
ET.SubElement(vschar, "pixelaspectratio").text = PIXEL_ASPECT_RATIO
ET.SubElement(vschar, "pixelaspectratio").text = "square"

if len(tl.v) > 0 and len(tl.v[0]) > 0:
track = ET.SubElement(video, "track")
Expand All @@ -476,25 +419,24 @@ def fcp7_write_xml(name: str, ffmpeg: FFmpeg, output: str, tl: v3, log: Log) ->
_out = f"{int(clip.offset / clip.speed) + clip.dur}"

clipitem = ET.SubElement(track, "clipitem", id=f"clipitem-{j+1}")
ET.SubElement(clipitem, "masterclipid").text = "masterclip-2"
ET.SubElement(clipitem, "name").text = src.path.stem
ET.SubElement(clipitem, "start").text = _start
ET.SubElement(clipitem, "end").text = _end
ET.SubElement(clipitem, "in").text = _in
ET.SubElement(clipitem, "out").text = _out

_id = src_to_id[(clip.src, 0)]
_id = src_to_id[clip.src]
filedef = ET.SubElement(clipitem, "file", id=_id)

pathurl = src_to_url[(clip.src, 0)]
pathurl = src_to_url[clip.src]
if pathurl not in file_defs:
media_def(filedef, pathurl, clip.src, tl, timebase, ntsc)
file_defs.add(pathurl)

if clip.speed != 1:
clipitem.append(speedup(clip.speed * 100))

for i in range(len(src.audios) + 1):
for i in range(len(src.audios) * 2 + 1): # `2` because stereo.
link = ET.SubElement(clipitem, "link")
ET.SubElement(
link, "linkclipref"
Expand All @@ -513,65 +455,61 @@ def fcp7_write_xml(name: str, ffmpeg: FFmpeg, output: str, tl: v3, log: Log) ->
ET.SubElement(aschar, "depth").text = DEPTH
ET.SubElement(aschar, "samplerate").text = str(tl.sr)

for t, aclips in enumerate(tl.a):
track = ET.Element(
"track", currentExplodedTrackIndex="0", premiereTrackType="Stereo"
)

for j, aclip in enumerate(aclips):
src = aclip.src

_start = f"{aclip.start}"
_end = f"{aclip.start + aclip.dur}"
_in = f"{int(aclip.offset / aclip.speed)}"
_out = f"{int(aclip.offset / aclip.speed) + aclip.dur}"

if not src.videos:
clip_item_num = j + 1
master_id = "1"
else:
clip_item_num = len(aclips) + 1 + j + (t * len(aclips))
master_id = "2"

clipitem = ET.SubElement(
track,
"clipitem",
id=f"clipitem-{clip_item_num}",
premiereChannelType="stereo",
t = 0
for aclips in tl.a:
for channelcount in range(0, 2): # Because "stereo" is hardcoded.
t += 1
track = ET.Element(
"track",
currentExplodedTrackIndex=f"{channelcount}",
totalExplodedTrackCount="2", # Because "stereo" is hardcoded.
premiereTrackType="Stereo",
)
ET.SubElement(clipitem, "masterclipid").text = f"masterclip-{master_id}"
ET.SubElement(clipitem, "name").text = src.path.stem
ET.SubElement(clipitem, "start").text = _start
ET.SubElement(clipitem, "end").text = _end
ET.SubElement(clipitem, "in").text = _in
ET.SubElement(clipitem, "out").text = _out

_id = src_to_id[(aclip.src, aclip.stream)]
pathurl = src_to_url[(aclip.src, aclip.stream)]

if aclip.stream > 0:
my_src = src_to_src[(aclip.src, aclip.stream)]
else:
my_src = aclip.src

filedef = ET.SubElement(clipitem, "file", id=_id)
if pathurl not in file_defs:
media_def(filedef, pathurl, my_src, tl, timebase, ntsc)
file_defs.add(pathurl)
if src.videos:
ET.SubElement(track, "outputchannelindex").text = f"{channelcount + 1}"

sourcetrack = ET.SubElement(clipitem, "sourcetrack")
ET.SubElement(sourcetrack, "mediatype").text = "audio"
ET.SubElement(sourcetrack, "trackindex").text = "1"
labels = ET.SubElement(clipitem, "labels")
ET.SubElement(labels, "label2").text = "Iris"
for j, aclip in enumerate(aclips):
src = aclip.src

if aclip.speed != 1:
clipitem.append(speedup(aclip.speed * 100))
_start = f"{aclip.start}"
_end = f"{aclip.start + aclip.dur}"
_in = f"{int(aclip.offset / aclip.speed)}"
_out = f"{int(aclip.offset / aclip.speed) + aclip.dur}"

if src.videos:
ET.SubElement(clipitem, "outputchannelindex").text = "1"
if not src.videos:
clip_item_num = j + 1
else:
clip_item_num = len(aclips) + 1 + j + (t * len(aclips))

audio.append(track)
clipitem = ET.SubElement(
track,
"clipitem",
id=f"clipitem-{clip_item_num}",
premiereChannelType="stereo",
)
ET.SubElement(clipitem, "name").text = src.path.stem
ET.SubElement(clipitem, "start").text = _start
ET.SubElement(clipitem, "end").text = _end
ET.SubElement(clipitem, "in").text = _in
ET.SubElement(clipitem, "out").text = _out

pathurl = src_to_url[aclip.src]
filedef = ET.SubElement(clipitem, "file", id=src_to_id[aclip.src])
if pathurl not in file_defs:
media_def(filedef, pathurl, aclip.src, tl, timebase, ntsc)
file_defs.add(pathurl)

sourcetrack = ET.SubElement(clipitem, "sourcetrack")
ET.SubElement(sourcetrack, "mediatype").text = "audio"
ET.SubElement(sourcetrack, "trackindex").text = f"{t + 1}"
labels = ET.SubElement(clipitem, "labels")
ET.SubElement(labels, "label2").text = "Iris"

if aclip.speed != 1:
clipitem.append(speedup(aclip.speed * 100))

audio.append(track)

tree = ET.ElementTree(xmeml)
ET.indent(tree, space="\t", level=0)
Expand Down

0 comments on commit 234ecdf

Please sign in to comment.