]> git.parisson.com Git - telecaster-server.git/commitdiff
add remux and transcode scripts
authorGuillaume Pellerin <yomguy@parisson.com>
Thu, 22 Dec 2022 10:35:43 +0000 (11:35 +0100)
committerGuillaume Pellerin <yomguy@parisson.com>
Thu, 22 Dec 2022 10:35:43 +0000 (11:35 +0100)
bin/create_thumbs.py [new file with mode: 0644]
bin/remux_fix_media.py [new file with mode: 0644]
bin/transcode.py [new file with mode: 0644]
bin/transcode_nv.py [new file with mode: 0644]
bin/transcode_nv2.py [new file with mode: 0644]

diff --git a/bin/create_thumbs.py b/bin/create_thumbs.py
new file mode 100644 (file)
index 0000000..dc3fd20
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/python
+
+import os, sys, string
+import logging
+
+class Logger:
+    """A logging object"""
+
+    def __init__(self, file):
+        self.logger = logging.getLogger('myapp')
+        self.hdlr = logging.FileHandler(file)
+        self.formatter = logging.Formatter('%(message)s')
+        self.hdlr.setFormatter(self.formatter)
+        self.logger.addHandler(self.hdlr)
+        self.logger.setLevel(logging.INFO)
+
+log_file = 'thumbs.log'
+logger = Logger(log_file)
+root_dir = sys.argv[-1]
+args = sys.argv[1:-1]
+source_format = 'webm'
+done = []
+preview_tc = '00:00:05'
+
+if os.path.exists(log_file):
+    f = open(log_file, 'r')
+    for line in f.readlines():
+        done.append(line[:-1])
+    f.close()
+
+for root, dirs, files in os.walk(root_dir):
+    for file in files:
+        path = os.path.abspath(root + os.sep + file)
+        name, ext = os.path.splitext(file)
+        if ext[1:] == source_format:
+            dest = os.path.abspath(root + os.sep + name + '.png')
+            if not dest in done or '--force' in args:
+                command = 'ffmpeg -ss '+ preview_tc + ' -i ' + path + '  -y ' + dest
+                os.system(command)
+                logger.logger.info(dest)
+
+print "DONE!"
diff --git a/bin/remux_fix_media.py b/bin/remux_fix_media.py
new file mode 100644 (file)
index 0000000..9784c40
--- /dev/null
@@ -0,0 +1,113 @@
+#!/usr/bin/python
+
+import os, sys, psutil
+import datetime
+from ebml.utils.ebml_data import *
+
+class FixCheckMedia(object):
+
+    def __init__(self, dir, tmp_dir):
+        self.dir = dir
+        self.tmp_dir = tmp_dir
+        if not os.path.exists(self.tmp_dir):
+            os.makedirs(self.tmp_dir)
+
+    def process(self):
+        webm_fixed_log = 'webm.fixed'
+        webm_tofix_log = 'webm.tofix'
+        mp3_fixed_log = 'mp3.fixed'
+        mp3_tofix_log = 'mp3.tofix'
+
+        for root, dirs, files in os.walk(self.dir):
+            for filename in files:
+                source = root + os.sep + filename
+                name = os.path.splitext(filename)[0]
+                ext = os.path.splitext(filename)[1][1:]
+
+                if (ext == 'webm' or ext == 'mp4') and os.path.getsize(source):
+                    dir_files = os.listdir(root)
+
+                    if not webm_fixed_log in dir_files:
+                        print source
+                        self.fix_webm(source)
+                        f = open(root + os.sep + webm_fixed_log, 'w')
+                        f.close()
+                        if os.path.exists(root + os.sep + webm_tofix_log):
+                            os.remove(root + os.sep + webm_tofix_log)
+
+                    if mp3_tofix_log in dir_files or not mp3_fixed_log in dir_files:
+                        for file in dir_files:
+                            dest_ext = os.path.splitext(file)[1][1:]
+                            if dest_ext == 'mp3':
+                                dest = root + os.sep + file
+                                print dest
+                                self.fix_mp3(source, dest)
+                                f = open(root + os.sep + mp3_fixed_log, 'w')
+                                f.close()
+                                if os.path.exists(root + os.sep + mp3_tofix_log):
+                                    os.remove(root + os.sep + mp3_tofix_log)
+                                #break
+
+
+    def hard_fix_webm(self, path):
+        try:
+            tmp_file = self.tmp_dir + 'out.webm '
+            command = 'ffmpeg -loglevel 0 -i "'+ path + '" -vcodec libvpx -vb 500k -acodec libvorbis -aq 7 -f webm -y "' + tmp_file + '" > /dev/null'
+            print command
+            os.system(command)
+            command = 'mv '  + tmp_file + path
+            os.system(command)
+        except:
+            pass
+
+
+    def fix_webm(self, path):
+        try: 
+            tmp_file = self.tmp_dir + 'out.webm'
+            command = '/usr/local/bin/ffmpeg -loglevel 0 -i "' + path + '" -vcodec copy -acodec copy -f webm -y "' + tmp_file + '" > /dev/null'
+            print command
+            os.system(command)
+            #ebml_obj = EBMLData(tmp_file)
+            #offset = ebml_obj.get_first_cluster_seconds()
+            command = '/usr/local/bin/ffmpeg -loglevel 0 -i "' + tmp_file + '" -vcodec copy -acodec copy -f webm -y "' + path + '" > /dev/null'
+            print command
+            os.system(command)
+        except:
+            pass
+
+    def fix_mp3(self, source, path):
+        try:
+            command = '/usr/local/bin/ffmpeg -loglevel 0 -i "'+ source + '" -vn -aq 6 -y "' + path + '" > /dev/null'
+            print command
+            os.system(command)
+        except:
+            pass
+
+def get_pids(name, args=None):
+    """Get a process pid filtered by arguments and uid"""
+    pids = []
+    for proc in psutil.process_iter():
+        if proc.cmdline:
+            if name == proc.name:
+                if args:
+                    if args in proc.cmdline:
+                        pids.append(proc.pid)
+                else:
+                    pids.append(proc.pid)
+    return pids
+
+dir = sys.argv[-2]
+tmp_dir = sys.argv[-1]
+
+path =  os.path.abspath(__file__)
+pids = get_pids('python2.6',args=path)
+
+print datetime.datetime.now()
+if len(pids) <= 1:
+    print 'starting process...'
+    f = FixCheckMedia(dir, tmp_dir)
+    f.process()
+    print 'process finished.\n'
+else:
+    print 'already started !\n'
+
diff --git a/bin/transcode.py b/bin/transcode.py
new file mode 100644 (file)
index 0000000..12719e6
--- /dev/null
@@ -0,0 +1,76 @@
+#!/usr/bin/python
+
+import os, sys, string
+import logging
+
+
+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 TelemetaTranscode(object):
+    """docstring for TelemetaTranscode"""
+
+    threads = 4
+    source_formats = ['webm', 'mp4']
+    dest_formats = {
+                   'mp3' : '-vn -acodec libmp3lame -aq 6',
+                   'ogg' : '-vn -acodec libvorbis -aq 6',
+                   'mp4' : '-vcodec libx264 -threads ' + str(threads) + \
+                           ' -c:v libx264 -crf 17 -maxrate 1100k -bufsize 1835k -acodec aac -strict -2 -ab 96k',
+                   'png' : '-ss 0:0:10',
+                   'webm' : '-vcodec libvpx -threads ' + str(threads) + \
+                           ' -c:v libvpx -crf 17 -b:v 1100k',
+                  }
+
+
+    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:
+                path = os.path.abspath(root + os.sep + file)
+                name, ext = os.path.splitext(file)
+                ext = ext[1:]
+                if ext in self.source_formats:
+                    for format, ffmpeg_args in self.dest_formats.iteritems():
+                        local_file = name + '.' + format
+                        dest = os.path.abspath(root + os.sep + local_file)
+                        local_files = os.listdir(root)
+                        if not (local_file in local_files or self.get_ext_in_dir(format, root)) or '--force' in self.args:
+                            if ext == 'webm' and format == 'ogg':
+                                ffmpeg_args = '-vn -acodec copy'
+                            command = 'ffmpeg -loglevel 0 -i "' + path + '" ' + ffmpeg_args + ' -y "' + dest + '"'
+                            self.logger.logger.info(command)
+                            if not '--dry-run' in self.args:
+                                os.system(command)
+                            else:
+                                print command
+
+
+if __name__ == '__main__':
+    t = TelemetaTranscode(sys.argv[1:])
+    t.run()
diff --git a/bin/transcode_nv.py b/bin/transcode_nv.py
new file mode 100644 (file)
index 0000000..b832ae5
--- /dev/null
@@ -0,0 +1,78 @@
+#!/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 TelemetaTranscode(object):
+    """docstring for TelemetaTranscode"""
+
+    threads = 4
+    source_formats = ['webm', 'mp4']
+    dest_formats = {
+                   'mp3' : '-vn -acodec libmp3lame -aq 6',
+                   'mp4' : '-c:v h264_nvenc -maxrate 1100k -c:a aac -b:a 128k',
+                   'png' : '-ss 0:0:10',
+                  }
+
+    date_limit = datetime.datetime(year=2021, month=6, day=24)
+
+    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:
+                path = os.path.abspath(root + os.sep + file)
+                name, ext = os.path.splitext(file)
+                ext = ext[1:]
+                date_dir = datetime.datetime.fromtimestamp(os.path.getmtime(path))
+                if ext in self.source_formats and date_dir > self.date_limit:
+                    print(date_dir)
+                    for format, ffmpeg_args in self.dest_formats.iteritems():
+                        local_file = name + '.' + format
+                        dest = os.path.abspath(root + os.sep + local_file)
+                        local_files = os.listdir(root)
+                        if not (local_file in local_files or self.get_ext_in_dir(format, root)) or '--force' in self.args:
+                            if ext == 'webm' and format == 'ogg':
+                                ffmpeg_args = '-vn -acodec copy'
+                            if format == 'png':
+                                command = '/usr/local/bin/ffmpeg -loglevel 0 ' + ffmpeg_args + ' -i "' + path + '" -frames:v 1 -y "' + dest + '"'
+                            else: 
+                                command = '/usr/local/bin/ffmpeg -loglevel 0 -i "' + path + '" ' + ffmpeg_args + ' -y "' + dest + '"'
+                            self.logger.logger.info(command)
+                            if not '--dry-run' in self.args:
+                                os.system(command)
+                                print(command)
+
+
+if __name__ == '__main__':
+    t = TelemetaTranscode(sys.argv[1:])
+    t.run()
diff --git a/bin/transcode_nv2.py b/bin/transcode_nv2.py
new file mode 100644 (file)
index 0000000..40b8faa
--- /dev/null
@@ -0,0 +1,73 @@
+#!/usr/bin/python
+
+import os, sys, string
+import logging
+
+
+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 TelemetaTranscode(object):
+    """docstring for TelemetaTranscode"""
+
+    threads = 4
+    source_formats = ['webm',]
+    dest_formats = {
+                   'mp3' : '-vn -acodec libmp3lame -aq 3',
+                   'mp4' : '-c:v h264_nvenc -maxrate 1100k -c:a aac -b:a 96k',
+                   'png' : '-ss 0:0:10',
+                  }
+
+    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:
+                path = os.path.abspath(root + os.sep + file)
+                name, ext = os.path.splitext(file)
+                ext = ext[1:]
+                if ext in self.source_formats:
+                    for format, ffmpeg_args in self.dest_formats.iteritems():
+                        local_file = name + '.' + format
+                        dest = os.path.abspath(root + os.sep + local_file)
+                        local_files = os.listdir(root)
+                        if not (local_file in local_files or self.get_ext_in_dir(format, root)) or '--force' in self.args:
+                            if ext == 'webm' and format == 'ogg':
+                                ffmpeg_args = '-vn -acodec copy'
+                            if format == 'png':
+                                command = 'ffmpeg -loglevel 0 ' + ffmpeg_args + ' -i "' + path + '" -frames:v 1 -y "' + dest + '"'
+                            else: 
+                                command = 'ffmpeg -loglevel 0 -i "' + path + '" ' + ffmpeg_args + ' -y "' + dest + '"'
+                            self.logger.logger.info(command)
+                            if not '--dry-run' in self.args:
+                                os.system(command)
+                                print(command)
+
+
+if __name__ == '__main__':
+    t = TelemetaTranscode(sys.argv[1:])
+    t.run()