Skip to content

Instantly share code, notes, and snippets.

@sauravtom
Last active March 16, 2018 20:13
Show Gist options
  • Save sauravtom/e688d059a929196f924f4ca132799739 to your computer and use it in GitHub Desktop.
Save sauravtom/e688d059a929196f924f4ca132799739 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import subprocess
#current directory path
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
#width of the ticker (in pixels)
WIDTH = 678
def marquee(script,narration_file_name= None):
if not narration_file_name:
narration_len = 15
else:
#finding the length of the narration
len_cmd = "ffmpeg -i %s/newsmeme_slides/%s 2>&1 | grep Duration | awk '{print $2}' | tr -d ,"%(DIR_PATH,narration_file_name)
pipe = subprocess.Popen(len_cmd, shell=True, stdout=subprocess.PIPE).stdout
narration_len = pipe.read()
narration_len = narration_len.split(":")[-1]
narration_len = int(float(narration_len.strip()))
#making sure the video script is free of any unwanted characters
video_script = video_script.encode('utf8')
video_script = video_script.replace("'","")
video_script = video_script.replace("\n","")
video_script = video_script.encode('ascii',errors='ignore')
video_script = video_script.upper()
video_script = video_script.replace("."," | ")
os.system("mkdir %s/newsmeme_slides/marquee"%(DIR_PATH))
#create a very very long image with the entire script written on it.
os.system("convert -background transparent -fill black -font %s -gravity center -size x22 \
label:'%s' %s/newsmeme_slides/marquee/marquee_text.png"%(font_loc,video_script,DIR_PATH))
#create the placeholder template
#os.system("convert -size %sx22 xc:black %s/newsmeme_slides/marquee/marquee_template.png"%(WIDTH,DIR_PATH))
pixels_per_char = 7.9
frame_width = int(WIDTH)
len_marquee = int(len(video_script)*pixels_per_char)
len_marquee = len_marquee - frame_width/2
time_marquee= narration_len
#crop the looong script image into smaller frames
for i in range(len_marquee + frame_width):
os.system("composite -geometry +%s+0 %s/newsmeme_slides/marquee/marquee_text.png %s/newsmeme_asset/marquee_template.png \
%s/newsmeme_slides/marquee/%s.png"%( frame_width/4-i ,DIR_PATH,DIR_PATH,DIR_PATH,i))
#combine the cropped frames into an animation
os.system("ffmpeg -r %s -i %s/newsmeme_slides/marquee/%%01d.png -c:v libx264 -pix_fmt yuv420p \
%s/newsmeme_slides/marquee/marquee.mp4"%(int(len_marquee/time_marquee),DIR_PATH,DIR_PATH))
#optional
#overlay the marquee on top of your video
os.system('ffmpeg -y -i %s/newsmeme_slides/video.mp4 -i %s/newsmeme_slides/marquee/marquee.mp4 -filter_complex \
"overlay=0:main_h-overlay_h" -codec:a copy %s/newsmeme_slides/video_with_marquee.mp4'%(DIR_PATH,DIR_PATH,DIR_PATH))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment