Advertisement
savsanta

encode.py

Oct 14th, 2018
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. """
  2. This python script encodes all files that have the extension mkv in the current
  3. working directory.
  4.  
  5. Sources:
  6.    http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
  7. """
  8. import subprocess, os
  9.  
  10. #-------------------------------------------------------------------------------
  11. # CONFIGURABLE SETTINGS
  12. #-------------------------------------------------------------------------------
  13.  
  14. # controls the quality of the encode
  15. CRF_VALUE = '25'
  16.  
  17. # h.264 profile
  18. PROFILE = 'high'
  19.  
  20. # encoding speed:compression ratio
  21. PRESET = 'slow'
  22.  
  23. # path to ffmpeg bin
  24. FFMPEG_PATH = '/usr/bin/ffmpeg'
  25.  
  26. # font dir
  27. FONT_DIR = '/var/www/.fonts'
  28.  
  29. #-------------------------------------------------------------------------------
  30. # encoding script
  31. #-------------------------------------------------------------------------------
  32.  
  33. def process():
  34.     cwd = os.getcwd()
  35.  
  36.     # get a list of files that have the extension mkv
  37.     filelist = filter(lambda f: f.split('.')[-1] == 'mp4', os.listdir(cwd))
  38.     filelist = sorted(filelist)
  39.  
  40.     # encode each file
  41.     for file in filelist:
  42.         encode(file)
  43.  
  44.  
  45. def encode(file):
  46.     name = ''.join(file.split('.')[:-1])
  47.     #subtitles = 'temp.ass'.format(name)
  48.     output = '{}.mp4'.format(name)
  49.  
  50.     try:
  51.         command = [
  52.             FFMPEG_PATH, '-i', file,
  53.             '-c:v', 'libx264', '-preset', PRESET, '-profile:v', PROFILE, '-crf', CRF_VALUE,
  54.         ]
  55.  
  56.         # create a folder called attachments and symlink it to FONT_DIR
  57.         # extract attachments
  58.         subprocess.call(['mkdir', 'attachments'])
  59.         subprocess.call(['rm', '-f', FONT_DIR])
  60.         subprocess.call(['ln', '-s', '{}/attachments'.format(os.getcwd()), FONT_DIR])
  61.  
  62.         os.chdir('attachments')
  63.  
  64.         subprocess.call([FFMPEG_PATH, '-dump_attachment:t', '', '-i', '../{}'.format(file)])
  65.  
  66.         os.chdir('..')
  67.  
  68.         # extract ass subtitles and and subtitle into command
  69. #        subprocess.call([FFMPEG_PATH, '-i', file, subtitles])
  70. #        if os.path.getsize(subtitles) > 0:
  71. #            command += ['-vf', 'ass={}'.format(subtitles)]
  72.  
  73.         output = "/home/ubuntu/fin2/" + output
  74.  
  75.         command += ['-c:a', 'copy']             # if audio is using AAC copy it - else encode it
  76.         command += ['-threads', '8', output]    # add threads and output
  77.         subprocess.call(command)                # encode the video!
  78.  
  79.     finally:
  80.         # always cleanup even if there are errors
  81.         subprocess.call(['rm', '-fr', 'attachments'])
  82.         subprocess.call(['rm', '-f', FONT_DIR])
  83. #        subprocess.call(['rm', '-f', subtitles])
  84.  
  85.  
  86. if __name__ == "__main__":
  87.     process()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement