Exemple #1
0
def flvrec(filename, host='localhost', port=5900,
           framerate=12, keyframe=120,
           preferred_encoding=(0,), pwdfile=None,
           blocksize=32, clipping=None,
           cmdline=None,
           debug=0, verbose=1, time_per_piece=None):
    fp = file(filename, 'wb')
    if pwdfile:
        pwdcache = PWDFile(pwdfile)
    else:
        pwdcache = PWDCache('%s:%d' % (host,port))
    writer = FLVWriter(fp, framerate=framerate, debug=debug)
    sink = FLVVideoSink(writer,
                        blocksize=blocksize, framerate=framerate, keyframe=keyframe,
                        clipping=clipping, debug=debug)
    client = RFBNetworkClient(host, port, sink, timeout=500/framerate,
                              pwdcache=pwdcache, preferred_encoding=preferred_encoding,
                              debug=debug)
    if verbose:
        print >>sys.stderr, 'start recording'
    pid = 0
    if cmdline:
        pid = os.fork()
        if pid == 0:
            os.setpgrp()
            os.execvp('sh', ['sh', '-c', cmdline])
            sys.exit(1)
    retval = 0
    
    starttime = datetime.datetime.now()

    try:
        def sigint_handler(sig, frame):
            raise KeyboardInterrupt
        signal.signal(signal.SIGINT, sigint_handler)
        client.open()
        try:
            while 1:
                if datetime.datetime.now() > starttime + datetime.timedelta(minutes=time_per_piece):
                    starttime = datetime.datetime.now()
                    fp = file('out%s.flv' % time.strftime('%Y%m%d%H%M'), 'wb')
                    writer = FLVWriter(fp, framerate=framerate, debug=debug)
                    sink.replace_writer(writer)
                client.idle()
        finally:
            client.close()
    except KeyboardInterrupt:
        pass
    except socket.error, e:
        print >>sys.stderr, 'Socket error:', e
        retval = 1
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
def flvrec(filename,
           host='localhost',
           port=5900,
           framerate=12,
           keyframe=120,
           preferred_encoding=(0, ),
           pwdfile=None,
           blocksize=32,
           clipping=None,
           cmdline=None,
           debug=0,
           verbose=1):
    fp = file(filename, 'wb')
    if pwdfile:
        pwdcache = PWDFile(pwdfile)
    else:
        pwdcache = PWDCache('%s:%d' % (host, port))
    writer = FLVWriter(fp, framerate=framerate, debug=debug)
    sink = FLVVideoSink(writer,
                        blocksize=blocksize,
                        framerate=framerate,
                        keyframe=keyframe,
                        clipping=clipping,
                        debug=debug)
    client = RFBNetworkClient(host,
                              port,
                              sink,
                              timeout=500 / framerate,
                              pwdcache=pwdcache,
                              preferred_encoding=preferred_encoding,
                              debug=debug)
    if verbose:
        print >> sys.stderr, 'start recording'
    pid = 0
    if cmdline:
        pid = os.fork()
        if pid == 0:
            os.setpgrp()
            os.execvp('sh', ['sh', '-c', cmdline])
            sys.exit(1)
    retval = 0
    try:

        def sigint_handler(sig, frame):
            raise KeyboardInterrupt

        signal.signal(signal.SIGINT, sigint_handler)
        client.open()
        try:
            while 1:
                client.idle()
        finally:
            client.close()
    except KeyboardInterrupt:
        pass
    except socket.error, e:
        print >> sys.stderr, 'Socket error:', e
        retval = 1
Exemple #5
0
    def client_from_config(config):
        password = config.get('password')
        filename = config.get('filename')
        host = config.get('host')
        port = config.get('port')
        output_path = config.get('output_path')
        framerate = config.get('framerate')
        keyframe = config.get('keyframe')
        preferred_encoding = config.get('preferred_encoding')
        blocksize = config.get('blocksize')
        clipping = config.get('clipping')
        debug = config.get('debug')

        pwdcache = MemoryPWD(password)

        try:
            os.makedirs(output_path)
        except OSError as exc:
            if exc.errno == errno.EEXIST and os.path.isdir(output_path):
                pass
            else:
                raise
        fp = open(os.path.join(output_path, filename), 'wb')

        writer = FLVWriter(fp, framerate=framerate, debug=debug)
        sink = FLVVideoSink(writer,
                            blocksize=blocksize,
                            framerate=framerate,
                            keyframe=keyframe,
                            clipping=clipping,
                            debug=debug)
        client = RFBNetworkClient(host,
                                  port,
                                  sink,
                                  timeout=500 / framerate,
                                  pwdcache=pwdcache,
                                  preferred_encoding=preferred_encoding,
                                  debug=debug)

        return fp, writer, client