简体   繁体   English

FFMPEG优化重新编码mkv到mp4 hardsub

[英]FFMPEG optimisation re-encoding mkv to mp4 hardsub

I've wrote a program with python, ffmpeg and the linux terminal.我用 python、ffmpeg 和 linux 终端编写了一个程序。 It works, but it's very slow... And i want optimise my code, but i don't what i could do for do it.它可以工作,但速度很慢......我想优化我的代码,但我无法做到这一点。 My code:我的代码:

import os

def extractSub():
    """
    This function extract the sub of mkv file and put it in mp4Folder folder.
    """
    dirLocate = "./mkvFolder/"
    for filename in os.listdir(dirLocate):
        if(filename.endswith(".mkv")):
            os.system("ffmpeg -i {0}  -map 0:s:0 {0}.ass".format(dirLocate + filename))

def burnSub():
    """
    This function burn/hardsub the sub extract with extractSub() function and put in the final mp4 file.
    """
    dirLocate = "./mkvFolder/"
    mp4Locate = "./mp4Folder/"
    for filename in os.listdir(dirLocate):
        if(filename.endswith(".mkv")):
            os.system("ffmpeg -i {0} subtitles={0} {1}.mp4".format(dirLocate + filename, mp4Locate + filename[0:-4]))

start = time.time()

extractSub()
burnSub()

print("The time used to execute this is given below")

end = time.time()

I hope that I gave enough detail...我希望我提供了足够的细节......

optimized code and a broad exception clause优化的代码和广泛的例外条款

import os
import time
import traceback

dir_locate = input("the directory to the subtitles file location:")
print(dir_locate)
mp4_locate = input("the directory to the mp4 file location")
print(mp4_locate)


def extractsub():
    """
    This function extract the sub of mkv file and put it in mp4Folder folder.
    """
    for filename in os.listdir(dir_locate):
        if filename.endswith(".mkv"):
            os.system("ffmpeg -i {0}  -map 0:s:0 {0}.ass".format(dir_locate + filename))


def burnsub():
    """
    This function burn/hardsub the sub extract with extractSub() function and put in the final mp4 file.
    """
    for filename in os.listdir(dir_locate):
        if filename.endswith(".mkv"):
            os.system("ffmpeg -i {0} subtitles={0} {1}.mp4".format(dir_locate + filename, mp4_locate + filename[0:-4]))


start = time.time()
print("The time used to execute this is given below")
end = time.time()

if __name__ == '__main__':
    try:
        extractsub()
        burnsub()
    except KeyboardInterrupt:
        print('\nInterrupted')
    except Exception:
        with open("log.txt", "w") as log:
            traceback.print_exc(file=log)
            print('\nError is printed to log.txt')

or或者

import os
import time
import traceback

dir_locate = "./mkvFolder/"
mp4_locate = "./mp4Folder/"


def extractsub():
    """
    This function extract the sub of mkv file and put it in mp4Folder folder.
    """
    for filename in os.listdir(dir_locate):
        if filename.endswith(".mkv"):
            os.system("ffmpeg -i {0}  -map 0:s:0 {0}.ass".format(dir_locate + filename))


def burnsub():
    """
    This function burn/hardsub the sub extract with extractSub() function and put in the final mp4 file.
    """
    for filename in os.listdir(dir_locate):
        if filename.endswith(".mkv"):
            os.system("ffmpeg -i {0} subtitles={0} {1}.mp4".format(dir_locate + filename, mp4_locate + filename[0:-4]))


start = time.time()
print("The time used to execute this is given below")
end = time.time()

if __name__ == '__main__':
    try:
        extractsub()
        burnsub()
    except KeyboardInterrupt:
        print('\nInterrupted')
    except Exception:
        with open("log.txt", "w") as log:
            traceback.print_exc(file=log)
            print('\nError is printed to log.txt')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM