<commit_before>#!/usr/bin/python
"""
  Utility for extraction of video stream from navdata log
  usage:
       ./video.py <input navdata log> <output video file>
"""
import sys
import struct
from navdata import cutPacket,videoAckRequired


def navdata2video( inputFile, outputFile ):
    data = open(inputFile, "rb").read()
    arr = []
    while len(data) > 0:
        packet, data = cutPacket( data )
        if videoAckRequired( packet ):
            frameNumber, frameFlags, fragmentNumber = struct.unpack("<HBB", packet[7:11])
            arr.append( (frameNumber, fragmentNumber, packet[12:]) )
    vf = open(outputFile, "wb")
    prev = None
    for a in sorted(arr):
        if prev != a[:2]:
            vf.write(a[2])
        else:
            print "duplicity", prev
        prev = a[:2]
    vf.close()


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print __doc__
        sys.exit(2)
    navdata2video( sys.argv[1], sys.argv[2] )

# vim: expandtab sw=4 ts=4 
<commit_msg>added option for dumping frames for youtube video "re-packing"<commit_after>#!/usr/bin/python
"""
  Utility for extraction of video stream from navdata log
  usage:
       ./video.py <input navdata log> <output video file> [<output frames directory>]
"""
import sys
import struct
import os
from navdata import cutPacket,videoAckRequired


def navdata2video( inputFile, outputFile, outDir = ".", dumpIndividualFrames=False, startIndex=0 ):
    data = open(inputFile, "rb").read()
    arr = []
    while len(data) > 0:
        packet, data = cutPacket( data )
        if videoAckRequired( packet ):
            frameNumber, frameFlags, fragmentNumber = struct.unpack("<HBB", packet[7:11])
            arr.append( (frameNumber, fragmentNumber, packet[12:]) )
    vf = open(outputFile, "wb")
    prev = None
    frameIndex = startIndex
    frame = ""
    for a in sorted(arr):
        if prev != a[:2]:
            if prev is not None and prev[0] != a[0]:
                # new frame
                if dumpIndividualFrames:
                    fout = open(outDir+os.sep+"frame%04d.bin" % frameIndex, "wb")
                    fout.write(frame)
                    fout.close()                    
                frame = ""
                frameIndex += 1
            frame += a[2]
            vf.write(a[2])
        else:
            print "duplicity", prev
        prev = a[:2]
    vf.close()


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print __doc__
        sys.exit(2)
    if len(sys.argv) == 3:
        navdata2video( sys.argv[1], sys.argv[2] )
    else:
        outDir = sys.argv[3]
        startIndex = len(os.listdir( outDir ))
        navdata2video( sys.argv[1], sys.argv[2], outDir=outDir, dumpIndividualFrames=True, startIndex=startIndex )

# vim: expandtab sw=4 ts=4 

{'fraction_non_alphanumeric': 0.12598425196850394, 'fraction_numerical': 0.0164638511095204, 'mean_word_length': 2.2462253193960513, 'pattern_counts': {'":': 2, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6995927', 'n_tokens_mistral': 915, 'n_tokens_neox': 868, 'n_words': 235}