User:Manetta/scripts/python-csv-ffmpeg-2001-triples

From XPUB & Lens-Based wiki

2001, a reassembled version made with triples

1. annotate shots from film in spreadsheat in the form of triples, and export to csv file

triple = subject + predicate + object
example: Dave + is located in + main room

00:00:00.00,00:00:07.00,space,is located in,between stars
00:00:07.00,00:01:08.00,spaceship,is located in,space,#,spaceflight,flying to,jupiter,#,spaceship,performs,spaceflight
00:01:08.00,00:01:19.00,spaceship,is located in,space
00:01:19.00,00:01:31.00,spaceship,is located in,space
00:01:31.00,00:01:47.00,dave,is located in,main room,,dave ,running at,main room,,main room ,is located in ,spaceship
00:01:47.00,00:01:56.00,other three astronauts,is located in,hybernation beds
00:01:56.00,00:02:23.00,dave,is located in,main room,,dave ,running at,main room
00:02:23.00,00:02:30.00,dave,is located in,main room,,dave ,running at,main room

2. create subtitle file from csv file

import os
import csv
import itertools

# SIMPLE SRT:
with open('2001-space-odyssey-simple.srt', 'w') as txt:
	with open('2001-chunks-annotations-01-simple.csv', 'rb') as f:
			reader = csv.reader(f)
			i=1
			for row in reader:
				start = row[0]
				end = row[1]
				x1 = row[2]
				r1 = row[3]
				y1 = row[4]
				x2 = row[6]
				r2 = row[7]
				y2 = row[8]
				x3 = row[10]
				r3 = row[11]
				y3 = row[12]
				x4 = row[14]
				r4 = row[15]
				y4 = row[16]
				x5 = row[18]
				r5 = row[19]
				y5 = row[20]
				x6 = row[22]
				r6 = row[23]
				y6 = row[24]
				x7 = row[26]
				r7 = row[27]
				y7 = row[28]
				x8 = row[30]
				r8 = row[31]
				y8 = row[32]
				x9 = row[34]
				r9 = row[35]
				y9 = row[36]
				a = i
				b = start+" --> "+end
				c = x1+" "+r1+" "+y1+"\n"+x2+" "+r2+" "+y2+"\n"+x3+" "+r3+" "+y3+"\n"+x4+" "+r4+" "+y4+"\n"+x5+" "+r5+" "+y5+"\n"+x6+" "+r6+" "+y6+"\n"+x7+" "+r7+" "+y7+"\n"+x8+" "+r8+" "+y8+"\n"+x9+" "+r9+" "+y9+"\n\n\n"
				# print c

				txt.write(str(a)+"\n")
				txt.write(b+"\n")
				txt.write(c)    
				i=i+1


3. videogrep all clips that are annotated with 'Dave'

searching with regular expressions (re) for either 'dave' and 'dave)'

import os
import re

p = "python videogrep.py --input 2001-space-odyssey-jupiter-mission.avi --search 'dave\ |dave\)' --output video-dave.mp4"
print p
os.system(p)


4. create a ordering of the grepped videoclips, export new videofile where the grepped clips are timestretched to the original shotduration

from __future__ import division 
import os
import csv
import re
import glob
import subprocess as sub
import datetime
import itertools

# open csv with duration column in it:
with open('../2001-chunks-annotations-01-duration.csv', 'rb') as f:
	reader = csv.reader(f)
	i=0

	# *** GET DURATION OF CSV SHEET (ORIGINAL SHOT DURATION) ***
	for row in reader:
		shotduration = row[3]
		print shotduration

		# --> if using an integer number:
		# shotduration = "".join(shotduration)
		# shotduration = shotduration.split('.')
		# shotduration = shotduration[0]

		# SHOTDURATION / GREPPEDDURATION = SETPTS

		# *** LIST OF VIDEO'S TO REPLACE ORIGINAL SHOTS ***
		videofiles = [
		'video-between-stars.mp4',
		'video-jupiter.mp4',
		'video-space.mp4',
		'video-space.mp4',
		'video-main-room.mp4',
		'video-hybernation-beds.mpeg',
		'video-main-room.mp4',
		'video-main-room.mp4',
		'video-main-room.mp4',
		'video-undefined-room.mp4',
		'video-food.mp4',
		'video-button.mp4',
		'video-door.mp4',
		'video-frank.mp4',
		'video-button.mp4',
		'video-button.mp4',
		'video-BBC.mp4',
		'video-main-room.mp4',
		'video-tablet.mp4',
		'video-couch.mp4',
		'video-earth.mp4',
		'video-other-three-astronauts.mpeg',
		'video-frank-and-dave.mp4',
		'video-hybernation.mp4',
		'video-tablet.mp4',
		'video-earth.mp4',
		'video-hybernation-beds.mpeg',
		'video-food.mp4',
		'video-frank-at-tablet.mp4',
		'video-frank-and-dave-at-tablet.mp4',
		'video-hybernation-beds.mpeg',
		'video-HAL.mp4',
		'video-main-room.mp4',
		'video-earth.mp4',
		'video-HAL.mp4',
		'video-earth.mp4',
		'video-BBC.mp4',
		'video-eating-table.mp4',
		'video-tablet.mp4',
		'video-earth.mp4',
		'video-frank-at-tablet.mp4',
		'video-HAL.mp4',
		'video-tablet.mp4',
		'video-main-room.mp4',
		'video-HAL.mp4'
		]
		# print videofiles[i]

		# *** READING DURATION OF LIST OF VIDEOFILES***
		v = "ffmpeg -i "+videofiles[i]+" 2>&1 | grep Duration | awk '{print $2}' | tr -d ,"
		# print v
		p = os.popen(v,'rb')
		while 1:
			line = p.readline()
			if not line: break
			# print line
			number = line.split(':')
			# print number[0]

			# *** CONVERTING TIMESTAMP 
			# TO INTEGER (IN SECONDS) ***
			hours = float(number[0])*2400
			# print number[1]
			minutes = float(number[1])*60
			# print number[2]
			seconds = float(number[2])

			# --> if using integer number:  
			# seconds = number[2].split('.')
			# seconds = seconds[0]

			clipduration = float(hours)+float(minutes)+float(seconds)
			print clipduration	

		# set stretch value:
		pts = float(shotduration) / float(clipduration)
		print pts
		# print i

		# *** STRETCH THE VIDEOFILES TO ORIGINAL SHOT DURATION
		# (DIVIDING SHOTDURATION / CLIPDURATION)
		# SHOTDURATION = FROM ORIGINAL FILM SPEED
		# CLIPDURATION = LENGTH OF VIDEOGREPPED VIDEOS ***
		x = "ffmpeg -i "+videofiles[i]+" -vf 'setpts="+str(pts)+"*PTS' -an -y export/0"+str(i)+".mpeg"
		print x
		os.system(x)

		# *** WRITE VIDEO FILENAME TO TXT FILE ***
		with open('clipslist.txt', 'w') as txt:
			m = 0
			for video in videofiles:
				print>>txt,"file 'export/0"+str(m)+".mpeg'"
				m=m+1

		print "ENDING"
		i=i+1

# *** joining the stretched clips together in one file ***
a = "ffmpeg -f concat -i clipslist.txt -c copy 2001-joined-clips-01.avi"
print a
os.system(a)