def flvcat(outfile, srcfiles, framerate=12, keyframe=120, blocksize=32, clipping=None, panwindow=None, panspeed=0, force=False, debug=0): if not force and os.path.exists(outfile): raise IOError('file already exists: %r' % outfile) fout = open(outfile, 'wb') writer = FLVWriter(fout, has_video=True, has_audio=True, framerate=framerate, debug=debug) processor = FLVMovieProcessor(writer=writer, debug=debug) for fname in srcfiles: ranges = None m = re.match(r'([^:]+):(.+)$', fname) if m: fname = m.group(1) ranges = MultipleRange(m.group(2)) fin = open(fname, 'rb') parser = FLVParser(fin) audiosink = AudioSink() videosink = FLVVideoSink(writer, framerate=framerate, keyframe=keyframe, blocksize=blocksize, clipping=clipping, panwindow=panwindow, panspeed=panspeed, debug=debug) processor.process_flv(parser, audiosink, videosink, ranges=ranges) parser.close() fin.close() writer.close() fout.close() return
def flvsplit(outbase, srcfile, framerate=12, keyframe=120, blocksize=32, duration=sys.maxsize, overlap=0, nameformat='%s-%03d.flv', force=False, debug=0): fin = open(srcfile, 'rb') parser = FLVParser(fin) totaldur = parser.get_duration() (_,_,totaldur,_,_) = parser[-1] print('total duration: %d' % totaldur, file=sys.stderr) t0 = 0 i = 0 while 1: outfile = nameformat % (outbase, i) if not force and os.path.exists(outfile): raise IOError('file already exists: %r' % outfile) fout = open(outfile, 'wb') writer = FLVWriter(fout, has_video=True, has_audio=True, framerate=framerate, debug=debug) processor = FLVMovieProcessor(writer=writer, debug=debug) audiosink = AudioSink() videosink = FLVVideoSink(writer, framerate=framerate, keyframe=keyframe, blocksize=blocksize, debug=debug) t1 = min(t0+duration, totaldur) print('writing %r (%d-%d)...' % (outfile, t0, t1), file=sys.stderr) ranges = MultipleRange([(t0, t1)]) processor.process_flv(parser, audiosink, videosink, ranges=ranges) writer.close() fout.close() if totaldur <= t1: break t0 = max(0, t1-overlap) i += 1 parser.close() fin.close() return
def mp3add(srcfile, mp3files, outfile, force=False, debug=0): if not force and os.path.exists(outfile): raise IOError('file already exists: %r' % outfile) fout = open(outfile, 'wb') writer = FLVWriter(fout, debug=debug, has_video=True, has_audio=True) fin = open(srcfile, 'rb') parser = FLVParser(fin, debug=debug) for (i, (tag, _, timestamp, _, _)) in enumerate(parser): if tag == 8: pass elif tag == 9: writer.write_video_frame(timestamp, parser.get_data(i)) elif tag == 18: (k, v) = parser.parse_metadata(parser.get_data(i)) if k == 'onMetaData': writer.set_screen_size(v.get('width', 0), v.get('height', 0)) else: writer.write_other_data(tag, parser.get_data(i)) parser.close() fin.close() for fname in mp3files: m = re.match(r'([^:]+):(.+)$', fname) ranges = None if m: fname = m.group(1) ranges = MultipleRange(m.group(2)) audio = AudioSink() fp = open(fname, 'rb') audio.load(fp) fp.close() if ranges: for (_, s, e) in ranges: audio.put(writer, s, e, s) else: audio.put(writer) writer.close() fout.close() return
def flvdump(fp, verbose=0, debug=0): parser = FLVParser(fp, debug=debug) timestamp = 0 other = video = audio = 0 for (i, (tag, length, timestamp, offset, _)) in enumerate(parser): data = parser.get_data(i) if tag == 8: # Audio tag flags = ord(data[0]) atype = ATYPE.get((flags & 0xf0) >> 4, '?') rate = RATE[(flags & 0x0c) >> 2] if flags & 2: samplesize = 16 else: samplesize = 8 if flags & 1: ch = 'stereo' else: ch = 'mono' audio += 1 if 1 <= verbose: print ('%08d: audio: %s, rate=%d, %dbit, %s (%d bytes)' % (timestamp, atype, rate, samplesize, ch, length)) elif tag == 9: # Video tag ftype = FTYPE.get(ord(data[0]) >> 4, '?') buf = StringIO(data) codec = ord(buf.read(1)) & 0xf cname = CODEC.get(codec, '?') video += 1 if 1 <= verbose: if codec == 3: x = ord(buf.read(1)) y = ord(buf.read(1)) blockwidth = ((x >> 4)+1) * 16 imagewidth = (x & 0xf) << 8 | y x = ord(buf.read(1)) y = ord(buf.read(1)) blockheight = ((x >> 4)+1) * 16 imageheight = (x & 0xf) << 8 | y print ('%08d: video: %s/%s %dx%d (block:%dx%d) (%d bytes)' % (timestamp, cname, ftype, imagewidth, imageheight, blockwidth, blockheight, length)) if 2 <= verbose: r = [] for y in xrange((imageheight + blockheight-1)/blockheight): for x in xrange((imagewidth + blockwidth-1)/blockwidth): (n,) = unpack('>H', buf.read(2)) buf.read(n) r.append(n) print ' ',r else: print '%08d: video: %s/%s (%d bytes)' % (timestamp, cname, ftype, length) elif tag == 18: # Data tag print '%08d: data (%d bytes)' % (timestamp, length) if 1 <= verbose: (k,v) = parser.parse_metadata(data) print ' %s: %r' % (k,v) else: other += 1 if 1 <= verbose: print ('%08d: tag %d (%d bytes)' % (timestamp, tag, length)) if 2 <= verbose: N = 16 for i in xrange(0, len(data), N): line = data[i:i+N] print ' %s: %r' % (' '.join( '%02x' % ord(c) for c in line ), line) parser.close() print 'time=%.3f, video: %d, audio: %d, other: %d' % (timestamp*.001, video, audio, other) return