Skip to content

Commit c0fa2e1

Browse files
committed
fix mkv
- fixed wrong duration for mkv - fixed wrong type for -d argument - fixed range for -n - fixed error on exit
1 parent e42f3f6 commit c0fa2e1

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ dist
1010
ffmpeg.exe
1111
ffmpeg
1212
cutter.spec
13-
bin
13+
bin
14+
compile.bat

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ positional arguments:
2828
path the path of the video or the folder (for many videos) to cut
2929
3030
options:
31-
-h, --help show this help message and exit
32-
-d D duration of silence in seconds (default: 0.8)
33-
-n N noise level in dB (default: -40)
34-
-fr FR output video frame rate
35-
-x X Speed of the video
36-
-vfr variable frame rate on output (if -fr is given, it indicates the max number of duplicates frames per second)
37-
--keep-cfr keeps Constant Frame Rate version of the file for future editing.
38-
--preview makes a preview of 180 seconds
31+
-h, --help show this help message and exit
32+
-d D duration of silence in seconds
33+
-n [-80,-20] noise level in dB (from -80 to -20)
34+
-fr FR output video frame rate
35+
-x X Speed of the video
36+
-vfr variable frame rate on output (if -fr is given, it indicates the max number of duplicates frames per
37+
second)
38+
--keep-cfr keeps Constant Frame Rate version of the file for future editing.
39+
--preview makes a preview of 180 seconds
3940
```
4041

4142
## Examples

cutter.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ def durationDiff(original, edited):
4444
ffprobe_json1 = json.loads(os.popen(f'{FFPROBE_CMD} "{original}"').read())
4545
ffprobe_json2 = json.loads(os.popen(f'{FFPROBE_CMD} "{edited}"').read())
4646

47-
video1 = int(float(ffprobe_json1["streams"][0]["duration"]))
48-
video2 = int(float(ffprobe_json2["streams"][0]["duration"]))
47+
if "streams" in ffprobe_json1 and "duration" in ffprobe_json1["streams"][0]:
48+
video1 = int(float(ffprobe_json1["streams"][0]["duration"]))
49+
video2 = int(float(ffprobe_json2["streams"][0]["duration"]))
50+
51+
elif "format" in ffprobe_json1 and "duration" in ffprobe_json1["format"]:
52+
video1 = int(float(ffprobe_json1["format"]["duration"]))
53+
video2 = int(float(ffprobe_json2["format"]["duration"]))
54+
55+
else:
56+
return "??:??"
57+
4958

5059
minutes = str(int((abs(video1 - video2))/60))
5160
seconds = f"{int((abs(video1 - video2))%60):02d}"
@@ -62,8 +71,8 @@ def durationDiff(original, edited):
6271
parser = argparse.ArgumentParser()
6372
parser.add_argument("path", help="the path of the video or the folder (for many videos) to cut")
6473
# parser.add_argument("--teams", default=False,action="store_true", help="crops the video")
65-
parser.add_argument('-d', type=int, required=False, default=DURATION, help='duration of silence in seconds')
66-
parser.add_argument('-n', type=int, required=False, default=NOISE, help='noise level in dB')
74+
parser.add_argument('-d', type=float, required=False, default=DURATION, help='duration of silence in seconds')
75+
parser.add_argument('-n', type=int, choices=range(-80, -19), metavar="[-80,-20]", required=False, default=NOISE, help='noise level in dB (from -80 to -20)')
6776
parser.add_argument('-fr', type=int, required=False, help='output video frame rate')
6877
parser.add_argument('-x', type=float, default=-1, required=False, help='Speed of the video')
6978
parser.add_argument('-vfr', default=False, required=False, action="store_true", help='variable frame rate on output (if -fr is given, it indicates the max number of duplicates frames per second)')
@@ -144,6 +153,10 @@ def check_variable_framerate(media_file):
144153
print(f"CFR non verificato correttamente! {e}")
145154
return True
146155

156+
def check_mp4(filename):
157+
if (filename[-4:] == ".mp4"):
158+
return True
159+
return False
147160

148161
# converte un video a framerate variabile in un video a framerate statico
149162
def convert_to_cfr(_input_file, _output_file):
@@ -163,9 +176,10 @@ def convert_to_cfr(_input_file, _output_file):
163176
# decode delle opzioni
164177
_fr = f"-r {args.fr}" if (args.fr) else "" # framerate
165178
_t = f"-t 180" if (args.preview) else ""
179+
_ignore_chapters = "-ignore_chapters 1" if (check_mp4(filename)) else ""
166180

167181
# effettua la conversione
168-
command = f"{FFMPEG_CMD} -y -ignore_chapters 1 -i \"{_input_file}\" {_t} -hide_banner -loglevel info -vsync cfr -metadata comment=\"cfr version\" {_fr} -preset ultrafast \"{_tmp_file}\""
182+
command = f"{FFMPEG_CMD} -y {_ignore_chapters} -i \"{_input_file}\" {_t} -hide_banner -loglevel info -vsync cfr -metadata comment=\"cfr version\" {_fr} -preset ultrafast \"{_tmp_file}\""
169183
fancy_print(f"🔧 Sto \033[35mConvertendo\033[0m il file in CFR ({_input_file})", command)
170184

171185
os.system(command)
@@ -181,9 +195,10 @@ def speed(_input_file):
181195

182196
# decode
183197
_t = f"-t 180" if (args.preview) else "" # modalità preview
198+
_ignore_chapters = "-ignore_chapters 1" if (check_mp4(filename)) else ""
184199

185200
pts = 1/args.x
186-
command = f'{FFMPEG_CMD} -y -ignore_chapters 1 -i "{_input_file}" {_t} -hide_banner -filter:v "setpts=PTS*{pts}" -filter:a "atempo={args.x}" -preset ultrafast "{_tmp_output}"'
201+
command = f'{FFMPEG_CMD} -y {_ignore_chapters} -i "{_input_file}" {_t} -hide_banner -filter:v "setpts=PTS*{pts}" -filter:a "atempo={args.x}" -preset ultrafast "{_tmp_output}"'
187202
fancy_print(f"🚀 Sto \033[33mvelocizzando\033[0m il video ({_input_file})", command)
188203
os.system(command)
189204
# os.replace(_tmp_output, _input_file)
@@ -214,9 +229,10 @@ def cut(__file__):
214229
name = os.path.basename(filename)
215230
tmp_path = tempfile.gettempdir()
216231
_tmp_output = f"{tmp_path}/{name}{TMP_FILE_MARK}{file_extension}"
232+
_ignore_chapters = "-ignore_chapters 1" if (check_mp4(filename)) else ""
217233

218234
# eseguo il comando di taglio
219-
command = f'{FFMPEG_CMD} -y -ignore_chapters 1 -i "{input_file}" {_t} -hide_banner -filter_script:v "{vfilter}" -filter_script:a "{afilter}" {_vfr} {_fr} -metadata comment="edited" "{_tmp_output}"'
235+
command = f'{FFMPEG_CMD} -y {_ignore_chapters} -i "{input_file}" {_t} -hide_banner -filter_script:v "{vfilter}" -filter_script:a "{afilter}" {_vfr} {_fr} -metadata comment="edited" "{_tmp_output}"'
220236
fancy_print(f"✂️ Sto \033[94mtagliando\033[0m il file ({__file__})", command)
221237
os.system(command)
222238
shutil.move(_tmp_output, output)
@@ -260,7 +276,7 @@ def sort(lst):
260276
# path errato
261277
else:
262278
print("Errore, file non valido.")
263-
exit()
279+
sys.exit(0)
264280

265281
# creo la cartella fatti se non presente
266282
if not os.path.exists(folder+f"/{ORIGINAL_FILES_DESTINATION_FOLDER}"):

0 commit comments

Comments
 (0)