xpra icon
Bug tracker and wiki

Ticket #1107: save-video-to-file.patch

File save-video-to-file.patch, 7.3 KB (added by Antoine Martin, 4 years ago)

makes it possible to save the raw video stream to a file on the server

  • xpra/codecs/enc_x264/encoder.pyx

     
    1212X264_LOGGING = os.environ.get("XPRA_X264_LOGGING", "WARNING")
    1313LOG_NALS = os.environ.get("XPRA_X264_LOG_NALS", "0")=="1"
    1414USE_OPENCL = os.environ.get("XPRA_X264_OPENCL", "0")=="1"
     15SAVE_TO_FILE = os.environ.get("XPRA_SAVE_TO_FILE")
    1516
    16 
    17 from xpra.util import nonl
     17from xpra.util import nonl, AtomicInteger
    1818from xpra.os_util import bytestostr
    1919from xpra.codecs.codec_constants import get_subsampling_divs, video_spec
    2020from collections import deque
     
    322322def get_type():
    323323    return "x264"
    324324
     325generation = AtomicInteger()
    325326def get_info():
    326327    global COLORSPACES, MAX_WIDTH, MAX_HEIGHT
    327328    return {"version"   : get_version(),
    328329            "buffer_api": get_buffer_api_version(),
    329330            "max-size"  : (MAX_WIDTH, MAX_HEIGHT),
     331            "generation": generation.get(),
    330332            "formats"   : COLORSPACES.keys()}
    331333
    332334def get_encodings():
     
    410412    cdef unsigned long long bytes_in
    411413    cdef unsigned long long bytes_out
    412414    cdef object last_frame_times
     415    cdef object file
    413416    cdef uint64_t first_frame_timestamp
    414417
    415418    cdef object __weakref__
    416419
    417420    def init_context(self, int width, int height, src_format, dst_formats, encoding, int quality, int speed, scaling, options):    #@DuplicatedSignature
    418         global COLORSPACE_FORMATS
     421        global COLORSPACE_FORMATS, generation
    419422        cs_info = COLORSPACE_FORMATS.get(src_format)
    420423        assert cs_info is not None, "invalid source format: %s, must be one of: %s" % (src_format, COLORSPACE_FORMATS.keys())
    421424        assert encoding=="h264", "invalid encoding: %s" % encoding
     
    441444            self.profile = cs_info[1]
    442445            log("using default profile=%s", self.profile)
    443446        self.init_encoder()
     447        gen = generation.increase()
     448        if SAVE_TO_FILE is not None:
     449            filename = SAVE_TO_FILE+str(gen)+".%s" % encoding
     450            self.file = open(filename, 'wb')
     451            log.info("saving %s stream to %s", encoding, filename)
    444452
    445453    cdef init_encoder(self):
    446454        cdef x264_param_t param
     
    487495        self.bytes_out = 0
    488496        self.last_frame_times = []
    489497        self.first_frame_timestamp = 0
     498        f = self.file
     499        if f:
     500            self.file = None
     501            f.close()
    490502
    491503
    492504    def get_info(self):             #@DuplicatedSignature
     
    643655        self.frames += 1
    644656        self.last_frame_times.append((start, end))
    645657        assert self.context!=NULL
     658        if self.file and frame_size>0:
     659            self.file.write(cdata)
     660            self.file.flush()
    646661        return  cdata, client_options
    647662
    648663
  • xpra/codecs/vpx/encoder.pyx

     
    88from collections import deque
    99from xpra.codecs.codec_constants import video_spec
    1010from xpra.os_util import bytestostr
     11from xpra.util import AtomicInteger
    1112
    1213from xpra.log import Logger
    1314log = Logger("encoder", "vpx")
    1415
     16SAVE_TO_FILE = os.environ.get("XPRA_SAVE_TO_FILE")
    1517
     18
    1619#sensible default:
    1720cpus = 2
    1821try:
     
    269272    return [input_colorspace]
    270273
    271274
     275generation = AtomicInteger()
    272276def get_info():
    273277    global CODECS, MAX_SIZE
    274278    info = {"version"       : get_version(),
     
    275279            "encodings"     : CODECS,
    276280            "buffer_api"    : get_buffer_api_version(),
    277281            "abi_version"   : get_abi_version(),
     282            "generation"    : generation.get(),
    278283            "build_config"  : vpx_codec_build_config()}
    279284    for e, maxsize in MAX_SIZE.items():
    280285        info["%s.max-size" % e] = maxsize
     
    368373    cdef int quality
    369374    cdef int lossless
    370375    cdef object last_frame_times
     376    cdef object file
    371377
    372378    cdef object __weakref__
    373379
     
    461467                self.codec_control("periodic Q boost", VP9E_SET_FRAME_PERIODIC_BOOST, 0)
    462468        self.do_set_encoding_speed(speed)
    463469        self.do_set_encoding_quality(quality)
     470        gen = generation.increase()
     471        if SAVE_TO_FILE is not None:
     472            filename = SAVE_TO_FILE+str(gen)+".%s" % encoding
     473            self.file = open(filename, 'wb')
     474            log.info("saving %s stream to %s", encoding, filename)
    464475
    465476
    466477    def codec_control(self, info, int attr, int value):
     
    563574        self.max_threads = 0
    564575        self.encoding = ""
    565576        self.src_format = ""
     577        f = self.file
     578        if f:
     579            self.file = None
     580            f.close()
    566581
    567582
    568583    def compress_image(self, image, quality=-1, speed=-1, options={}):
     
    659674        log("vpx returning %s image: %s bytes", self.encoding, len(img))
    660675        end = time.time()
    661676        self.last_frame_times.append((start, end))
     677        if self.file and pkt.data.frame.sz>0:
     678            self.file.write(img)
     679            self.file.flush()
    662680        return img
    663681
    664682    def set_encoding_speed(self, int pct):
  • xpra/codecs/xvid/encoder.pyx

     
    99from xpra.log import Logger
    1010log = Logger("encoder", "xvid")
    1111
    12 from xpra.util import nonl
    13 from xpra.os_util import bytestostr
    14 from xpra.codecs.codec_constants import get_subsampling_divs, video_spec
    15 from collections import deque
     12from xpra.util import AtomicInteger
     13from xpra.codecs.codec_constants import video_spec
    1614
    17 from libc.stdint cimport int64_t, uint64_t, uint8_t
     15SAVE_TO_FILE = os.environ.get("XPRA_SAVE_TO_FILE")
    1816
    1917
     18from libc.stdint cimport uint8_t
     19
    2020cdef extern from "string.h":
    2121    void * memset ( void * ptr, int value, size_t num )
    2222
     
    269269def get_type():
    270270    return "xvid"
    271271
     272generation = AtomicInteger()
    272273def get_info():
    273274    global COLORSPACES, MAX_WIDTH, MAX_HEIGHT
    274275    return {"version"   : get_version(),
    275276            "buffer_api": get_buffer_api_version(),
    276277            "max-size"  : (MAX_WIDTH, MAX_HEIGHT),
     278            "generation": generation.get(),
    277279            "formats"   : COLORSPACES}
    278280
    279281def get_encodings():
     
    310312    cdef int speed
    311313    cdef unsigned long long bytes_in
    312314    cdef unsigned long long bytes_out
     315    cdef object file
    313316
    314317    cdef object __weakref__
    315318
     
    326329        self.frames = 0
    327330        self.time = 0
    328331        self.init_encoder()
     332        gen = generation.increase()
     333        if SAVE_TO_FILE is not None:
     334            filename = SAVE_TO_FILE+str(gen)+".%s" % encoding
     335            self.file = open(filename, 'wb')
     336            log.info("saving %s stream to %s", encoding, filename)
    329337
    330338    cdef init_encoder(self):
    331339        cdef int r, i
     
    367375        self.time = 0
    368376        self.quality = 0
    369377        self.speed = 0
     378        f = self.file
     379        if f:
     380            self.file = None
     381            f.close()
    370382
    371383
    372384    def get_info(self):             #@DuplicatedSignature
     
    489501        self.time += end-start
    490502        self.frames += 1
    491503        assert self.context!=NULL
     504        if self.file and r>0:
     505            self.file.write(cdata)
     506            self.file.flush()
    492507        return  cdata, client_options
    493508
    494509