def test_parse_audio_media_segment(self): with open(os.path.join(test_utils.TEST_PATH, 'data/audio_segment.m4s'), 'rb') as f: data = f.read() root = mp4.mp4(data, len(data)) # mfhd mfhd = root.find('moof.mfhd') self.assertTrue(mfhd) self.assertEquals(mfhd.seqno, 1) # tfhd tfhd = root.find('moof.traf.tfhd') self.assertTrue(tfhd) self.assertEquals(tfhd.track_id, 1) # tfdt tfdt = root.find('moof.traf.tfdt') self.assertTrue(tfdt) self.assertEquals(tfdt.decode_time, 0) # trun trun = root.find('moof.traf.trun') self.assertTrue(trun) self.assertEquals(trun.sample_count, 282)
def fetch(url, key=None): if not url.startswith('http'): data = open(url, 'rb').read() print 'read data of length: {0}'.format(len(data)) print '--' else: url_parts = urlparse.urlparse(url) conn = httplib.HTTPConnection(url_parts.netloc) conn.set_debuglevel(1) conn.connect() print 'downloading {0}...'.format(url) conn.request('GET', url_parts.path) res = conn.getresponse() if res.status == 302: return fetch(res.getheader('location')) print '--' data = res.read() print 'fetched data of length: {0}'.format(len(data)) print '--' if data: root = mp4.mp4(data, len(data), key=key) print root.description() else: print data
def test_parse_audio_init_segment(self): with open(os.path.join(test_utils.TEST_PATH, 'data/audio_init.mp4'), 'rb') as f: data = f.read() root = mp4.mp4(data, len(data)) # mvhd mvhd = root.find('moov.mvhd') self.assertTrue(mvhd) self.assertEquals(mvhd.timescale, 90000) self.assertEquals(mvhd.duration, 9630720) # tkhd tkhd = root.find('moov.trak.tkhd') self.assertTrue(tkhd) self.assertEquals(tkhd.track_id, 1) self.assertEquals(tkhd.duration, 9630720) # mdhd mdhd = root.find('moov.trak.mdia.mdhd') self.assertTrue(mdhd) self.assertEquals(mdhd.timescale, 48000) self.assertEquals(mdhd.duration, 5136384) # mp4a mp4a = root.find('moov.trak.mdia.minf.stbl.stsd.mp4a') self.assertTrue(mp4a) self.assertEquals(mp4a.channels, 2) self.assertEquals(mp4a.sample_size, 16) self.assertEquals(mp4a.sample_rate, 48000) # esds esds = root.find('moov.trak.mdia.minf.stbl.stsd.mp4a.esds') self.assertTrue(esds)
def main(): mp4file='/Users/ramberg/Desktop/birthday.MOV' foo = mp4(mp4file) foo.readfile(mp4file) ppath=os.environ['PYTHONPATH'].split(os.pathsep) print "Path ", ppath print " test Done"
def test_parse_video_init_segment(self): with open(os.path.join(test_utils.TEST_PATH, 'data/video_init.mp4'), 'rb') as f: data = f.read() root = mp4.mp4(data, len(data)) # mvhd mvhd = root.find('moov.mvhd') self.assertTrue(mvhd) self.assertEquals(mvhd.timescale, 90000) self.assertEquals(mvhd.duration, 9623970) # tkhd tkhd = root.find('moov.trak.tkhd') self.assertTrue(tkhd) self.assertEquals(tkhd.track_id, 5) self.assertEquals(tkhd.duration, 9623970) # mdhd mdhd = root.find('moov.trak.mdia.mdhd') self.assertTrue(mdhd) self.assertEquals(mdhd.timescale, 90000) self.assertEquals(mdhd.duration, 9623970) # avc1 avc1 = root.find('moov.trak.mdia.minf.stbl.stsd.avc1') self.assertTrue(avc1) self.assertEquals(avc1.width, 320) self.assertEquals(avc1.height, 180) # avcC avcC = root.find('moov.trak.mdia.minf.stbl.stsd.avc1.avcC') self.assertTrue(avcC) self.assertEquals(avcC.profile_ind, 100) self.assertEquals(avcC.level, 13)
def main(): segment_file = sys.argv[1] key = None if len(sys.argv) == 3: key = binascii.unhexlify(sys.argv[2]) with open(segment_file, 'rb') as f: segment = f.read() root = mp4.mp4(segment) print root.description() # Parse some boxes moof = root.find('moof') traf = moof.find('traf') tfdt = traf.find('tfdt') trun = traf.find('trun') nr_samples = trun.sample_count sample_info = [trun.sample_entry(i) for i in range(nr_samples)] sample_offset = moof.offset + trun.data_offset # Get IVs ivs = [] sub_sample_vec = [] uuids = traf.find_all('uuid') for uuid in uuids: if uuid.extended_type == binascii.unhexlify(SENC_GUID): data = uuid.fmap[uuid.offset+28:uuid.offset+uuid.size] senc = mp4.sampleEncryption_box(data, uuid.version, uuid.flags, iv_size=8) senc.decoration # Needed to set the ivs = senc.ivs sub_sample_vec = senc.sub_sample_vec # Loop over samples cnt = 0 for sample in sample_info: size = sample['size'] sample_data = segment[sample_offset:sample_offset + size] # Decrypt if encrytped if ivs: iv = ivs[cnt] iv16 = binascii.unhexlify(iv) + '\x00\x00\x00\x00\x00\x00\x00\x00' print 'key_len=', len(key), ', iv_len=', len(iv16) print 'key=', binascii.hexlify(key), 'iv=', binascii.hexlify(iv16) if sub_sample_vec: # Sub-sample encryption (H264) - NOTE: some manual work needed. sub_samples = sub_sample_vec[cnt] pos = 0 for sub_sample in sub_samples: clear = sub_sample[0] encrypted = sub_sample[1] pos += clear print 'clear=', clear, 'encrypted=', encrypted decrypted_part = decrypt_ctr(sample_data[pos:pos+encrypted], key, iv16) sample_data_2 = sample_data[0:pos] sample_data_2 += decrypted_part sample_data_2 += sample_data[pos+encrypted:] sample_data = sample_data_2 pos += encrypted else: # Full sample encryption (AAC) decrypted = decrypt_ctr(sample_data, key, iv16) #decrypted = decrypt_cbc(sample_data, key, iv16) sample_data = decrypted # Display sample data print '' print 'sample size:', len(sample_data) print ts.dump_hex(sample_data, 16); sample_offset += size cnt += 1
def __init__(self, name, data): self.name = name self.root = mp4(data) self.segment_data = self._find_subsegment_data(self.root) self.sidx_segment_data = self._get_sidx_segment_data(self.root)
def main(): segment_file = sys.argv[1] key = None if len(sys.argv) == 3: key = binascii.unhexlify(sys.argv[2]) with open(segment_file, 'rb') as f: segment = f.read() root = mp4.mp4(segment) print root.description() # Parse some boxes moof = root.find('moof') traf = moof.find('traf') tfdt = traf.find('tfdt') trun = traf.find('trun') nr_samples = trun.sample_count sample_info = [trun.sample_entry(i) for i in range(nr_samples)] sample_offset = moof.offset + trun.data_offset # Get IVs ivs = [] sub_sample_vec = [] uuids = traf.find_all('uuid') for uuid in uuids: if uuid.extended_type == binascii.unhexlify(SENC_GUID): data = uuid.fmap[uuid.offset + 28:uuid.offset + uuid.size] senc = mp4.sampleEncryption_box(data, uuid.version, uuid.flags, iv_size=8) senc.decoration # Needed to set the ivs = senc.ivs sub_sample_vec = senc.sub_sample_vec # Loop over samples cnt = 0 for sample in sample_info: size = sample['size'] sample_data = segment[sample_offset:sample_offset + size] # Decrypt if encrytped if ivs: iv = ivs[cnt] iv16 = binascii.unhexlify(iv) + '\x00\x00\x00\x00\x00\x00\x00\x00' print 'key_len=', len(key), ', iv_len=', len(iv16) print 'key=', binascii.hexlify(key), 'iv=', binascii.hexlify(iv16) if sub_sample_vec: # Sub-sample encryption (H264) - NOTE: some manual work needed. sub_samples = sub_sample_vec[cnt] pos = 0 for sub_sample in sub_samples: clear = sub_sample[0] encrypted = sub_sample[1] pos += clear print 'clear=', clear, 'encrypted=', encrypted decrypted_part = decrypt_ctr( sample_data[pos:pos + encrypted], key, iv16) sample_data_2 = sample_data[0:pos] sample_data_2 += decrypted_part sample_data_2 += sample_data[pos + encrypted:] sample_data = sample_data_2 pos += encrypted else: # Full sample encryption (AAC) decrypted = decrypt_ctr(sample_data, key, iv16) #decrypted = decrypt_cbc(sample_data, key, iv16) sample_data = decrypted # Display sample data print '' print 'sample size:', len(sample_data) print ts.dump_hex(sample_data, 16) sample_offset += size cnt += 1