"Fossies" - the Fresh Open Source Software Archive

Member "horizon-16.0.0/openstack_dashboard/test/integration_tests/video_recorder.py" (16 Oct 2019, 2621 Bytes) of package /linux/misc/openstack/horizon-16.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 import logging
   14 import os
   15 import signal
   16 import subprocess
   17 from tempfile import mktemp
   18 from threading import Thread
   19 import time
   20 
   21 LOG = logging.getLogger(__name__)
   22 
   23 
   24 class VideoRecorder(object):
   25 
   26     def __init__(self, width, height, display='0.0', frame_rate=15):
   27         self.is_launched = False
   28         self.file_path = mktemp() + '.mp4'
   29         # avconv -f x11grab -r 15 -s 1920x1080 -i :0.0 -codec libx264 out.mp4
   30         self._cmd = ['avconv', '-f', 'x11grab', '-r', str(frame_rate),
   31                      '-s', '{}x{}'.format(width, height),
   32                      '-i', ':{}'.format(display),
   33                      '-codec', 'libx264', self.file_path]
   34 
   35     def start(self):
   36         if self.is_launched:
   37             LOG.warning('Video recording is running already')
   38             return
   39 
   40         if not os.environ.get('AVCONV_INSTALLED', False):
   41             LOG.error("avconv isn't installed. Video recording is skipped")
   42             return
   43 
   44         fnull = open(os.devnull, 'w')
   45         LOG.info('Record video via %s', ' '.join(self._cmd))
   46         self._popen = subprocess.Popen(self._cmd, stdout=fnull, stderr=fnull)
   47         self.is_launched = True
   48 
   49     def stop(self):
   50         if not self.is_launched:
   51             LOG.warning('Video recording is stopped already')
   52             return
   53 
   54         self._popen.send_signal(signal.SIGINT)
   55 
   56         def terminate_avconv():
   57             limit = time.time() + 10
   58 
   59             while time.time() < limit:
   60                 time.sleep(0.1)
   61                 if self._popen.poll() is not None:
   62                     return
   63 
   64             os.kill(self._popen.pid, signal.SIGTERM)
   65 
   66         t = Thread(target=terminate_avconv)
   67         t.start()
   68 
   69         self._popen.communicate()
   70         t.join()
   71         self.is_launched = False
   72 
   73     def clear(self):
   74         if self.is_launched:
   75             LOG.error("Video recording is running still")
   76             return
   77 
   78         if not os.path.isfile(self.file_path):
   79             LOG.warning("%s is absent already", self.file_path)
   80             return
   81 
   82         os.remove(self.file_path)