resources/archive/video_exporter.py

The module provides a high-level interface of video export in the form of the ShotSaver class.

Class arguments

  • callback (callable[str, str], optional)Callback function to which the current export status is passed.
  • queue_maxlen (int, optional): Maximum export queue length. 1000 by default.

Class public attributes

  • export_folder (str)Standard folder for video export. Server screenshots folder by default.
  • file_name_tmpl (str): Common filename pattern. By default: "{name} ({dt_start} - {dt_end}){sub}.avi"
  • callback (callable[str, str], optional): Same as the callback argument.
  • cancel_task_with_states (Tuple[int])List of task states that are being removed from the task list in the gui interface of the Trassir.  (2, 3) by default. 
    Possible states:
    • 0 - in queue
    • 1 - exporting
    • 2 - success
    • 3 - failed

By default, all tasks from the list that are successfully completed or with an error are being removed.
For debugging, you can disable automatic task cleaning.

from video_exporter import VideoExporter

ve = VideoExporter()
ve.cancel_task_with_states = tuple()

Class methods

  • export - Creating a task for exporting a video fragment.
    List of arguments:
    • channel_full_guid (str): Channel guid in the form of ChannelGuid_ServerGuid .
    • dt_start (datetime.datetime)Start date / time of the video segment to export.
    • dt_end (datetime.datetime, optional)End date / time of the video segment to export.
    • duration (int, optional)Duration of the video segment in seconds. Used only if dt_end is not specified. 60 by default.
    • prefer_substream (bool, optional)If True - exports a substream. False by default.
    • file_name (str, optional)Name of the exported file. If not specified, generates a name according to the file_name_tmpl parameter.
    • file_path (str, optional)Path to export. If not specified, the path specified in the export_folder parameter is used.
    • options (Dict[str: any])Additional export options, described below.
    • callback (callable[str, str], optional)Callback function to which the current export status is passed. If not specified, the callback class parameter is used.
    • task_guid (str, optional)Guid of the task. The default is set using the host.random_guid() function.

Export options

  • is_hardware (bool)Specifies the video source. False - TRASSIR, True - camera.
  • video_codec ('mpeg4'|'wmv')Video codec name. Letter case is ignored. If you specify any other value, including an empty string, the original video format will be used (without transcoding).
  • video_bitrate (200|400|600|800|1000|2000|2500|3000|5000|7500|10000|16000)Bitrate of the output video stream in kB/s. If any other value is specified, the nearest not higher than supported will be used, i.e. if you specify 999, the value 800 will be used, except for values ​​less than 200 (including negative ones), in which case the value 200 will be used. By default, the original bitrate of the original video is used, but when this field is specified, the original bitrate cannot be specified. So if you want the original bitrate, don't set the video_bitrate field at all.
    Applies only if video codec is specified (re-encoding enabled), otherwise ignored.
  • video_resolution ('2560x1920'|'2048x1536'|'1920x1080'|'1600x1200'|'1280x1024'|'1280x1960'|'1280x720'|'1024x768'| '800x600'|'720x576'|'704x576'|'640x480'|'352x288'|'320x240'|'176x144'):  Output video resolution. Any other values ​​are not allowed. If an empty line is specified, the original video resolution will be used.
    Applies only if video codec is specified (re-encoding enabled), otherwise ignored.
  • audio_codec ('no audio'|'pcm'|'wma')Audio codec name. Letter case is ignored. If you specify any other value, including an empty string, the original audio format will be used (no re-encoding).
  • audio_bitrate (64|128)Bitrate of the output audio stream in kB/s. If you specify a value greater than 96, the value 128 will be used, otherwise the value 64 will be used. By default, the original bitrate of the original audio stream is used, but when this field is specified, the original bitrate cannot be specified. So if you want the original bitrate, don't set the audio_bitrate field at all.
    Applies only if audio codec is specified (re-encoding enabled), otherwise ignored.
  • need_fliprotate (bool): If True, the "flip" and "rotate" parameters from the corresponding channel settings will be applied.
    Applies only if video codec is specified (re-encoding enabled), otherwise ignored.
  • watermark_need_figures (bool)If True, figures (if any) will be drawn over the video.
    Applies only if video codec is specified (re-encoding enabled), otherwise ignored.
  • need_channel_name_watermark (bool): If True, the channel name will be drawn over the video (taken from the settings of the corresponding channel).
    Applies only if video codec is specified (re-encoding enabled), otherwise ignored.
  • need_timestamp_watermark (bool): If True, the timestamp of the current frame will be drawn over the video in the area specified by the watermark_align field.
    Applies only if video codec is specified (re-encoding enabled), otherwise ignored.
  • watermark_align (0|1|2|3|4): Defines in which area of ​​the frame the watermark text will be drawn.
    • 0 or 1 - top left corner;
    • 2 - top right corner;
    • 3 - lower left corner;
    • 4 - lower right corner.

Export statuses

Possible export statuses are contained in the status object:

  • status.canceled - Task has been canceled.
  • status.exporting - Video is being exported.
  • status.error - Export error has occurred.
  • status.success - Video has been successfully exported.
  • status.in_queue - Task is in the export queue.

Code examples

Exporting the archive for the last 15 secs.
import datetime
from video_exporter import VideoExporter

CHANNEL_GUID = "uR76TlcA_O4Yazs8u"

ve = VideoExporter()

dt = datetime.datetime.now() - datetime.timedelta(seconds=15)
ve.export(CHANNEL_GUID, dt, duration=15)
Exporting the substream for today from 12:00 to 12:15.
import datetime
from video_exporter import VideoExporter

CHANNEL_GUID = "uR76TlcA_O4Yazs8u"

ve = VideoExporter()

today = datetime.datetime.now().today()

dt_start = datetime.datetime.combine(today, datetime.time(hour=12))
dt_end = datetime.datetime.combine(today, datetime.time(hour=12, minute=15))

ve.export(CHANNEL_GUID, dt_start, dt_end=dt_end, prefer_substream=True)
Tracking the video export status

Opening the video on successful export.

import os
import datetime
import host
from video_exporter import VideoExporter, status

CHANNEL_GUID = "uR76TlcA_O4Yazs8u"

tasks = {}


def callback(guid, state):
    host.message("[%s] %s" % (guid, state))
    if state == status.success:
        fpath = tasks.pop(guid)
        os.startfile(fpath)
    elif state == status.error:
        fpath = tasks.pop(guid)
        host.error("Export failed: %s" % fpath)


ve = VideoExporter(callback=callback)

dt = datetime.datetime.now() - datetime.timedelta(seconds=15)
task_guid, file_path = ve.export(CHANNEL_GUID, dt, duration=15)
tasks[task_guid] = file_path


  • Нет меток