-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmakeMP4.py
119 lines (102 loc) · 4.26 KB
/
makeMP4.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# coding=utf-8
# Copyright 2024- Mark McIntyre
from __future__ import print_function
import os
import sys
import platform
import shutil
import subprocess
import time
import errno
import logging
from distutils.spawn import find_executable
from FF_bin_suite import readFF, buildFF, add_text, saveImage
if sys.version_info[0] < 3:
import tkMessageBox
else:
import tkinter.messagebox as tkMessageBox
log = logging.getLogger("CMN_binViewer")
run_dir = os.path.abspath(".")
def makeMP4(FF_input, start_frame, end_frame, ff_dir, mp4_name='', deinterlace=True, annotate='', fps=25, FF_next=None, end_next=None, data_type=1,
ffmpeg_path=''):
if platform.system() == 'Windows':
if ffmpeg_path == '':
ffmpeg_path = find_executable('ffmpeg.exe')
if ffmpeg_path == 'ffmpeg.exe':
ffmpeg_path = os.path.join(run_dir, ffmpeg_path)
ffmpegisfile = os.path.isfile(ffmpeg_path.replace('"',''))
if ffmpeg_path is None or ffmpegisfile is False:
tkMessageBox.showinfo("Alert", "ffmpeg.exe not found!")
return False
out_dir = os.path.split(mp4_name)[0]
tmp_dir = out_dir + '/tmp_img_dir'
if os.path.isdir(tmp_dir):
shutil.rmtree(tmp_dir)
mkdir_safe(tmp_dir)
log.info('making mp4')
cwd = os.getcwd()
os.chdir(ff_dir)
ffBinRead = readFF(FF_input, datatype=data_type)
for k in range(start_frame, end_frame+1):
img_array = buildFF(ffBinRead, k, videoFlag = True)
if annotate != '':
img_array = add_text(img_array, annotate)
_ = saveFrame(img_array, k, tmp_dir, FF_input)
print('.', end='')
if FF_next:
ffBinRead = readFF(FF_next, datatype=data_type)
for k in range(0, end_next+1):
img_array = buildFF(ffBinRead, k, videoFlag = True)
if annotate != '':
img_array = add_text(img_array, annotate)
_ = saveFrame(img_array, k+end_frame+1, tmp_dir, FF_input)
print('.', end='')
print(' ')
tmp_img_patt = os.path.abspath(os.path.join(tmp_dir, FF_input+"_%03d.png"))
# If running on Windows, use ffmpeg.exe
if platform.system() == 'Windows':
# Construct the ecommand for ffmpeg
com = '"' + ffmpeg_path + '" -hide_banner -loglevel error -pix_fmt yuv420p -y -f image2 -pattern_type sequence -start_number ' + str(start_frame) + ' -i ' + tmp_img_patt +' ' + mp4_name
else:
# If avconv is not found, try using ffmpeg
software_name = "avconv"
if os.system(software_name + " --help > /dev/null"):
software_name = "ffmpeg"
# Construct the ecommand for ffmpeg
com = software_name + " -hide_banner -loglevel error -pix_fmt yuv420p -y -f image2 -pattern_type sequence -start_number " + str(start_frame) + " -i " + tmp_img_patt +" " + mp4_name
else:
com = "cd " + tmp_dir + ";" \
+ software_name + " -v quiet -r 30 -y -start_number " + str(start_frame) + " -i " + tmp_img_patt \
+ " -vcodec libx264 -pix_fmt yuv420p -crf 25 -movflags faststart -g 15 -vf \"hqdn3d=4:3:6:4.5,lutyuv=y=gammaval(0.97)\" " \
+ mp4_name
log.info(com)
subprocess.call(com, shell=True, cwd=out_dir)
#Delete temporary directory and files inside
#print(tmp_dir)
if os.path.isdir(tmp_dir):
#print('deleting tempdir')
try:
shutil.rmtree(tmp_dir)
except:
# may occasionally fail due to ffmpeg thread still terminating
# so catch this and wait a bit
time.sleep(2)
shutil.rmtree(tmp_dir)
log.info('done')
os.chdir(cwd)
return True
def saveFrame(frame, frame_no, out_dir, file_name):
file_name_saving = file_name + '_{:03d}'.format(frame_no) + '.png'
out_path = os.path.join(out_dir, file_name_saving)
saveImage(frame, out_path, False)
return file_name_saving
def mkdir_safe(path):
""" Makes a directory and handles all errors.
"""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST:
pass
else:
raise