This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ffmpeg.py
66 lines (56 loc) · 1.92 KB
/
ffmpeg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
import sys
import os
import ntpath
import subprocess
import time
import sys
import glob
import re
from natsort import natsort
reload(sys)
sys.setdefaultencoding('utf-8')
if getattr(sys, 'frozen', False):
FFMPEG_PATH = os.path.join(sys._MEIPASS, 'ffmpeg', 'ffmpeg.exe')
else:
FFMPEG_PATH = os.path.join(os.getcwd(), 'ffmpeg', 'ffmpeg.exe')
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def TsToMp4(Folder, OutPutFileName = ""):
Files = os.listdir(Folder)
Files = natsort(Files)
fullPathFiles = []
for i in Files:
path = os.path.join(Folder, i)
if os.path.isfile(path):
if i.endswith(".ts"):
fullPathFiles.append(path)
if fullPathFiles == []:
print "Thu muc %s khong co file .ts" % Folder
return
DirComplete = os.path.join(Folder[0:Folder.rfind("\\")], 'complete')
if not os.path.exists(DirComplete): os.mkdir(DirComplete)
DirLog = os.path.join(DirComplete, 'log')
if not os.path.exists(DirLog): os.mkdir(DirLog)
concatFile = os.path.join(DirComplete, 'concat.txt')
with open(concatFile, 'w') as f:
for i in fullPathFiles:
if os.path.isfile(i):
f.write("file '%s'\n" % i.replace("'", "'\\''"))
if OutPutFileName:
outputFile = os.path.join(DirComplete, OutPutFileName)
FileLog = os.path.join(DirLog, OutPutFileName + ".log")
else:
outputFile = os.path.join(DirComplete, 'output.mp4')
FileLog = os.path.join(DirLog, 'output.log')
args = [FFMPEG_PATH, '-f', 'concat', '-i', concatFile, '-c', 'copy', '-bsf:a', 'aac_adtstoasc', outputFile]
with open(FileLog, 'w') as f:
process = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
for line in iter(lambda: process.stdout.read(1), ''):
sys.stdout.write(line)
f.write(line.rstrip('\n'))
os.remove(concatFile)
def ConvertInFolder(Folder):
for folderName, subfolders, filenames in os.walk(Folder):
TsToMp4(folderName, folderName.split("\\")[-1] + ".mp4")