Repository navigation
Expand file tree
/
Copy pathfftester.py
More file actions
1447 lines (1158 loc) · 65 KB
/
Copy pathfftester.py
File metadata and controls
1447 lines (1158 loc) · 65 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, scrolledtext
import threading
import subprocess
import tempfile
import re
import json
from PIL import Image, ImageTk
import cv2
import time
import shutil
from collections import defaultdict
class FFtester:
def __init__(self, root, ffmpeg_path):
self.ffmpeg_path = ffmpeg_path
self.root = root
self.root.title("FFtester")
self.root.geometry("1200x900")
# Video variables
self.input_video_path = None
self.encoded_video_path = None
self.cap_original = None
self.cap_encoded = None
self.is_playing = False
self.playing_thread = None
self.temp_dir = tempfile.mkdtemp()
# FFmpeg info
self.video_codecs = []
self.audio_codecs = []
self.containers = []
self.codec_presets = defaultdict(list)
self.codec_crf_ranges = defaultdict(lambda: {"min": 0, "max": 51, "default": 23})
self.codec_pixel_formats = defaultdict(list)
# Media info
self.video_info = {}
self.audio_info = {}
# Status variable for all tabs
self.status_var = tk.StringVar(value="Ready")
# **NEW: Initialize fullscreen and image attributes**
self.is_fullscreen = False
self.original_img = None
self.encoded_img = None
# Load FFmpeg capabilities
self.load_ffmpeg_info()
# Create UI
self.create_ui()
# Bind closing event to cleanup function
self.root.protocol("WM_DELETE_WINDOW", self.cleanup)
def load_ffmpeg_info(self):
self.status_var.set("Loading FFmpeg information...")
# Get video codecs
try:
result = subprocess.run([self.ffmpeg_path, '-encoders'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
lines = result.stdout.split('\n')
# Process video encoders
video_codecs = []
audio_codecs = []
for line in lines:
if re.match(r'^\s+[.VASEFL]+\s+\w+\s+.*$', line):
parts = line.strip().split()
if len(parts) >= 3:
codec_type, codec_name = parts[0], parts[1]
if 'V' in codec_type: # Video codec
video_codecs.append(codec_name)
elif 'A' in codec_type: # Audio codec
audio_codecs.append(codec_name)
self.video_codecs = sorted(video_codecs)
self.audio_codecs = sorted(audio_codecs)
# Get common presets for popular codecs
popular_video_codecs = ["libx264", "libx265", "h264_nvenc", "hevc_nvenc", "libvpx-vp9", "libaom-av1"]
for codec in popular_video_codecs:
# Set default presets for libx264 and similar codecs
if codec in ["libx264", "libx265", "h264_nvenc", "hevc_nvenc"]:
self.codec_presets[codec] = ["ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow"]
elif codec == "libvpx-vp9":
self.codec_presets[codec] = ["0", "1", "2", "3", "4", "5", "6"]
elif codec == "libaom-av1":
self.codec_presets[codec] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
# CRF ranges
if codec in ["libx264", "h264_nvenc"]:
self.codec_crf_ranges[codec] = {"min": 0, "max": 51, "default": 23}
elif codec in ["libx265", "hevc_nvenc"]:
self.codec_crf_ranges[codec] = {"min": 0, "max": 51, "default": 28}
elif codec == "libvpx-vp9":
self.codec_crf_ranges[codec] = {"min": 0, "max": 63, "default": 31}
elif codec == "libaom-av1":
self.codec_crf_ranges[codec] = {"min": 0, "max": 63, "default": 30}
# Get pixel formats
result = subprocess.run([self.ffmpeg_path, '-pix_fmts'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
lines = result.stdout.split('\n')
pixel_formats = []
for line in lines:
if re.match(r'^[IO.]{3} \w+.*$', line):
parts = line.strip().split()
if len(parts) >= 2:
pixel_formats.append(parts[1])
# Common pixel formats for each codec
self.pixel_formats = sorted(pixel_formats)
for codec in popular_video_codecs:
if codec in ["libx264", "h264_nvenc"]:
self.codec_pixel_formats[codec] = ["yuv420p", "yuv422p", "yuv444p", "yuvj420p", "yuvj422p", "yuvj444p"]
elif codec in ["libx265", "hevc_nvenc"]:
self.codec_pixel_formats[codec] = ["yuv420p", "yuv422p", "yuv444p", "gbrp", "gbrap"]
elif codec in ["libvpx-vp9", "libaom-av1"]:
self.codec_pixel_formats[codec] = ["yuv420p", "yuv422p", "yuv444p", "yuva420p"]
# Get containers
result = subprocess.run([self.ffmpeg_path, '-formats'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
lines = result.stdout.split('\n')
containers = []
for line in lines:
if re.match(r'^\s*[DE ]{2}\s+\w+\s+.*$', line):
parts = line.strip().split()
if len(parts) >= 3 and 'E' in parts[0]: # Encodable format
containers.append(parts[1])
# Filter common container formats
common_containers = ["mp4", "mkv", "webm", "mov", "avi", "flv", "ts", "mxf", "mpg"]
self.containers = [c for c in common_containers if c in containers]
self.status_var.set("Ready")
except Exception as e:
print(f"Error loading FFmpeg info: {e}")
# Set some default values if FFmpeg query fails
self.video_codecs = ["libx264", "libx265", "h264_nvenc", "hevc_nvenc", "libvpx-vp9", "libaom-av1"]
self.audio_codecs = ["aac", "libmp3lame", "libopus", "flac", "pcm_s16le"]
self.containers = ["mp4", "mkv", "webm", "mov", "avi"]
self.codec_presets["libx264"] = ["ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow"]
self.codec_crf_ranges["libx264"] = {"min": 0, "max": 51, "default": 23}
self.codec_pixel_formats["libx264"] = ["yuv420p", "yuv422p", "yuv444p"]
self.status_var.set("Error loading FFmpeg information")
def create_ui(self):
# Create notebook for tabs
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Main tab
main_tab = ttk.Frame(self.notebook)
self.notebook.add(main_tab, text="Main")
# Media Info tab
media_info_tab = ttk.Frame(self.notebook)
self.notebook.add(media_info_tab, text="Media Info")
# Advanced settings tab
advanced_tab = ttk.Frame(self.notebook)
self.notebook.add(advanced_tab, text="Advanced Settings")
# Command view tab
command_tab = ttk.Frame(self.notebook)
self.notebook.add(command_tab, text="FFmpeg Command")
# Create UI for main tab
self.create_main_tab(main_tab)
# Create UI for media info tab
self.create_media_info_tab(media_info_tab)
# Create UI for advanced tab
self.create_advanced_tab(advanced_tab)
# Create UI for command tab
self.create_command_tab(command_tab)
# Status bar (shared across all tabs)
status_bar = ttk.Label(self.root, textvariable=self.status_var, relief=tk.SUNKEN, anchor=tk.W)
status_bar.pack(fill=tk.X, side=tk.BOTTOM, padx=5, pady=5)
def create_main_tab(self, parent):
# Main frame
main_frame = ttk.Frame(parent, padding=10)
main_frame.pack(fill=tk.BOTH, expand=True)
# Top control panel
control_frame = ttk.LabelFrame(main_frame, text="Video Controls", padding=10)
control_frame.pack(fill=tk.X, pady=5)
# File selection
file_frame = ttk.Frame(control_frame)
file_frame.pack(fill=tk.X, pady=5)
self.file_path_var = tk.StringVar()
ttk.Label(file_frame, text="Video File:").pack(side=tk.LEFT, padx=5)
ttk.Entry(file_frame, textvariable=self.file_path_var, width=50).pack(side=tk.LEFT, padx=5)
ttk.Button(file_frame, text="Browse...", command=self.select_video).pack(side=tk.LEFT, padx=5)
# Basic encoding settings frame
settings_frame = ttk.LabelFrame(main_frame, text="Basic Encoding Settings", padding=10)
settings_frame.pack(fill=tk.X, pady=5)
# Container format
container_frame = ttk.Frame(settings_frame)
container_frame.pack(fill=tk.X, pady=5)
ttk.Label(container_frame, text="Container:").pack(side=tk.LEFT, padx=5)
self.container_var = tk.StringVar(value="mp4")
container_dropdown = ttk.Combobox(container_frame, textvariable=self.container_var,
values=self.containers, width=10)
container_dropdown.pack(side=tk.LEFT, padx=5)
# Video codec selection
codec_frame = ttk.Frame(settings_frame)
codec_frame.pack(fill=tk.X, pady=5)
ttk.Label(codec_frame, text="Video Codec:").pack(side=tk.LEFT, padx=5)
self.video_codec_var = tk.StringVar(value="libx264")
self.video_codec_dropdown = ttk.Combobox(codec_frame, textvariable=self.video_codec_var,
values=self.video_codecs, width=15)
self.video_codec_dropdown.pack(side=tk.LEFT, padx=5)
self.video_codec_dropdown.bind("<<ComboboxSelected>>", self.update_codec_dependent_options)
# Audio codec selection
ttk.Label(codec_frame, text="Audio Codec:").pack(side=tk.LEFT, padx=5)
self.audio_codec_var = tk.StringVar(value="aac")
audio_codec_dropdown = ttk.Combobox(codec_frame, textvariable=self.audio_codec_var,
values=self.audio_codecs, width=15)
audio_codec_dropdown.pack(side=tk.LEFT, padx=5)
# CRF/Quality
quality_frame = ttk.Frame(settings_frame)
quality_frame.pack(fill=tk.X, pady=5)
ttk.Label(quality_frame, text="CRF/Quality:").pack(side=tk.LEFT, padx=5)
self.crf_var = tk.StringVar(value="23")
self.crf_scale = ttk.Scale(quality_frame, from_=0, to=51, orient=tk.HORIZONTAL,
variable=self.crf_var, length=200)
self.crf_scale.pack(side=tk.LEFT, padx=5)
self.crf_entry = ttk.Entry(quality_frame, textvariable=self.crf_var, width=4)
self.crf_entry.pack(side=tk.LEFT, padx=5)
# Preset
ttk.Label(quality_frame, text="Preset:").pack(side=tk.LEFT, padx=5)
self.preset_var = tk.StringVar(value="medium")
self.preset_dropdown = ttk.Combobox(quality_frame, textvariable=self.preset_var,
values=self.codec_presets["libx264"], width=10)
self.preset_dropdown.pack(side=tk.LEFT, padx=5)
# Resolution scaling
resolution_frame = ttk.Frame(settings_frame)
resolution_frame.pack(fill=tk.X, pady=5)
ttk.Label(resolution_frame, text="Resolution:").pack(side=tk.LEFT, padx=5)
self.width_var = tk.StringVar(value="")
self.height_var = tk.StringVar(value="")
ttk.Entry(resolution_frame, textvariable=self.width_var, width=5).pack(side=tk.LEFT)
ttk.Label(resolution_frame, text="x").pack(side=tk.LEFT)
ttk.Entry(resolution_frame, textvariable=self.height_var, width=5).pack(side=tk.LEFT, padx=5)
ttk.Label(resolution_frame, text="(leave empty for original)").pack(side=tk.LEFT)
# FPS
ttk.Label(resolution_frame, text="FPS:").pack(side=tk.LEFT, padx=5)
self.fps_var = tk.StringVar(value="")
ttk.Entry(resolution_frame, textvariable=self.fps_var, width=5).pack(side=tk.LEFT, padx=5)
ttk.Label(resolution_frame, text="(leave empty for original)").pack(side=tk.LEFT)
# Encode button
encode_frame = ttk.Frame(main_frame)
encode_frame.pack(fill=tk.X, pady=10)
ttk.Button(encode_frame, text="Encode Video", command=self.encode_video).pack(pady=5)
# Video display area
video_frame = ttk.Frame(main_frame)
video_frame.pack(fill=tk.BOTH, expand=True, pady=10)
# Original video canvas
original_frame = ttk.LabelFrame(video_frame, text="Original Video")
original_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5)
self.original_canvas = tk.Canvas(original_frame, bg="black")
self.original_canvas.pack(fill=tk.BOTH, expand=True)
# Encoded video canvas
encoded_frame = ttk.LabelFrame(video_frame, text="Encoded Video")
encoded_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5)
self.encoded_canvas = tk.Canvas(encoded_frame, bg="black")
self.encoded_canvas.pack(fill=tk.BOTH, expand=True)
# Playback controls
playback_frame = ttk.Frame(main_frame)
playback_frame.pack(fill=tk.X, pady=5)
self.play_btn = ttk.Button(playback_frame, text="Play", command=self.toggle_playback)
self.play_btn.pack(side=tk.LEFT, padx=5)
self.reset_btn = ttk.Button(playback_frame, text="Reset", command=self.reset_playback)
self.reset_btn.pack(side=tk.LEFT, padx=5)
# Fullscreen button (or other trigger)
fullscreen_frame = ttk.Frame(main_frame)
fullscreen_frame.pack(fill=tk.X, pady=5)
self.fullscreen_btn = ttk.Button(fullscreen_frame, text="Fullscreen", command=self.toggle_fullscreen)
self.fullscreen_btn.pack(side=tk.LEFT, padx=5)
# Bind canvas resize event for proper scaling
self.original_canvas.bind("<Configure>", self.resize_original_video)
self.encoded_canvas.bind("<Configure>", self.resize_encoded_video)
# Statistics frame
stats_frame = ttk.LabelFrame(main_frame, text="Video Statistics", padding=10)
stats_frame.pack(fill=tk.X, pady=5)
# Original video stats
original_stats_frame = ttk.Frame(stats_frame)
original_stats_frame.pack(fill=tk.X, pady=5)
ttk.Label(original_stats_frame, text="Original File Size:").pack(side=tk.LEFT, padx=5)
self.original_size_var = tk.StringVar(value="N/A")
ttk.Label(original_stats_frame, textvariable=self.original_size_var).pack(side=tk.LEFT, padx=5)
ttk.Label(original_stats_frame, text="Original Resolution:").pack(side=tk.LEFT, padx=5)
self.original_res_var = tk.StringVar(value="N/A")
ttk.Label(original_stats_frame, textvariable=self.original_res_var).pack(side=tk.LEFT, padx=5)
ttk.Label(original_stats_frame, text="Original FPS:").pack(side=tk.LEFT, padx=5)
self.original_fps_var = tk.StringVar(value="N/A")
ttk.Label(original_stats_frame, textvariable=self.original_fps_var).pack(side=tk.LEFT, padx=5)
# Encoded video stats
encoded_stats_frame = ttk.Frame(stats_frame)
encoded_stats_frame.pack(fill=tk.X, pady=5)
ttk.Label(encoded_stats_frame, text="Encoded File Size:").pack(side=tk.LEFT, padx=5)
self.encoded_size_var = tk.StringVar(value="N/A")
ttk.Label(encoded_stats_frame, textvariable=self.encoded_size_var).pack(side=tk.LEFT, padx=5)
ttk.Label(encoded_stats_frame, text="Encoded Resolution:").pack(side=tk.LEFT, padx=5)
self.encoded_res_var = tk.StringVar(value="N/A")
ttk.Label(encoded_stats_frame, textvariable=self.encoded_res_var).pack(side=tk.LEFT, padx=5)
ttk.Label(encoded_stats_frame, text="Size Reduction:").pack(side=tk.LEFT, padx=5)
self.size_reduction_var = tk.StringVar(value="N/A")
ttk.Label(encoded_stats_frame, textvariable=self.size_reduction_var).pack(side=tk.LEFT, padx=5)
self.root.bind("<Escape>", self.toggle_fullscreen)
def create_media_info_tab(self, parent):
# Media info frame
media_frame = ttk.Frame(parent, padding=10)
media_frame.pack(fill=tk.BOTH, expand=True)
# Video Information Section
video_info_frame = ttk.LabelFrame(media_frame, text="Video Stream Information", padding=10)
video_info_frame.pack(fill=tk.BOTH, expand=True, pady=5)
self.video_info_text = scrolledtext.ScrolledText(video_info_frame, height=10, width=80, wrap=tk.WORD)
self.video_info_text.pack(fill=tk.BOTH, expand=True, pady=5)
# Audio Information Section
audio_info_frame = ttk.LabelFrame(media_frame, text="Audio Stream Information", padding=10)
audio_info_frame.pack(fill=tk.BOTH, expand=True, pady=5)
self.audio_info_text = scrolledtext.ScrolledText(audio_info_frame, height=10, width=80, wrap=tk.WORD)
self.audio_info_text.pack(fill=tk.BOTH, expand=True, pady=5)
# Container Information Section
container_info_frame = ttk.LabelFrame(media_frame, text="Container Information", padding=10)
container_info_frame.pack(fill=tk.BOTH, expand=True, pady=5)
self.container_info_text = scrolledtext.ScrolledText(container_info_frame, height=6, width=80, wrap=tk.WORD)
self.container_info_text.pack(fill=tk.BOTH, expand=True, pady=5)
# Refresh button
refresh_frame = ttk.Frame(media_frame)
refresh_frame.pack(fill=tk.X, pady=5)
ttk.Button(refresh_frame, text="Refresh Media Info", command=self.refresh_media_info).pack(side=tk.RIGHT)
def create_advanced_tab(self, parent):
# Advanced settings frame
advanced_frame = ttk.Frame(parent, padding=10)
advanced_frame.pack(fill=tk.BOTH, expand=True)
# Create left and right columns for advanced settings
left_column = ttk.Frame(advanced_frame)
left_column.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5)
right_column = ttk.Frame(advanced_frame)
right_column.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5)
# ===== Video Advanced Settings (Left Column) =====
video_settings = ttk.LabelFrame(left_column, text="Advanced Video Settings", padding=10)
video_settings.pack(fill=tk.X, pady=5)
# Video bitrate
video_bitrate_frame = ttk.Frame(video_settings)
video_bitrate_frame.pack(fill=tk.X, pady=5)
ttk.Label(video_bitrate_frame, text="Video Bitrate:").pack(side=tk.LEFT, padx=5)
self.video_bitrate_var = tk.StringVar(value="")
ttk.Entry(video_bitrate_frame, textvariable=self.video_bitrate_var, width=10).pack(side=tk.LEFT, padx=5)
ttk.Label(video_bitrate_frame, text="(e.g., 5M, 2000k, leave empty for CRF)").pack(side=tk.LEFT)
# Pixel format
pixel_format_frame = ttk.Frame(video_settings)
pixel_format_frame.pack(fill=tk.X, pady=5)
ttk.Label(pixel_format_frame, text="Pixel Format:").pack(side=tk.LEFT, padx=5)
self.pixel_format_var = tk.StringVar(value="yuv420p")
self.pixel_format_dropdown = ttk.Combobox(pixel_format_frame, textvariable=self.pixel_format_var,
values=self.codec_pixel_formats["libx264"], width=15)
self.pixel_format_dropdown.pack(side=tk.LEFT, padx=5)
# Profile
profile_frame = ttk.Frame(video_settings)
profile_frame.pack(fill=tk.X, pady=5)
ttk.Label(profile_frame, text="Profile:").pack(side=tk.LEFT, padx=5)
self.profile_var = tk.StringVar(value="")
self.profile_dropdown = ttk.Combobox(profile_frame, textvariable=self.profile_var,
values=["", "baseline", "main", "high"], width=10)
self.profile_dropdown.pack(side=tk.LEFT, padx=5)
# Level
ttk.Label(profile_frame, text="Level:").pack(side=tk.LEFT, padx=5)
self.level_var = tk.StringVar(value="")
self.level_dropdown = ttk.Combobox(profile_frame, textvariable=self.level_var,
values=["", "3.0", "3.1", "4.0", "4.1", "4.2", "5.0", "5.1", "5.2"], width=5)
self.level_dropdown.pack(side=tk.LEFT, padx=5)
# GOP size
gop_frame = ttk.Frame(video_settings)
gop_frame.pack(fill=tk.X, pady=5)
ttk.Label(gop_frame, text="GOP Size:").pack(side=tk.LEFT, padx=5)
self.gop_var = tk.StringVar(value="")
ttk.Entry(gop_frame, textvariable=self.gop_var, width=5).pack(side=tk.LEFT, padx=5)
ttk.Label(gop_frame, text="(frames between keyframes)").pack(side=tk.LEFT)
# ===== Audio Advanced Settings (Right Column) =====
audio_settings = ttk.LabelFrame(right_column, text="Advanced Audio Settings", padding=10)
audio_settings.pack(fill=tk.X, pady=5)
# Audio bitrate
audio_bitrate_frame = ttk.Frame(audio_settings)
audio_bitrate_frame.pack(fill=tk.X, pady=5)
ttk.Label(audio_bitrate_frame, text="Audio Bitrate:").pack(side=tk.LEFT, padx=5)
self.audio_bitrate_var = tk.StringVar(value="128k")
ttk.Entry(audio_bitrate_frame, textvariable=self.audio_bitrate_var, width=10).pack(side=tk.LEFT, padx=5)
ttk.Label(audio_bitrate_frame, text="(e.g., 128k, 192k)").pack(side=tk.LEFT)
# Audio sample rate
sample_rate_frame = ttk.Frame(audio_settings)
sample_rate_frame.pack(fill=tk.X, pady=5)
ttk.Label(sample_rate_frame, text="Sample Rate:").pack(side=tk.LEFT, padx=5)
self.sample_rate_var = tk.StringVar(value="")
sample_rate_dropdown = ttk.Combobox(sample_rate_frame, textvariable=self.sample_rate_var,
values=["", "44100", "48000", "96000", "192000"], width=10)
sample_rate_dropdown.pack(side=tk.LEFT, padx=5)
ttk.Label(sample_rate_frame, text="Hz (leave empty for original)").pack(side=tk.LEFT)
# Audio channels
channels_frame = ttk.Frame(audio_settings)
channels_frame.pack(fill=tk.X, pady=5)
ttk.Label(channels_frame, text="Audio Channels:").pack(side=tk.LEFT, padx=5)
self.channels_var = tk.StringVar(value="")
channels_dropdown = ttk.Combobox(channels_frame, textvariable=self.channels_var,
values=["", "1", "2", "6"], width=5)
channels_dropdown.pack(side=tk.LEFT, padx=5)
ttk.Label(channels_frame, text="(1=mono, 2=stereo, 6=5.1, empty=original)").pack(side=tk.LEFT)
# Audio sample format
sample_format_frame = ttk.Frame(audio_settings)
sample_format_frame.pack(fill=tk.X, pady=5)
ttk.Label(sample_format_frame, text="Sample Format:").pack(side=tk.LEFT, padx=5)
self.sample_format_var = tk.StringVar(value="")
sample_format_dropdown = ttk.Combobox(sample_format_frame, textvariable=self.sample_format_var,
values=["", "s16", "s32", "flt", "fltp"], width=10)
sample_format_dropdown.pack(side=tk.LEFT, padx=5)
# ===== Additional Parameters (Bottom) =====
additional_frame = ttk.LabelFrame(advanced_frame, text="Additional Parameters", padding=10)
additional_frame.pack(fill=tk.BOTH, expand=True, pady=5)
# Additional FFmpeg parameters
ttk.Label(additional_frame, text="Additional FFmpeg Parameters:").pack(anchor=tk.W, padx=5, pady=5)
self.additional_params_var = tk.StringVar()
ttk.Entry(additional_frame, textvariable=self.additional_params_var, width=80).pack(fill=tk.X, padx=5, pady=5)
def create_command_tab(self, parent):
# Command display frame
command_frame = ttk.Frame(parent, padding=10)
command_frame.pack(fill=tk.BOTH, expand=True)
# Preview button
ttk.Button(command_frame, text="Generate Command Preview", command=self.preview_command).pack(anchor=tk.W, pady=5)
# Command text area
ttk.Label(command_frame, text="FFmpeg Command:").pack(anchor=tk.W, pady=5)
self.command_text = scrolledtext.ScrolledText(command_frame, height=10, width=80, wrap=tk.WORD)
self.command_text.pack(fill=tk.BOTH, expand=True, pady=5)
# Command output log
ttk.Label(command_frame, text="Output Log:").pack(anchor=tk.W, pady=5)
self.output_log = scrolledtext.ScrolledText(command_frame, height=10, width=80, wrap=tk.WORD)
self.output_log.pack(fill=tk.BOTH, expand=True, pady=5)
# Continue from where the original code left off
def select_video(self):
"""Select input video file"""
file_path = filedialog.askopenfilename(
title="Select Video File",
filetypes=[("Video Files", "*.mp4 *.avi *.mkv *.mov *.webm *.flv *.wmv")]
)
if file_path:
self.input_video_path = file_path
self.file_path_var.set(file_path)
# Load video info
self.load_video_info()
# Update UI with loaded video info
self.update_video_info_ui()
# Reset any encoded video
self.encoded_video_path = None
self.encoded_size_var.set("N/A")
self.encoded_res_var.set("N/A")
self.size_reduction_var.set("N/A")
# Stop any ongoing playback
self.stop_playback()
def load_video_info(self):
"""Load input video information using FFprobe"""
if not self.input_video_path:
return
try:
self.status_var.set("Loading media information...")
# Run FFprobe to get JSON output with media information
cmd = [
"ffprobe", "-v", "quiet", "-print_format", "json",
"-show_streams", "-show_format", self.input_video_path
]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
media_info = json.loads(result.stdout)
# Extract video and audio stream information
self.video_info = {}
self.audio_info = {}
for stream in media_info.get("streams", []):
if stream.get("codec_type") == "video" and not self.video_info:
self.video_info = stream
elif stream.get("codec_type") == "audio" and not self.audio_info:
self.audio_info = stream
# Extract container information
self.container_info = media_info.get("format", {})
# Update file size information
file_size = os.path.getsize(self.input_video_path)
self.original_size_var.set(f"{file_size / (1024 * 1024):.2f} MB")
# Update resolution information
if self.video_info:
width = self.video_info.get("width", "?")
height = self.video_info.get("height", "?")
self.original_res_var.set(f"{width}x{height}")
# Suggest the same resolution for encoding
self.width_var.set(str(width))
self.height_var.set(str(height))
# Update FPS information
frame_rate = self.video_info.get("r_frame_rate", "")
if "/" in frame_rate:
num, den = frame_rate.split("/")
if den != "0":
fps = round(float(num) / float(den), 2)
self.original_fps_var.set(f"{fps}")
self.fps_var.set(str(int(fps)))
self.status_var.set("Ready")
except Exception as e:
print(f"Error loading video info: {e}")
self.status_var.set(f"Error loading video info: {str(e)}")
def update_video_info_ui(self):
"""Update the media info tab UI with loaded video information"""
# Clear previous info
self.video_info_text.delete(1.0, tk.END)
self.audio_info_text.delete(1.0, tk.END)
self.container_info_text.delete(1.0, tk.END)
# Format and display video stream info
if self.video_info:
video_details = [
f"Codec: {self.video_info.get('codec_name', 'Unknown')} ({self.video_info.get('codec_long_name', 'Unknown')})",
f"Resolution: {self.video_info.get('width', '?')}x{self.video_info.get('height', '?')}",
f"Bit Depth: {self.video_info.get('bits_per_raw_sample', '?')} bits",
f"Pixel Format: {self.video_info.get('pix_fmt', 'Unknown')}",
f"Frame Rate: {self.video_info.get('r_frame_rate', 'Unknown')}",
f"Average Frame Rate: {self.video_info.get('avg_frame_rate', 'Unknown')}",
f"Bitrate: {int(self.video_info.get('bit_rate', 0)) // 1000} kbps" if 'bit_rate' in self.video_info else "Bitrate: Unknown",
f"Duration: {self.video_info.get('duration', '?')} seconds",
f"Profile: {self.video_info.get('profile', 'Unknown')}",
f"Level: {self.video_info.get('level', 'Unknown')}"
]
self.video_info_text.insert(tk.END, "\n".join(video_details))
else:
self.video_info_text.insert(tk.END, "No video stream found")
# Format and display audio stream info
if self.audio_info:
audio_details = [
f"Codec: {self.audio_info.get('codec_name', 'Unknown')} ({self.audio_info.get('codec_long_name', 'Unknown')})",
f"Sample Rate: {self.audio_info.get('sample_rate', '?')} Hz",
f"Channels: {self.audio_info.get('channels', '?')}",
f"Channel Layout: {self.audio_info.get('channel_layout', 'Unknown')}",
f"Sample Format: {self.audio_info.get('sample_fmt', 'Unknown')}",
f"Bitrate: {int(self.audio_info.get('bit_rate', 0)) // 1000} kbps" if 'bit_rate' in self.audio_info else "Bitrate: Unknown",
f"Duration: {self.audio_info.get('duration', '?')} seconds"
]
self.audio_info_text.insert(tk.END, "\n".join(audio_details))
else:
self.audio_info_text.insert(tk.END, "No audio stream found")
# Format and display container info
if self.container_info:
container_details = [
f"Format: {self.container_info.get('format_name', 'Unknown')} ({self.container_info.get('format_long_name', 'Unknown')})",
f"Duration: {self.container_info.get('duration', '?')} seconds",
f"Size: {float(self.container_info.get('size', 0)) / (1024*1024):.2f} MB",
f"Bitrate: {int(self.container_info.get('bit_rate', 0)) // 1000} kbps" if 'bit_rate' in self.container_info else "Bitrate: Unknown",
f"Number of Streams: {self.container_info.get('nb_streams', '?')}",
f"Start Time: {self.container_info.get('start_time', '0')} seconds"
]
self.container_info_text.insert(tk.END, "\n".join(container_details))
else:
self.container_info_text.insert(tk.END, "No container info available")
def refresh_media_info(self):
"""Refresh the media information"""
if self.input_video_path:
self.load_video_info()
self.update_video_info_ui()
def update_codec_dependent_options(self, event=None):
"""Update codec-dependent UI elements when codec selection changes"""
selected_codec = self.video_codec_var.get()
# Update CRF range
crf_range = self.codec_crf_ranges.get(selected_codec, {"min": 0, "max": 51, "default": 23})
self.crf_scale.config(from_=crf_range["min"], to=crf_range["max"])
self.crf_var.set(str(crf_range["default"]))
# Update presets
presets = self.codec_presets.get(selected_codec, ["medium"])
self.preset_dropdown.config(values=presets)
if presets:
self.preset_var.set(presets[len(presets)//2]) # Default to middle preset
else:
self.preset_var.set("")
# Update pixel formats
pixel_formats = self.codec_pixel_formats.get(selected_codec, ["yuv420p"])
self.pixel_format_dropdown.config(values=pixel_formats)
if pixel_formats:
self.pixel_format_var.set(pixel_formats[0])
# Update profile/level options based on codec
if selected_codec in ["libx264", "h264_nvenc"]:
self.profile_dropdown.config(values=["", "baseline", "main", "high"])
self.level_dropdown.config(values=["", "3.0", "3.1", "4.0", "4.1", "4.2", "5.0", "5.1", "5.2"])
elif selected_codec in ["libx265", "hevc_nvenc"]:
self.profile_dropdown.config(values=["", "main", "main10", "main12", "main444-8", "main444-10"])
self.level_dropdown.config(values=["", "3.1", "4.0", "4.1", "5.0", "5.1", "5.2", "6.0", "6.1", "6.2"])
else:
self.profile_dropdown.config(values=[""])
self.level_dropdown.config(values=[""])
self.profile_var.set("")
self.level_var.set("")
def preview_command(self):
"""Generate and display the FFmpeg command without executing it"""
if not self.input_video_path:
messagebox.showwarning("No Input", "Please select an input video file first.")
return
cmd = self._build_ffmpeg_command()
# Display the command in the command text area
self.command_text.delete(1.0, tk.END)
self.command_text.insert(tk.END, " ".join(cmd))
def _build_ffmpeg_command(self):
"""Build the FFmpeg command based on current settings"""
if not self.input_video_path:
return []
# Get output file path
output_ext = self.container_var.get()
output_file = os.path.join(self.temp_dir, f"encoded_output.{output_ext}")
cmd = [self.ffmpeg_path, "-y", "-i", self.input_video_path]
# Video codec options
video_codec = self.video_codec_var.get()
if video_codec:
cmd.extend(["-c:v", video_codec])
# Video quality/bitrate
video_bitrate = self.video_bitrate_var.get()
crf_value = self.crf_var.get()
if video_bitrate:
cmd.extend(["-b:v", video_bitrate])
elif crf_value:
if video_codec in ["libx264", "libx265", "h264_nvenc", "hevc_nvenc"]:
cmd.extend(["-crf", crf_value])
elif video_codec in ["libvpx-vp9"]:
cmd.extend(["-crf", crf_value])
elif video_codec in ["libaom-av1"]:
cmd.extend(["-crf", crf_value])
# Preset
preset = self.preset_var.get()
if preset:
cmd.extend(["-preset", preset])
# Resolution
width = self.width_var.get()
height = self.height_var.get()
if width and height:
cmd.extend(["-vf", f"scale={width}:{height}"])
# FPS
fps = self.fps_var.get()
if fps:
if width and height:
# Update the scale filter to include fps
cmd[-1] = f"scale={width}:{height},fps={fps}"
else:
cmd.extend(["-vf", f"fps={fps}"])
# Pixel format
pixel_format = self.pixel_format_var.get()
if pixel_format:
cmd.extend(["-pix_fmt", pixel_format])
# Profile and level
profile = self.profile_var.get()
if profile:
cmd.extend(["-profile:v", profile])
level = self.level_var.get()
if level:
cmd.extend(["-level", level])
# GOP size (keyframe interval)
gop = self.gop_var.get()
if gop:
cmd.extend(["-g", gop])
# Audio codec options
audio_codec = self.audio_codec_var.get()
if audio_codec:
cmd.extend(["-c:a", audio_codec])
# Audio bitrate
audio_bitrate = self.audio_bitrate_var.get()
if audio_bitrate:
cmd.extend(["-b:a", audio_bitrate])
# Audio sample rate
sample_rate = self.sample_rate_var.get()
if sample_rate:
cmd.extend(["-ar", sample_rate])
# Audio channels
channels = self.channels_var.get()
if channels:
cmd.extend(["-ac", channels])
# Audio sample format
sample_format = self.sample_format_var.get()
if sample_format:
cmd.extend(["-sample_fmt", sample_format])
# Additional parameters
additional_params = self.additional_params_var.get()
if additional_params:
cmd.extend(additional_params.split())
cmd.append(output_file)
return cmd
def encode_video(self):
"""Encode the video with the selected settings"""
if not self.input_video_path:
messagebox.showwarning("No Input", "Please select an input video file first.")
return
# Stop any existing playback
self.stop_playback()
# Build the FFmpeg command
cmd = self._build_ffmpeg_command()
# Update status
self.status_var.set("Encoding in progress...")
# Clear the output log
self.output_log.delete(1.0, tk.END)
# Switch to the command tab to show progress
self.notebook.select(3) # Index of the command tab
# Update the command preview
self.command_text.delete(1.0, tk.END)
self.command_text.insert(tk.END, " ".join(cmd))
# Run encoding in a separate thread
encoding_thread = threading.Thread(target=self._run_encoding, args=(cmd,))
encoding_thread.daemon = True
encoding_thread.start()
def _run_encoding(self, cmd):
"""Run the FFmpeg encoding process and handle output"""
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
self.output_log.insert(tk.END, output)
self.output_log.see(tk.END)
return_code = process.poll()
if return_code == 0:
self.root.after(0, self._encoding_finished_success)
else:
error_message = process.stderr.read()
self.root.after(0, self._encoding_finished_error, return_code, error_message)
except Exception as e:
self.root.after(0, self._encoding_finished_exception, e)
def _encoding_finished_success(self):
"""Handle successful encoding"""
self.status_var.set("Encoding completed successfully!")
messagebox.showinfo("Encoding Complete", "Video encoding completed!")
# Get file sizes and calculate percentage change
original_size = os.path.getsize(self.input_video_path)
encoded_size = os.path.getsize(self.encoded_video_path)
percentage_change = ((encoded_size - original_size) / original_size) * 100
# Update UI elements
self.original_size_var.set(f"{original_size / (1024 * 1024):.2f} MB")
self.encoded_size_var.set(f"{encoded_size / (1024 * 1024):.2f} MB")
self.size_reduction_var.set(f"{percentage_change:.2f} %")
self.load_video_info()
self.update_video_info_ui()
self.load_and_display_frames()
def load_and_display_frames(self):
"""Load the first frame of the original and encoded videos and display them"""
if not self.input_video_path or not self.encoded_video_path:
messagebox.showwarning("No Input/Output", "Please select both input and output video files.")
return
try:
# Open video files
self.cap_original = cv2.VideoCapture(self.input_video_path)
self.cap_encoded = cv2.VideoCapture(self.encoded_video_path)
# Read the first frame from each video
ret_original, original_frame = self.cap_original.read()
ret_encoded, encoded_frame = self.cap_encoded.read()
if not ret_original or not ret_encoded:
messagebox.showerror("Error", "Could not read frames from one or both video files.")
return
# Convert frames to PIL images
original_frame_rgb = cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB)
encoded_frame_rgb = cv2.cvtColor(encoded_frame, cv2.COLOR_BGR2RGB)
original_frame_pil = Image.fromarray(original_frame_rgb)
encoded_frame_pil = Image.fromarray(encoded_frame_rgb)
# Resize frames to fit canvases while preserving aspect ratio
self.original_img = original_frame_pil
self.encoded_img = encoded_frame_pil
self.resize_video_frame(self.original_canvas.winfo_geometry(), self.original_canvas, self.original_img)
self.resize_video_frame(self.encoded_canvas.winfo_geometry(), self.encoded_canvas, self.encoded_img)
# Update canvases with the first frames
#self.original_photo = ImageTk.PhotoImage(original_frame_pil)
#self.original_canvas.create_image(0, 0, image=self.original_photo, anchor=tk.NW)
#self.encoded_photo = ImageTk.PhotoImage(encoded_frame_pil)
#self.encoded_canvas.create_image(0, 0, image=self.encoded_photo, anchor=tk.NW)
# Update play button state
self.play_btn.config(state=tk.NORMAL)
self.reset_btn.config(state=tk.NORMAL)
except Exception as e:
print(f"Error loading and displaying frames: {e}")
messagebox.showerror("Error", f"Could not load and display video frames: {e}")
def _encoding_finished_error(self, return_code, error_message):
"""Handle encoding error"""
self.status_var.set(f"Encoding failed with code {return_code}")
self.output_log.insert(tk.END, f"\nFFmpeg Error (Code {return_code}):\n{error_message}")
messagebox.showerror("Encoding Error", f"Video encoding failed! See output log for details. (Error Code: {return_code})")
def _encoding_finished_exception(self, e):
"""Handle unexpected encoding exception"""
print(f"Encoding error: {e}")
self.status_var.set(f"Encoding failed: {e}")
messagebox.showerror("Encoding Error", f"An error occurred during encoding: {e}")
def _run_encoding(self, cmd):
"""Run the FFmpeg encoding process"""
try:
# Ensure the output directory exists
os.makedirs(os.path.dirname(cmd[-1]), exist_ok=True)
# Start the FFmpeg process
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
bufsize=1
)
# Read and display output
for line in process.stderr:
self.output_log.insert(tk.END, line)
self.output_log.see(tk.END) # Scroll to the bottom
self.root.update_idletasks()
# Wait for the process to finish
process.wait()
# Update encoded video path
self.encoded_video_path = cmd[-1]
# Update encoded video stats
if os.path.exists(self.encoded_video_path):
encoded_size = os.path.getsize(self.encoded_video_path)
self.encoded_size_var.set(f"{encoded_size / (1024 * 1024):.2f} MB")
# Calculate size reduction
original_size = os.path.getsize(self.input_video_path)
reduction_percent = ((original_size - encoded_size) / original_size) * 100
self.size_reduction_var.set(f"{reduction_percent:.2f}%")
# Get encoded video resolution
cap = cv2.VideoCapture(self.encoded_video_path)
if cap.isOpened():
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.encoded_res_var.set(f"{width}x{height}")
cap.release()
# Enable video playback