Skip to content

Instantly share code, notes, and snippets.

@JakeTrock
Created September 14, 2022 14:26
Show Gist options
  • Select an option

  • Save JakeTrock/5b984bf9d519b371e7f31a8f689c198b to your computer and use it in GitHub Desktop.

Select an option

Save JakeTrock/5b984bf9d519b371e7f31a8f689c198b to your computer and use it in GitHub Desktop.

Revisions

  1. JakeTrock created this gist Sep 14, 2022.
    63 changes: 63 additions & 0 deletions salvagedv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/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 <[email protected]>
    """

    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)