1
- #RIFE AutomationTool Python Ver1.0.1 2021/11/30
2
-
3
- def mainfunc (douga ,gui_hantei ,rifemode ,bairitsu ,bitto ,gazou ,hinsitsu ,sureddo ,kodekku ):
1
+ #RIFE AutomationTool Python Ver1.1 2022/4
4
2
3
+ def mainfunc (video ,gui_mode ,GUIsetting ):
5
4
import os
6
5
import subprocess as sp
7
- import math
8
- import glob
9
- import csv
10
6
import configparser
7
+ import shutil
8
+
9
+ os .chdir (os .path .dirname (__file__ ))
10
+
11
+ class configreader :
12
+ def __init__ (self ,configdata ):
13
+ self .codec = str (configdata .get ("CODEC" ))
14
+ self .bitrate = str (configdata .get ("BITRATE" ))
15
+ self .rifever = str (configdata .get ("RIFEVER" ))
16
+ self .rifeusage = str (configdata .get ("RIFEUSAGE" ))
17
+ self .interpolation = str (configdata .get ("INTERPOLATION" ))
18
+ self .picture = str (configdata .get ("PICTURE" ))
19
+ if self .picture == "png" :
20
+ self .jpgquality = ""
21
+ else :
22
+ self .jpgquality = str (configdata .get ("JPGQUALITY" ))
23
+ self .interpolation_menu = bool (int (configdata .get ("INTERPOLATION_M" )))
24
+ self .defaultdelete = bool (int (configdata .get ("DELETE" )))
25
+ self .delete_menu = bool (int (configdata .get ("DELETE_M" )))
26
+ self .end_menu = bool (int (configdata .get ("END_M" )))
27
+
28
+ class configgui :
29
+ def __init__ (self ,GUIsetting ):
30
+ self .codec = GUIsetting [0 ]
31
+ self .bitrate = GUIsetting [1 ]
32
+ self .rifever = GUIsetting [2 ]
33
+ self .rifeusage = GUIsetting [3 ]
34
+ self .interpolation = GUIsetting [4 ]
35
+ self .picture = GUIsetting [5 ]
36
+ if self .picture == "png" :
37
+ self .jpgquality = ""
38
+ else :
39
+ self .jpgquality = GUIsetting [6 ]
40
+ self .interpolation_menu = False
41
+ self .defaultdelete = True
42
+ self .delete_menu = False
43
+ self .end_menu = False
44
+
45
+ if gui_mode == True :
46
+ setting = configgui (GUIsetting )
47
+ else :
48
+ configdata = configparser .ConfigParser ()
49
+ configdata .read ("RATconfig.ini" , encoding = "utf-8" )
50
+ configdata = configdata ["DEFAULT" ]
51
+ setting = configreader (configdata )
11
52
12
-
13
- P_filepath = os .path .dirname (__file__ )
14
- os .chdir (P_filepath )
15
-
16
-
17
- config_ini = configparser .ConfigParser ()
18
- config_ini .read ("RATconfig.ini" , encoding = "utf-8" )
19
-
20
- read_default = config_ini ["DEFAULT" ]
21
-
22
-
23
- #以下、初期設定
24
- P_codec = str (read_default .get ("CODEC" )) #動画エンコードに使うコーデック、使用環境に応じて決定
25
- P_picture = str (read_default .get ("PICTURE" )) #処理中に使う画像の拡張子
26
- P_jpgquality = str (read_default .get ("JPGQUALITY" )) #jpg処理時に使用する画像品質の設定
27
- P_bitrate = str (read_default .get ("BITRATE" )) #動画エンコード後のビットレート
28
- P_rifemode = str (read_default .get ("RIFEVER" )) #RIFEの処理モード
29
- P_rifeusage = str (read_default .get ("RIFEUSAGE" )) #RIFEの補完時の並列処理数
30
- P_interpolation = str (read_default .get ("INTERPOLATION" )) #補完の倍率(初期値)
31
-
32
- P_end_menu = bool (int (read_default .get ("END_M" ))) #終了時に終了したことを伝えるか、str型ではなくbool型で
33
- P_delete_menu = bool (int (read_default .get ("DELETE_M" ))) #データを削除するかしないかのチェックをするか、str型ではなくbool型で
34
- P_defaultdelete = bool (int (read_default .get ("DELETE" ))) #チェックをスルーした際自動で削除するか、str型ではなくbool型で
35
- P_interpolation_menu = bool (int (read_default .get ("INTERPOLATION_M" ))) #倍率を確認するか、str型ではなくbool型で
36
- P_guimode = gui_hantei #GUIで起動しているかCUIで起動しているか
37
-
38
-
39
- #P_framerate #元の動画のフレームレート
40
- #P_Aframerate #元の動画のフレームレート*補完の倍率
41
- # P_base 取り込む元のファイル(相対パス)
42
- #P_base_filename 元のファイルの名前
43
- # P_base_less 取り込む元のファイルの拡張子を抜いたもの
44
- # P_menu YesかNoか記憶
45
- # P_interpolation_int 補完倍率のint型
46
- # directory_number_flt 補完番号とディレクトリの番号を一致させる途中経過
47
- # directory_number ディレクトリの番号
48
- # P_check 画像ファイルに他のファイルが残っているか
49
- # P_check_inside 音声ストリームが動画に含まれており、それが音声ファイル化されたか
50
- # P_check_wav 動画ファイルとは別にwav形式の音声ファイルが追加されたか
51
- # P_check_m4a 動画ファイルとは別にm4a形式の音声ファイルが追加されたか
52
- # delete_list 作業ファイル削除時に作成されたデータの一覧を取得
53
-
54
-
55
- #===以下 csvmaker内 ===
56
- #framerate_csv,framerate_csv_d,framerate_base_framerate.cutはすべてフレームレート関係(CSV->str)
57
- #======
58
-
59
-
60
- #directory内は[元動画の画像変換後フォルダ,2倍補完後の画像フォルダ,4倍補完後の画像フォルダ,音声フォルダ,一時フォルダ]、デフォルトではメインファイル直下
61
53
directory = ["video_data" ,"video_data_2x" ,"video_data_4x" ,"audio_data" ,"temp" ]
54
+ for name in directory :
55
+ if setting .defaultdelete == True :
56
+ shutil .rmtree (name , ignore_errors = True )
57
+ os .makedirs (name , exist_ok = True )
62
58
63
- if P_guimode == True :
64
- P_bitrate = bitto
65
-
66
- if rifemode == "無印" :
67
- P_rifemode = ""
68
- else :
69
- P_rifemode = "-" + rifemode
70
-
71
- P_base = douga
72
-
73
- P_interpolation = bairitsu
74
-
75
- P_picture = gazou
76
-
77
- P_jpgquality = hinsitsu
78
-
79
- P_rifeusage = sureddo
80
-
81
- P_codec = kodekku
82
-
83
-
84
- def interpolationchecker (b ):
85
- if b == "2" or b == "4" :
86
- return True
87
- else :
88
- return False
89
-
90
- def csvmaker (c ,d ,e ):
91
- sp .run (f"ffprobe -v error -i { c } -select_streams v:0 -show_entries stream=r_frame_rate -of csv > { e } \{ d } _framerate_data.csv" , shell = True )
92
- framerate_csv = open (f".\{ e } \{ d } _framerate_data.csv" )
93
- framerate_csv_d = csv .reader (framerate_csv , delimiter = "," )
94
- framerate_base = next (framerate_csv_d )
95
- framerate_cut = (framerate_base [1 ]).split ("/" )[0 ]
96
- return (framerate_cut )
97
-
98
- if P_picture == "png" :
99
- P_jpgquality = ""
100
-
101
- if P_guimode == False :
102
- if not P_rifemode == "" :
103
- P_rifemode = "-" + P_rifemode
104
-
105
- P_check = glob .glob (f".\{ directory [0 ]} \\ *" )
106
- if not P_check == []:
107
- print ("前回の作業時のファイルが残っています" )
108
- print ("ファイルを削除してからEnterキーを押して続行してください" )
109
- input ()
110
-
111
-
112
- for i in range (len (directory )):
113
- os .makedirs (directory [i ], exist_ok = True )
114
-
115
- if P_guimode == False :
116
- P_base = input ("変換するファイルの名前を入力してください(このファイルの場所からの相対パス) >" )
117
-
118
-
119
- if P_interpolation_menu == True and P_guimode == False :
59
+ if gui_mode == False :
120
60
while True :
121
- P_interpolation = input ("補完の倍率を入力してください (2/4) >" )
122
- if interpolationchecker (P_interpolation ) == False :
123
- print ("倍率は2倍もしくは4倍で指定してください" )
124
- continue
125
- P_menu = input (f"{ P_interpolation } でよろしいですか? (y/n) >" )
126
- if P_menu == "y" or P_menu == "Y" :
61
+ video = input ("変換するファイルの名前を入力してください(絶対パス) >" )
62
+ if os .path .isfile (video ) == True :
127
63
break
128
- print ("2もしくは4を入力してください" )
129
-
130
-
131
-
132
- try :
133
- P_base_filename = P_base .split ("\\ " )[- 1 ]
134
- except :
135
- P_base_filename = P_base
136
- P_base_less = P_base_filename .split ("." )[- 2 ]
137
- P_interpolation_int = int (P_interpolation )
138
- directory_number_flt = math .log2 (P_interpolation_int )
139
- directory_number = int (directory_number_flt )
140
- P_framerate = csvmaker (P_base ,P_base_less ,directory [4 ])
141
- P_Aframerate = float (P_framerate )* 2
142
-
143
- sp .run (f"ffmpeg -y -i { P_base } { P_jpgquality } .\{ directory [0 ]} \{ P_base_less } _v%010d.{ P_picture } " ,shell = True )
144
- sp .run (f"ffmpeg -y -i { P_base } -vn -acodec copy .\{ directory [3 ]} \{ P_base_less } _a_inside.wav" ,shell = True )
145
-
146
-
147
- P_check_inside = glob .glob (f".\{ directory [3 ]} \\ { P_base_less } _a_inside.wav" )
148
- P_check_wav = glob .glob (f".\{ directory [3 ]} \\ { P_base_less } _a.wav" )
149
- P_check_m4a = glob .glob (f".\{ directory [3 ]} \\ { P_base_less } _a.m4a" )
150
-
151
-
152
- sp .run (f"rife-ncnn-vulkan -i { directory [0 ]} / -o { directory [1 ]} / { P_rifeusage } / -m rife{ P_rifemode } / -f { P_base_less } _v2x%010d.{ P_picture } " )
153
- if P_interpolation == "4" :
154
- sp .run (f"rife-ncnn-vulkan -i { directory [1 ]} / -o { directory [2 ]} / { P_rifeusage } / -m rife{ P_rifemode } / -f { P_base_less } _v4x%010d.{ P_picture } " )
155
- P_Aframerate = float (P_framerate )* 4
156
-
157
-
158
- if P_check_inside == [] and P_check_wav == [] and P_check_m4a == []:
159
- sp .run (f"ffmpeg -y -framerate { P_Aframerate } -i .\{ directory [directory_number ]} \{ P_base_less } _v{ P_interpolation } x%010d.{ P_picture } -vcodec { P_codec } -b:v { P_bitrate } -tune zerolatency -pix_fmt yuv420p -r { P_Aframerate } { P_base_less } _{ P_interpolation } x.mov" )
160
- elif not P_check_inside == []:
161
- sp .run (f"ffmpeg -y -framerate { P_Aframerate } -i .\{ directory [directory_number ]} \{ P_base_less } _v{ P_interpolation } x%010d.{ P_picture } -i .\{ directory [3 ]} \{ P_base_less } _a_inside.wav -c:v { P_codec } -c:a copy -b:v { P_bitrate } -pix_fmt yuv420p -r { P_Aframerate } { P_base_less } _{ P_interpolation } x.mov" )
162
- elif not P_check_wav == []:
163
- sp .run (f"ffmpeg -y -framerate { P_Aframerate } -i .\{ directory [directory_number ]} \{ P_base_less } _v{ P_interpolation } x%010d.{ P_picture } -i .\{ directory [3 ]} \{ P_base_less } _a.wav -c:v { P_codec } -c:a copy -b:v { P_bitrate } -pix_fmt yuv420p -r { P_Aframerate } { P_base_less } _{ P_interpolation } x.mov" )
164
- else :
165
- sp .run (f"ffmpeg -y -framerate { P_Aframerate } -i .\{ directory [directory_number ]} \{ P_base_less } _v{ P_interpolation } x%010d.{ P_picture } -i .\{ directory [3 ]} \{ P_base_less } _a.m4a -c:v { P_codec } -c:a copy -b:v { P_bitrate } -pix_fmt yuv420p -r { P_Aframerate } { P_base_less } _{ P_interpolation } x.mov" )
166
-
167
-
168
- P_menu = ''
169
-
170
- if P_guimode == False and P_delete_menu == True :
171
- print ("作業時に生成したファイルを削除しますか?\n 手動で音声ファイルを挿入した場合はそのファイルは削除されません" )
172
- P_menu = input ("y/n >" )
173
- elif P_guimode == True :
174
- P_menu = "y"
175
- elif P_guimode == False and P_defaultdelete == True :
176
- P_menu = "y"
177
-
178
-
179
- if P_menu == "y" or P_menu == "Y" :
180
- delete_list = glob .glob (f".\{ directory [0 ]} \\ { P_base_less } _v*" )
181
- count = len (delete_list )
182
- for i in range (count ):
183
- os .remove (delete_list [i ])
184
-
185
- delete_list = glob .glob (f".\{ directory [1 ]} \\ { P_base_less } _v2x*" )
186
- count = (len (delete_list ))
187
- for i in range (count ):
188
- os .remove (delete_list [i ])
189
-
190
- os .remove (f".\{ directory [4 ]} \\ { P_base_less } _framerate_data.csv" )
191
-
192
- if P_interpolation == "4" :
193
- delete_list = glob .glob (f".\{ directory [2 ]} \\ { P_base_less } _v4x*" )
194
- count = (len (delete_list ))
195
- for i in range (count ):
196
- os .remove (delete_list [i ])
197
-
198
- if not P_check_inside == []:
199
- delete_list = glob .glob (f".\{ directory [3 ]} \\ { P_base_less } _a_inside.wav" )
200
- count = (len (delete_list ))
201
- for i in range (count ):
202
- os .remove (delete_list [i ])
203
-
204
-
205
- if P_end_menu == True :
206
- print ("プログラムは正常に完了しました\n なにかキーを押して終了します" )
207
- input ()
208
-
209
- if P_guimode == True :
210
- return "Success"
211
-
64
+ else :
65
+ print ("ファイルが存在しません" )
66
+ continue
67
+ if setting .interpolation_menu == True :
68
+ while True :
69
+ setting .interpolation = input ("補完の倍率を入力してください (2/4) >" )
70
+ if setting .interpolation != "2" and setting .interpolation != "4" :
71
+ print ("2もしくは4で指定してください" )
72
+ continue
73
+ else :
74
+ setting .interpolation = int (setting .interpolation )
75
+ break
76
+
77
+ sp .run (f"ffprobe -i \" { video } \" -select_streams v:0 -show_entries stream=r_frame_rate -of csv > \" { os .path .dirname (__file__ )} \\ { directory [4 ]} \\ framerate_data.txt\" " , shell = True )
78
+ framerate = open (f"{ os .path .dirname (__file__ )} \\ { directory [4 ]} \\ framerate_data.txt" )
79
+ framerate = ((framerate .read ()).split ("," )[1 ]).split ("/" )[0 ]
80
+ Aframerate = int (int (framerate )* int (setting .interpolation ))
81
+
82
+ videodir = os .path .dirname (video )
83
+ title = os .path .basename (video )
84
+ title_no_extension = title .rsplit ("." , 1 )[0 ]
85
+ extension = title .rsplit ("." , 1 )[- 1 ]
86
+
87
+ sp .run (f"ffmpeg -i \" { video } \" -an { setting .jpgquality } .\\ { directory [0 ]} \\ { title_no_extension } _v%010d.{ setting .picture } " , shell = True )
88
+
89
+ sp .run (f"rife-ncnn-vulkan -i { directory [0 ]} / -o .\\ { directory [1 ]} / { setting .rifeusage } / -m rife{ setting .rifever } / -f { title_no_extension } _2x_%010d.{ setting .picture } " , shell = True )
90
+ if setting .interpolation == 4 :
91
+ sp .run (f"rife-ncnn-vulkan -i { directory [1 ]} / -o .\\ { directory [2 ]} / { setting .rifeusage } / -m rife{ setting .rifever } / -f { title_no_extension } _4x_%010d.{ setting .picture } " , shell = True )
92
+
93
+ sp .run (f"ffmpeg -i \" { video } \" -r { Aframerate } -i .\\ { directory [int (int (setting .interpolation )/ 2 )]} \\ { title_no_extension } _{ setting .interpolation } x_%010d.{ setting .picture } -map 0:1 -map 1:0 -c:a copy -c:v { setting .codec } -b:v { setting .bitrate } -r { Aframerate } \" { videodir } \\ { title_no_extension } _{ setting .interpolation } x.{ extension } \" " , shell = True )
94
+
95
+ if setting .defaultdelete == True :
96
+ for name in directory :
97
+ shutil .rmtree (name )
212
98
213
99
if __name__ == "__main__" :
214
- mainfunc ("" ,False ,"" , "" , "" , "" , "" , "" )
100
+ mainfunc ("" ,False ,[] )
0 commit comments