#!/usr/bin/env python
"""
This utility will search *any* file and look for what *appears* to be a DV
video frames and copy them into a new Raw DV file.
Written by Dan Dennedy <dan@dennedy.org>
"""
import sys
def is_dv_1(s):\
return ord(s[0]) == 0x1f and ord(s[1]) == 0x07 and ord(s[2]) == 0x00
def is_dv_2(s):
return ord(s[1]) == 0x07 and ord(s[2]) == 0x00 and ord(s[3]) == 0xff
def is_pal(s):
return (ord(s[3]) & 0x80)
if len(sys.argv) < 3 or sys.argv[1][0] == '-':
print 'Usage', sys.argv[0], '<input-file> <output-file>'
sys.exit(0)
inputFilename = sys.argv[1]
outputFilename = sys.argv[2]
print 'Salvaging from', inputFilename, 'into', outputFilename
pos = 0
count = 0
input = open(inputFilename, 'rb')
output = open(outputFilename, 'wb')
while True:
input.seek(pos)
dif1 = input.read(480)
if not dif1:
break
if is_dv_1(dif1):
dif2 = input.read(480)
if not dif2:
break
if is_dv_2(dif2):
size = (is_pal(dif1) and (480 * 300) or (480 * 250))
frame = input.read(size - 480 * 2)
if frame:
output.write(dif1)
output.write(dif2)
output.write(frame)
count += 1
if count % (size/4800) == 0:
print '\rSalvaging frame', count,
sys.stdout.flush()
pos += size
else:
break
else:
pos += 1
else:
pos += 1
input.close()
output.close()
print '\nSalvaging frame %d. Done.' % (count,)
sys.exit(0)
|