diff --git a/docs/api/settings.rst b/docs/api/settings.rst index c5947c4b5..466cb839a 100644 --- a/docs/api/settings.rst +++ b/docs/api/settings.rst @@ -254,6 +254,8 @@ mapped from the same fields in ``config.yaml`` unless otherwise noted: - * - ``webcam.ffmpegThreads`` - + * - ``webcam.ffmpegVideoCodec`` + - * - ``webcam.watermark`` - * - ``webcam.flipH`` diff --git a/docs/configuration/config_yaml.rst b/docs/configuration/config_yaml.rst index 79795f021..74fb7ed2d 100644 --- a/docs/configuration/config_yaml.rst +++ b/docs/configuration/config_yaml.rst @@ -1194,6 +1194,9 @@ Use the following settings to configure webcam support: # Should be left at 1 for RPi1. ffmpegThreads: 1 + # Videocodec to be used for encoding. Defaults to mpeg2video. + ffmpegVideoCodec: mpeg2video + # The bitrate to use for rendering the timelapse video. This gets directly passed to ffmpeg. bitrate: 5000k diff --git a/src/octoprint/server/api/settings.py b/src/octoprint/server/api/settings.py index f5983769a..a29856518 100644 --- a/src/octoprint/server/api/settings.py +++ b/src/octoprint/server/api/settings.py @@ -108,6 +108,7 @@ def getSettings(): "ffmpegPath": s.get(["webcam", "ffmpeg"]), "bitrate": s.get(["webcam", "bitrate"]), "ffmpegThreads": s.get(["webcam", "ffmpegThreads"]), + "ffmpegVideoCodec": s.get(["webcam", "ffmpegVideoCodec"]), "watermark": s.getBoolean(["webcam", "watermark"]), "flipH": s.getBoolean(["webcam", "flipH"]), "flipV": s.getBoolean(["webcam", "flipV"]), @@ -348,6 +349,7 @@ def _saveSettings(data): if "ffmpegPath" in data["webcam"]: s.set(["webcam", "ffmpeg"], data["webcam"]["ffmpegPath"]) if "bitrate" in data["webcam"]: s.set(["webcam", "bitrate"], data["webcam"]["bitrate"]) if "ffmpegThreads" in data["webcam"]: s.setInt(["webcam", "ffmpegThreads"], data["webcam"]["ffmpegThreads"]) + if "ffmpegVideoCodec" in data["webcam"] and data["webcam"]["ffmpegVideoCodec"] in ("mpeg2video", "libx264"): s.set(["webcam", "ffmpegVideoCodec"], data["webcam"]["ffmpegVideoCodec"]) if "watermark" in data["webcam"]: s.setBoolean(["webcam", "watermark"], data["webcam"]["watermark"]) if "flipH" in data["webcam"]: s.setBoolean(["webcam", "flipH"], data["webcam"]["flipH"]) if "flipV" in data["webcam"]: s.setBoolean(["webcam", "flipV"], data["webcam"]["flipV"]) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 7e3575dd7..9dbc4dc85 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -208,7 +208,8 @@ default_settings = { "snapshotSslValidation": True, "ffmpeg": None, "ffmpegThreads": 1, - "bitrate": "5000k", + "ffmpegVideoCodec": "mpeg2video", + "bitrate": "10000k", "watermark": True, "flipH": False, "flipV": False, diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index ee6c44ba1..44ca7da63 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -100,6 +100,7 @@ $(function() { }; self.webcam_available_ratios = ["16:9", "4:3"]; + self.webcam_available_videocodecs = ["mpeg2video", "libx264"]; var auto_locale = {language: "_default", display: gettext("Autodetect from browser"), english: undefined}; self.locales = ko.observableArray([auto_locale].concat(_.sortBy(_.values(AVAILABLE_LOCALES), function(n) { @@ -129,6 +130,7 @@ $(function() { self.webcam_ffmpegPath = ko.observable(undefined); self.webcam_bitrate = ko.observable(undefined); self.webcam_ffmpegThreads = ko.observable(undefined); + self.webcam_ffmpegVideoCodec = ko.observable(undefined); self.webcam_watermark = ko.observable(undefined); self.webcam_flipH = ko.observable(undefined); self.webcam_flipV = ko.observable(undefined); diff --git a/src/octoprint/templates/dialogs/settings/webcam.jinja2 b/src/octoprint/templates/dialogs/settings/webcam.jinja2 index 7a1330edc..f9b72b069 100644 --- a/src/octoprint/templates/dialogs/settings/webcam.jinja2 +++ b/src/octoprint/templates/dialogs/settings/webcam.jinja2 @@ -26,6 +26,7 @@ {% include "snippets/settings/webcam/ffmpegThreads.jinja2" %} {% include "snippets/settings/webcam/webcamSnapshotTimeout.jinja2" %} {% include "snippets/settings/webcam/webcamSnapshotSslValidation.jinja2" %} + {% include "snippets/settings/webcam/ffmpegVideoCodec.jinja2" %} diff --git a/src/octoprint/templates/snippets/settings/webcam/ffmpegVideoCodec.jinja2 b/src/octoprint/templates/snippets/settings/webcam/ffmpegVideoCodec.jinja2 new file mode 100644 index 000000000..c5f047a51 --- /dev/null +++ b/src/octoprint/templates/snippets/settings/webcam/ffmpegVideoCodec.jinja2 @@ -0,0 +1,6 @@ +
+ +
+ +
+
diff --git a/src/octoprint/timelapse.py b/src/octoprint/timelapse.py index 6b9ee6258..10b944682 100644 --- a/src/octoprint/timelapse.py +++ b/src/octoprint/timelapse.py @@ -16,7 +16,6 @@ try: except ImportError: import Queue as queue import requests -import re import octoprint.plugin import octoprint.util as util @@ -45,7 +44,8 @@ current_render_job = None # filename formats _capture_format = "{prefix}-%d.jpg" -_output_format = "{prefix}.mpg" +_capture_glob = "{prefix}-*.jpg" +_output_format = "{prefix}{postfix}.{extension}" # ffmpeg progress regexes _ffmpeg_duration_regex = re.compile("Duration: (\d{2}):(\d{2}):(\d{2})\.\d{2}") @@ -165,13 +165,13 @@ def render_unrendered_timelapse(name, gcode=None, postfix=None, fps=25): capture_dir = settings().getBaseFolder("timelapse_tmp") output_dir = settings().getBaseFolder("timelapse") threads = settings().get(["webcam", "ffmpegThreads"]) + videocodec = settings().get(["webcam", "ffmpegVideoCodec"]) job = TimelapseRenderJob(capture_dir, output_dir, name, postfix=postfix, - capture_format=_capture_format, - output_format=_output_format, fps=fps, threads=threads, + videocodec=videocodec, on_start=_create_render_start_handler(name, gcode=gcode), on_success=_create_render_success_handler(name, gcode=gcode), on_fail=_create_render_fail_handler(name, gcode=gcode), @@ -784,9 +784,9 @@ class TimelapseRenderJob(object): render_job_lock = threading.RLock() - def __init__(self, capture_dir, output_dir, prefix, postfix=None, capture_glob="{prefix}-*.jpg", - capture_format="{prefix}-%d.jpg", output_format="{prefix}{postfix}.mpg", fps=25, threads=1, - on_start=None, on_success=None, on_fail=None, on_always=None): + def __init__(self, capture_dir, output_dir, prefix, postfix=None, capture_glob=_capture_glob, + capture_format=_capture_format, output_format=_output_format, fps=25, threads=1, + videocodec="mpeg2video", on_start=None, on_success=None, on_fail=None, on_always=None): self._capture_dir = capture_dir self._output_dir = output_dir self._prefix = prefix @@ -796,6 +796,7 @@ class TimelapseRenderJob(object): self._output_format = output_format self._fps = fps self._threads = threads + self._videocodec = videocodec self._on_start = on_start self._on_success = on_success self._on_fail = on_fail @@ -824,12 +825,18 @@ class TimelapseRenderJob(object): self._logger.warn("Cannot create movie, path to ffmpeg or desired bitrate is unset") return + if self._videocodec == 'mpeg2video': + extension = "mpg" + else: + extension = "mp4" + input = os.path.join(self._capture_dir, self._capture_format.format(prefix=self._prefix, postfix=self._postfix if self._postfix is not None else "")) output = os.path.join(self._output_dir, self._output_format.format(prefix=self._prefix, - postfix=self._postfix if self._postfix is not None else "")) + postfix=self._postfix if self._postfix is not None else "", + extension=extension)) for i in range(4): if os.path.exists(input % i): @@ -853,7 +860,8 @@ class TimelapseRenderJob(object): # prepare ffmpeg command command_str = self._create_ffmpeg_command_string(ffmpeg, self._fps, bitrate, self._threads, input, output, - hflip=hflip, vflip=vflip, rotate=rotate, watermark=watermark) + self._videocodec, hflip=hflip, vflip=vflip, rotate=rotate, + watermark=watermark) self._logger.debug("Executing command: {}".format(command_str)) with self.render_job_lock: @@ -904,7 +912,7 @@ class TimelapseRenderJob(object): return (int(hours) * 60 + int(minutes)) * 60 + int(seconds) @classmethod - def _create_ffmpeg_command_string(cls, ffmpeg, fps, bitrate, threads, input, output, hflip=False, vflip=False, + def _create_ffmpeg_command_string(cls, ffmpeg, fps, bitrate, threads, input, output, videocodec, hflip=False, vflip=False, rotate=False, watermark=None, pixfmt="yuv420p"): """ Create ffmpeg command string based on input parameters. @@ -914,6 +922,7 @@ class TimelapseRenderJob(object): fps (int): Frames per second for output bitrate (str): Bitrate of output threads (int): Number of threads to use for rendering + videocodec (str): Videocodec to be used for encoding input (str): Absolute path to input files including file mask output (str): Absolute path to output file hflip (bool): Perform horizontal flip on input material. @@ -930,10 +939,16 @@ class TimelapseRenderJob(object): logger = logging.getLogger(__name__) + ### Not all players can handle non-mpeg2 in VOB format + if videocodec == "mpeg2video": + containerformat = "vob" + else: + containerformat = "mp4" + command = [ - ffmpeg, '-framerate', str(fps), '-i', '"{}"'.format(input), '-vcodec', 'mpeg2video', + ffmpeg, '-framerate', str(fps), '-i', '"{}"'.format(input), '-vcodec', videocodec, '-threads', str(threads), '-r', "25", '-y', '-b', str(bitrate), - '-f', 'vob'] + '-f', containerformat] filter_string = cls._create_filter_string(hflip=hflip, vflip=vflip, diff --git a/tests/timelapse/test_timelapse_renderjob.py b/tests/timelapse/test_timelapse_renderjob.py index e8617555c..c9f2a12e8 100644 --- a/tests/timelapse/test_timelapse_renderjob.py +++ b/tests/timelapse/test_timelapse_renderjob.py @@ -14,15 +14,19 @@ from octoprint.timelapse import TimelapseRenderJob class TimelapseRenderJobTest(unittest.TestCase): @data( - (("/path/to/ffmpeg", 25, "10000k", 1, "/path/to/input/files_%d.jpg", "/path/to/output.mpg"), + (("/path/to/ffmpeg", 25, "10000k", 1, "/path/to/input/files_%d.jpg", "/path/to/output.mpg", "mpeg2video"), dict(), '/path/to/ffmpeg -framerate 25 -i "/path/to/input/files_%d.jpg" -vcodec mpeg2video -threads 1 -r 25 -y -b 10000k -f vob -vf \'[in] format=yuv420p [out]\' "/path/to/output.mpg"'), - (("/path/to/ffmpeg", 25, "10000k", 1, "/path/to/input/files_%d.jpg", "/path/to/output.mpg"), + (("/path/to/ffmpeg", 25, "10000k", 1, "/path/to/input/files_%d.jpg", "/path/to/output.mp4", "libx264"), + dict(), + '/path/to/ffmpeg -framerate 25 -i "/path/to/input/files_%d.jpg" -vcodec libx264 -threads 1 -r 25 -y -b 10000k -f mp4 -vf \'[in] format=yuv420p [out]\' "/path/to/output.mp4"'), + + (("/path/to/ffmpeg", 25, "10000k", 1, "/path/to/input/files_%d.jpg", "/path/to/output.mpg", "mpeg2video"), dict(hflip=True), '/path/to/ffmpeg -framerate 25 -i "/path/to/input/files_%d.jpg" -vcodec mpeg2video -threads 1 -r 25 -y -b 10000k -f vob -vf \'[in] format=yuv420p,hflip [out]\' "/path/to/output.mpg"'), - (("/path/to/ffmpeg", 25, "20000k", 4, "/path/to/input/files_%d.jpg", "/path/to/output.mpg"), + (("/path/to/ffmpeg", 25, "20000k", 4, "/path/to/input/files_%d.jpg", "/path/to/output.mpg", "mpeg2video"), dict(rotate=True, watermark="/path/to/watermark.png"), '/path/to/ffmpeg -framerate 25 -i "/path/to/input/files_%d.jpg" -vcodec mpeg2video -threads 4 -r 25 -y -b 20000k -f vob -vf \'[in] format=yuv420p,transpose=2 [postprocessed]; movie=/path/to/watermark.png [wm]; [postprocessed][wm] overlay=10:main_h-overlay_h-10 [out]\' "/path/to/output.mpg"') ) diff --git a/translations/messages.pot b/translations/messages.pot index 5709bddc2..b6fe77163 100644 --- a/translations/messages.pot +++ b/translations/messages.pot @@ -4606,6 +4606,14 @@ msgstr "" msgid "FFMPEG threads" msgstr "" +#: src/octoprint/templates/snippets/settings/webcam/ffmpegVideoCodec.jinja2:1 +msgid "Videocodec uses for encoding" +msgstr "" + +#: src/octoprint/templates/snippets/settings/webcam/ffmpegVideoCodec.jinja2:2 +msgid "Videocodec" +msgstr "" + #: src/octoprint/templates/snippets/settings/webcam/watermark.jinja2:4 msgid "Enable OctoPrint watermark in timelapse movies" msgstr ""