-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_alpha.py
96 lines (67 loc) · 2.49 KB
/
merge_alpha.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
import csv, os, shutil, argparse, errno,subprocess
import glob
def argparser_prepare():
class PrettyFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
max_help_position = 35
parser = argparse.ArgumentParser(description='merge video in folder without recompress',
formatter_class=PrettyFormatter)
parser.add_argument( 'path', type=str,
help='path to folder')
return parser
def determine_codec(filename):
command = ['ffprobe', '-show_format', '-pretty', '-show_entries', 'stream=codec_name', '-loglevel', 'quiet', filename]
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
ffmpeg_result = out.decode()
if 'codec_name=hevc' in ffmpeg_result:
return 'h265'
if 'codec_name=mjpeg' in ffmpeg_result:
return 'mjpeg'
return 'h264'
def get_dir_filenames(path,ext)->list:
files=list()
for root, dirs, files in os.walk(path):
for file in files:
if file.lower().endswith(ext.lower()):
files.append(os.path.join(root, file))
return files
def list2textfile(items:list,filepath:str):
with open(filepath, "w") as file:
for item in items:
file.write('file '+item)
parser = argparser_prepare()
args = parser.parse_args()
folder = args.path
if not(os.path.isdir(folder)):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), folder)
files = get_dir_filenames(folder,'.avi')
assert len(files)>0
print(files)
source_codec = determine_codec(files[0])
listfile='list.txt'
list2textfile(files,listfile)
dstfilebase = os.path.splitext(files[0])[0]+'-merge'
if source_codec == 'mjpeg':
dst=dstfilebase+'.avi'
cmd = ['ffmpeg','-f','concat','safe',0,'-i',listfile,'-c','copy',dst]
print(' '.join(cmd))
subprocess.run(cmd)
quit()
os.unlink(listfile)
print('finished')
print(dst)
else:
'''
#create dir if not exists
mkdir -p $DST
#convert to mpeg transport stream
for f in $1/*.[mM][pPKk][4vV]; do ffmpeg -y -hide_banner -loglevel error -i $f -c copy -f mpegts $DST/$(basename $f| cut -d. -f1).ts; done
LIST='concat:'
for f in $DST/*.ts; do LIST+="$f|" ; done
#merge
ffmpeg -y -hide_banner -loglevel error -i "$LIST" -c copy -bsf:a aac_adtstoasc $DST/$DATE.mp4
rm -rf $DST/*.ts
rm -rf $DST/list.txt
'''