Created
June 11, 2015 08:34
-
-
Save hrsano645/133b4c6c932e59e7d763 to your computer and use it in GitHub Desktop.
Convert MPEG TS file to Mp4 file using Handbrake CLI , Preset Android Tablet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from __future__ import division, print_function, absolute_import, unicode_literals | |
import os | |
import sys | |
import itertools | |
import subprocess | |
def main(): | |
# ファイル一欄を作る | |
walkgen_list = list(os.walk(sys.argv[1])) | |
filelist = [] | |
for dpath, dname, dfilename in walkgen_list: | |
abs_dpath = os.path.abspath(dpath) | |
# print(os.path.join(abs_dpath)) | |
for fname in dfilename: | |
if os.path.splitext(fname)[1] in (".mp4", ".ts"): | |
filelist.append(os.path.join(abs_dpath, fname)) | |
# 同じファイルベースネームでmp4とtsが対になっているか調べて、tsのみのファイルを上げる | |
none_convert_ts = [] | |
grouping_file_basename = itertools.groupby(filelist, lambda x: os.path.splitext(x)[0]) | |
for k, g in grouping_file_basename: | |
list_g = list(g) | |
if len(list_g) == 1: | |
none_convert_ts.append(list_g[0]) | |
# tsファイルをhandbrakecliに渡す | |
for tsfilename in none_convert_ts: | |
(tsfilebase, fext) = os.path.splitext(tsfilename) | |
mp4_path = "{}{}".format(tsfilebase, ".mp4") | |
try: | |
subprocess.check_call( | |
"HandBrakeCLI -i {} -o {} --preset=\"Android Tablet\"".format(tsfilename, mp4_path), shell=True) | |
print("convertfile:{} next file".format(tsfilename)) | |
except subprocess.CalledProcessError: | |
print("CalledProcessError!!, next file") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment