Skip to content

Instantly share code, notes, and snippets.

@heuripedes
Created April 30, 2020 10:08
Show Gist options
  • Save heuripedes/904636259bc301963287d4c7f315a64c to your computer and use it in GitHub Desktop.
Save heuripedes/904636259bc301963287d4c7f315a64c to your computer and use it in GitHub Desktop.
import subprocess
import xml.etree.ElementTree as ET
import cairocffi
import pangocffi
import pangocairocffi
import sys
import os
SLIDES_INPUT_FILE="temp/slides_mp4_input.txt"
slides = list()
def compile_slides():
tree = ET.parse('./shapes.svg')
root = tree.getroot()
slides = list()
total_duration = 0
with open(SLIDES_INPUT_FILE, "w") as f:
for x in root.findall('w00:image', {'w00': 'http://www.w3.org/2000/svg', 'w99': 'http://www.w3.org/1999/xlink'}):
slide = {
'start': float(x.attrib["in"]),
'end': float(x.attrib["out"]),
'file': x.attrib["{http://www.w3.org/1999/xlink}href"],
'duration': None
}
duration = slide['duration'] = int(slide['end'] - slide['start'])
total_duration += duration
slides.append(slide)
print(f"file '{slide['file']}'", file=f)
print(f"duration {slide['duration']}", file=f)
return slides, total_duration
def compile_messages():
tree = ET.parse('./slides_new.xml')
root = tree.getroot()
messages = []
markup = ""
for x in root.findall('chattimeline', {'w00': 'http://www.w3.org/2000/svg', 'w99': 'http://www.w3.org/1999/xlink'}):
message = {
'timestamp': int(x.attrib["in"]),
'name': x.attrib["name"],
'text': x.attrib["message"]
}
messages.append(message)
markup += f"<b>{message['name']}:</b> {message['text']}\r\n"
return messages
slides, total_dur = compile_slides()
messages = compile_messages()
surf = cairocffi.ImageSurface(cairocffi.FORMAT_ARGB32, 640, 480)
context = cairocffi.Context(surf)
#pangocairo_context = pangocairocffi.Context(context)
#pangocairo_context.set_antialias(cairocffi.ANTIALIAS_SUBPIXEL)
layout = pangocairocffi.create_layout(context)
#fontname = "Sans"
#font = pangocffi.FontDescription(fontname + " 12")
#layout.set_font_description(font)
#pt_per_mm = 72 / 25.4
#layout.set_width(pangocffi.units_from_double(640 * pt_per_mm))
layout.set_width(pangocffi.units_from_double(640))
#layout.set_wrap(pangocffi.PANGO_WRAP_WORD)
def format_msg(msg):
return f"<b>{msg['name']}</b>: {msg['text']}\r\n"
last_timestamp = 0
markup = ""
max_msg = 20
chat_files = []
for i in range(0, len(messages)):
cur_timestamp = messages[i]['timestamp']
if last_timestamp != cur_timestamp:
context.rectangle(0, 0, 640, 480)
context.set_source_rgb(1, 1, 1)
context.fill()
context.set_source_rgb(0, 0, 0)
layout.set_markup(markup)
height = pangocffi.units_to_double(layout.get_extents()[0].height)
context.move_to(2, 480 - height)
pangocairocffi.show_layout(context, layout)
filename = f"temp/{i}.png"
chat_files.append((filename, cur_timestamp - last_timestamp))
with open(filename, "wb") as image_file:
surf.write_to_png(image_file)
#markup = ""
#j = max(0, i - 30)
#while j < i:
# markup += format_msg(messages[j])
# j += 1
markup += format_msg(messages[i])
#layout.set_markup(format_msg(messages[i]))
#height = pangocffi.units_to_double(layout.get_extents()[0].height)
#context.move_to(2, 480 - height)
#pangocairocffi.show_layout(context, layout)
last_timestamp = cur_timestamp
#max_msg -= 1
#if max_msg == 0:
# break
#pangocairo_context.update_layout(layout)
#pangocairo_context.show_layout(layout)
with open("temp/chat_mp4_inputs.txt", "w") as f:
for filename, duration in chat_files:
print(f"file '{filename}'", file=f)
print(f"duration {duration}", file=f)
#slides_cmd = ["ffmpeg", "-f", "concat", "-i", SLIDES_INPUT_FILE, "-vf", "scale=w=1280:h=720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2", "-preset", "ultrafast",
# "-tune", "stillimage", "-pix_fmt", "yuv420p", "slides.mp4"]
#chat_cmd = ..\ffmpeg\bin\ffmpeg.exe -f concat -i temp\chat_mp4_inputs.txt -preset ultrafast -tune stillimage -pix_fmt yuv420p chat.mp4
#..\ffmpeg\bin\ffmpeg.exe -i slides.mp4 -i chat.mp4 -i video\webcams.webm -filter_complex "[0:v]pad=1920:1080:0:(oh-ih)/2[slides];[slides][1:v]overlay=x=1280:y=480[slidechat];[slidechat][2:v]overlay=x=1280:y=0[out]" -map "[out]" -map 2:a -c:a aac -c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p -t 30 conferencia.mp4
#conference_cmd = ["ffmpeg", "-i", "slides.mp4", "-i", "video\webcams.webm",
# "-filter_complex", "[0:v]pad=1920:1080:x=0:y=-1[padded];[padded][1:v]overlay=x=1280[overlaid];[overlaid][2:v]overlay=x=1280:y=480",
# "-map", "[out]", "-map", "1:a", "-preset", "ultrafast", "-movflags", "+faststart", "-c:a", "copy", "conferencia.mp4"]
#-filter_complex "[0:v]pad=1920:1080:x=0:y=-1[padded];[padded][1:v]overlay=x=1280[out]" -map "[out]" -map 1:a -preset ultrafast -t 60 conferencia.mp4
#print(*cmd)
#print('total_dur', total_dur)
#p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
#p.communicate(image_list.encode())
#p.wait()
#subprocess.run(cmd, stdin=image_list, universal_newlines=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment