]> git.parisson.com Git - telecaster-server.git/commitdiff
add va-api based hevc transcoder
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 13 Jul 2023 09:02:20 +0000 (11:02 +0200)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 13 Jul 2023 09:02:20 +0000 (11:02 +0200)
src/home/telecaster/bin/py/transcode-vaapi.py [new file with mode: 0644]

diff --git a/src/home/telecaster/bin/py/transcode-vaapi.py b/src/home/telecaster/bin/py/transcode-vaapi.py
new file mode 100644 (file)
index 0000000..d34ec89
--- /dev/null
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+import os, sys, string
+import logging
+import datetime
+
+
+class Logger:
+    """A logging object"""
+
+    def __init__(self, file):
+        self.logger = logging.getLogger('myapp')
+        self.hdlr = logging.FileHandler(file)
+        self.formatter = logging.Formatter('%(asctime)s %(message)s')
+        self.hdlr.setFormatter(self.formatter)
+        self.logger.addHandler(self.hdlr)
+        self.logger.setLevel(logging.INFO)
+
+
+class Transcode(object):
+    """docstring for Transcode"""
+
+    source_formats = ['MOV',]
+    output_formats = {
+                   'mp4-hevc' :
+                    {"ext": "mp4",
+                     "opt_in": "-hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi",
+                     "opt_out": "-vf 'scale_vaapi=format=p010' -c:v hevc_vaapi -profile 2 -b:v 15M",
+                    }
+                  }
+
+    def __init__(self, args):
+        self.args = args
+        self.log_file = args[-1]
+        self.root_dir = args[-2]
+        self.logger = Logger(self.log_file)
+
+    def get_ext_in_dir(self, extension, root):
+        files = os.listdir(root)
+        exts = []
+        for f in files:
+            name, ext = os.path.splitext(f)
+            ext = ext[1:]
+            if not ext in exts:
+                exts.append(ext)
+        return extension in exts
+
+    def run(self):
+        for root, dirs, files in os.walk(self.root_dir):
+            for file in files:
+                input_path = os.path.abspath(root + os.sep + file)
+                name, ext = os.path.splitext(file)
+                ext = ext[1:]
+                if ext in self.source_formats:
+                    for output_format in self.output_formats:
+                        output_format = self.output_formats[output_format]
+                        output_file = name + '.' + output_format["ext"]
+                        output_path = os.path.abspath(root + os.sep + output_file)
+                        local_files = os.listdir(root)
+                        print(output_path)
+                        if not (output_file in local_files or self.get_ext_in_dir(output_format["ext"], root)) or '--force' in self.args:
+                            command = 'ffmpeg ' + output_format['opt_in'] + ' -i "' + input_path + '" ' + \
+                                output_format['opt_out'] + ' -y "' + output_path + '"'
+                            self.logger.logger.info(command)
+                            if not '--dry-run' in self.args:
+                                os.system(command)
+                                print(command)
+
+
+if __name__ == '__main__':
+    t = Transcode(sys.argv[1:])
+    t.run()