Advertisement
Guest User

4ffmpeg-8.043

a guest
Dec 8th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 111.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # work in python3.4 ---
  5. # from urllib.request import url2pathname
  6. #----------------------
  7.  
  8. from gi.repository import Gtk, GObject, Vte
  9. from gi.repository import GLib
  10. import os
  11. import sys, subprocess, shlex, re
  12. from subprocess import call
  13. from math import pow
  14. try:
  15.         from urllib import url2pathname
  16. except ImportError:
  17.         from urllib.request import url2pathname
  18. from gi.repository import Pango
  19. import threading
  20. import time
  21.                
  22. class reuse_init(object):
  23.  
  24.  
  25.         def make_label(self, text):
  26.                 label = Gtk.Label(text)
  27.                 label.set_use_underline(True)
  28.                 label.set_alignment(1.0, 0.5)
  29.                 label.show()
  30.                 return label
  31.  
  32.         def make_slider_and_spinner(self, init, min, max, step, page, digits):
  33.                 controls = {'adj':Gtk.Adjustment(init, min, max, step, page), 'slider':Gtk.HScale(), 'spinner':Gtk.SpinButton()}
  34.                 controls['slider'].set_adjustment(controls['adj'])
  35.                 controls['slider'].set_draw_value(False)
  36.                 controls['spinner'].set_adjustment(controls['adj'])
  37.                 controls['spinner'].set_digits(digits)
  38.                 controls['slider'].show()
  39.                 controls['spinner'].show()
  40.                 return controls
  41.        
  42.         def make_frame_ag(self, f_shadow_type, f_label, alig_pad, gri_row_sp, gri_colu_sp, gri_homog):
  43.                 controls =  {'frame':Gtk.Frame(), 'ali':Gtk.Alignment(), 'grid':Gtk.Grid()}
  44.                 controls['frame'].set_shadow_type(f_shadow_type) # 3 GTK_SHADOW_ETCHED_IN , 1 GTK_SHADOW_IN
  45.                 controls['frame'].set_label(f_label)
  46.                 controls['ali'].set_padding(alig_pad, alig_pad, alig_pad, alig_pad)
  47.                 controls['frame'].add(controls['ali'])
  48.                 controls['grid'].set_row_spacing(gri_row_sp)
  49.                 controls['grid'].set_column_spacing(gri_colu_sp)
  50.                 controls['grid'].set_column_homogeneous(gri_homog) # True
  51.                 controls['ali'].add(controls['grid'])
  52.                 controls['frame'].show()
  53.                 controls['ali'].show()
  54.                 controls['grid'].show()
  55.                 return controls
  56.        
  57.         def make_spinbut(self, init, min, max, step, page, digits, numeric):
  58.                 controls = {'adj':Gtk.Adjustment(init, min, max, step, page), 'spinner':Gtk.SpinButton()}
  59.                 controls['spinner'].set_adjustment(controls['adj'])
  60.                 controls['spinner'].set_digits(digits)
  61.                 controls['spinner'].set_numeric(numeric)
  62.                 controls['spinner'].show()
  63.                 return controls
  64.        
  65.         def make_combobox(self, *vals):
  66.                 controls = {'list':Gtk.ListStore(str), 'cbox':Gtk.ComboBox(), 'cellr':Gtk.CellRendererText()}
  67.                 for i, v in enumerate(vals):
  68.                         controls['list'].append([v])
  69.                 controls['cbox'].set_model(controls['list'])      
  70.                 controls['cbox'].pack_start(controls['cellr'], True)
  71.                 controls['cbox'].add_attribute(controls['cellr'], "text", 0)
  72.                 controls['cbox'].set_active(0)
  73.                 controls['cbox'].show()
  74.                 return controls
  75.        
  76.         def hms2sec(self, hms):
  77.                 result = 0
  78.                 listin = hms.split(':')
  79.                 listin.reverse()
  80.                 for i in range(len(listin)):
  81.                         if listin[i].isdigit():
  82.                                 if i == 0:
  83.                                         mult = 1
  84.                                 if i == 1:
  85.                                         mult = 60
  86.                                 if i == 2:
  87.                                         mult = 3600
  88.                                 result = result + (int(listin[i])*mult)            
  89.                 return result
  90.        
  91.         def sec2hms(self, seconds):
  92.                 seconds = float(seconds)
  93.                 h = int(seconds/3600)
  94.                 if (h > 0):
  95.                         seconds = seconds - (h * 3600)
  96.                 m = int(seconds / 60)
  97.                 s = int(seconds - (m * 60))
  98.                 hms = "{0:0=2d}:{1:0=2d}:{2:0=2d}".format(h,m,s)
  99.                 return hms
  100.        
  101.         def adj_change(self, adjustment, value, lower, upper , step_increment, page_increment):
  102.                 adjustment.set_value(value)
  103.                 adjustment.set_lower(lower)
  104.                 adjustment.set_upper(upper)
  105.                 adjustment.set_step_increment(step_increment)
  106.                 adjustment.set_page_increment(page_increment)
  107.                 #adjustment.set_page_size(page_size)
  108.                
  109.         def check_out_file(self):
  110.                 name = os.path.splitext(Vars.basename)[0]
  111.                 newname = Vars.outdir + '/' + name + Vars.ext
  112.                 if os.path.exists(newname):                    
  113.                         for i in range(2,20):
  114.                                 newname = Vars.outdir + '/' + name + '_' + str(i) + '_' + Vars.ext
  115.                                 if (os.path.exists(newname) == False):
  116.                                                         newname = newname
  117.                                                         break
  118.                 else:
  119.                         newname = newname
  120.                        
  121.                 Vars.newname = newname
  122.                 self.f3_run_outdir_label.set_tooltip_text(Vars.newname)
  123.                
  124.         def humansize(self, namefile):
  125.                 try:
  126.                         nbytes = os.path.getsize(namefile)
  127.                 except:
  128.                         return '0'
  129.                 suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
  130.                 if nbytes == 0: return '0 B'
  131.                 i = 0
  132.                 while nbytes >= 1024 and i < len(suffixes)-1:
  133.                         nbytes /= 1024.
  134.                         i += 1
  135.                 f = "{0:0.1f}".format(nbytes)
  136.                 return "{0:>5s} {1:2s}".format(f, suffixes[i])
  137.        
  138.  
  139. # Abstract struct class      
  140. class Struct:
  141.        
  142.         def __init__ (self, *argv, **argd):
  143.                 if len(argd):
  144.                         # Update by dictionary
  145.                         self.__dict__.update (argd)
  146.                 else:
  147.                         # Update by position
  148.                         attrs = filter (lambda x: x[0:2] != "__", dir(self))
  149.                         for n in range(len(argv)):
  150.                                 setattr(self, attrs[n], argv[n])
  151.                
  152. class Tips(Struct):
  153.        
  154.         map_tooltip_str = ' use 0:0  or  0:0,0:1,0:2 '
  155.         time_tooltip_str = ' use: 90 or 00:01:30 '
  156.        
  157.         black_dt_init_str = 'Detect black frames can take some time,\ndepending on the length of the video.'
  158.        
  159.         faac_q_str = ' faac vbr: q 80 ~96k, 90 ~100k, 100 ~118k, 120 ~128k, 160 ~156k, def:100 '
  160.         lamecbr_str = " lame mp3: 32-320 Kb/s def:128 "
  161.         lamevbr_str = " lame mp3 vbr: q 0 ~245Kb/s, 5 ~130Kb/s, 6 ~115Kb/s, 9 ~65Kb/s, def:6 "
  162.         aac_cbr_str = " aac cbr: 32-320 Kb/s def:128 "
  163.         aak_cbr_str = " aac_fdk cbr: 64-320 Kb/s def:128 "
  164.         audio_samp_str = ' audio samplerate '
  165.         audio_channels_str = ' audio channels '
  166.         f3v_label_out_start = "-crf 21.3"
  167.         subcopy_tooltip_srt = ' false= srt->ass '
  168.         cpu_tooltip_srt = ' false= -thread 0 '
  169.         nometa_tooltip_srt = ' true= --map_metadata -1 '
  170.         debug_tooltip_srt = ' only print command in terminal '
  171.         preset_tooltip_str = ' x264 preset '
  172.         tune_tooltip_str = ' x264 tune '
  173.        
  174.  
  175.  
  176. class Vars(Struct):
  177.        
  178.         def version_from_filename():
  179.                 '''
  180.                filename must be:
  181.                4FFmpeg-n.nnn.py
  182.                '''
  183.                 try:
  184.                         version = str(sys.argv[0])
  185.                 except:
  186.                         version = 'N/A'
  187.                         return version
  188.                 try:
  189.                         version = version.split('-')[1]
  190.                 except:
  191.                         version = 'N/A'
  192.                         return version
  193.                 try:
  194.                         version = version[:-3]
  195.                 except:
  196.                         version = 'N/A'
  197.                         return version          
  198.                 return version
  199.        
  200.         ffmpeg_ex = 'ffmpeg'
  201.         ffplay_ex = 'ffplay'
  202.         ffprobe_ex = 'ffprobe'
  203.         final_vf_str = ""
  204.         final_af_str = ""
  205.         deint_str = ""
  206.         crop_str = ""
  207.         scalestr = ""
  208.         dn_str = ""
  209.         uns_str = ""
  210.         fr_str = ""
  211.         filter_test_str = ""
  212.         video_width = "0"
  213.         video_height = "0"
  214.         add_1_1_dar = True
  215.         maps_str = ""
  216.         time_final_str = ""
  217.         final_codec_str = "-c:a libfaac -q:a 100 -ar 48k -ac 2 -c:v libx264 -crf 21.3 -preset faster -tune film -x264opts ref=4:bframes=4:direct=auto:aq-strength=1.3:ssim"
  218.         final_other_str = ""
  219.         a_copy = 0
  220.         audionone = 0
  221.         v_copy = 0
  222.         audio_codec_str = "-c:a libfaac -q:a 100 -ar 48k -ac 2"
  223.         vcodec_is = ""
  224.         video_codec_str = ""
  225.         video_codec_str_more = "-preset faster -tune film -x264opts ref=4:bframes=4:direct=auto:aq-strength=1.3:ssim"
  226.         container_str = "-f matroska"
  227.         is_avi = 0
  228.         info_str = ''
  229.         blackdet_is_run = False
  230.         time_start = 0 # for cropdetect
  231.         first_run = 0
  232.         p3_command = ""
  233.         filename = ""
  234.         dirname = ""
  235.         basename = ""
  236.         in_file_size = ""
  237.         ext = ".mkv"
  238.         outdir = ""
  239.         newname = ""
  240.         duration = ""
  241.         duration_in_sec = 0
  242.         time_done = 0
  243.         time_done_1 = 0
  244.         out_file_size = ""
  245.         debug = 0
  246.         first_pass = 0
  247.         n_pass = 1
  248.         p3_comm_first_p = ""
  249.         final_command1 = ""
  250.         final_command2 = ""
  251.         eff_pass = 0
  252.         version = version_from_filename()
  253.         BLACKDT_DONE = False
  254.         VOLDT_DONE = False
  255.         ENCODING = False
  256.  
  257.        
  258. class ProgrammaGTK(reuse_init):
  259.        
  260.  
  261.         def __init__(self):
  262.                
  263.                
  264.                 # initialize variables
  265.                                
  266.                 self.make_ui()
  267.                
  268.                 self.make_p3_out_command()
  269.                 Vars.outdir = os.getcwd()
  270.                
  271.                 if (len(sys.argv) == 2):
  272.                         try:
  273.                                 self.filechooserbutton.set_filename(str(sys.argv[1]))
  274.                                 self.file_changed(0)
  275.                         except:
  276.                                 pass
  277.                
  278.                 # ----------------------------
  279.                
  280.                
  281.         def make_ui(self):
  282.  
  283.                 # ---------------------
  284.                 self.window =  Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
  285.                 self.window.connect("destroy", self.on_window_destroy)
  286.                 self.window.set_title("4FFmpeg")
  287.                 self.window.set_position(1) # 1 center, 0 none,
  288.                 self.window.set_border_width(3)
  289.                 self.window.set_default_size(500,-1)
  290.                 #----------------------------------
  291.                
  292.                 #page1 stuff VIDEO FILTERS------------------------------------------
  293.                                
  294.                 # page1 VBox -------------------------
  295.                 self.vbox1 = Gtk.VBox()
  296.                 self.vbox1.set_spacing(2)
  297.                
  298.                 # map --------------------------------------
  299.                 self.make_map()        
  300.                 # add map frame
  301.                 self.vbox1.pack_start(self.f_map['frame'], 1, 0, 0)
  302.                
  303.                 #time cut ------------------------------------
  304.                 self.make_time_cut()
  305.                 # add time frame
  306.                 self.vbox1.pack_start(self.f_time['frame'], 1, 0, 0)
  307.                
  308.                 # deinterlace -----------------------
  309.                 self.make_deinterlace()
  310.                 # add deinterlace frame
  311.                 self.vbox1.pack_start(self.f_deint['frame'], 1, 0, 0)
  312.                
  313.                 # crop ----------------------------------------
  314.                 self.make_crop()        
  315.                 # add crop frame -------------------
  316.                 self.vbox1.pack_start(self.frame_crop['frame'], 1, 0, 0)
  317.        
  318.                 # scale ------------------------------
  319.                 self.make_scale()
  320.                 # add scale frame------------------
  321.                 self.vbox1.pack_start(self.frame_scale['frame'], 1, 0, 0)
  322.  
  323.                 # denoise ----------------------------
  324.                 self.make_denoise()
  325.                 # add denoise frame------------------------
  326.                 self.vbox1.pack_start(self.frame_dn['frame'], 1, 0, 0)
  327.                
  328.                 #test-------------------
  329.                 #self.f_test = self.make_frame_ag(3, "TEST" , 6, 8, 8, True)
  330.                 #self.label_test = Gtk.Label("TEST")
  331.                 #self.f_test['grid'].attach(self.label_test, 0, 0, 1, 1)
  332.  
  333.                 #self.vbox1.pack_start(self.f_test['frame'], 1, 0, 0)
  334.                 #-------------------------------------
  335.                
  336.                 # page 1 output------------------------
  337.                 self.frame_out = self.make_frame_ag(3, '  FFmpeg video filter string:  ' , 12, 8, 8, True)
  338.                 self.labelout = Gtk.Label("")
  339.                 self.labelout.set_selectable(1)
  340.                 self.labelout.set_width_chars(90)
  341.                 self.labelout.set_max_width_chars(90)
  342.                 self.labelout.set_line_wrap(True)
  343.                 self.frame_out['grid'].attach(self.labelout, 0, 0, 1 ,1)
  344.                 # add output frame ---------------------
  345.                 self.vbox1.pack_start(self.frame_out['frame'], 1, 0, 0)
  346.                
  347.                 # end page1 stuff
  348.                
  349.                
  350.                 # page2 stuff  UTILITY ------------------------------------------
  351.                
  352.                 # page2 VBox -------------------------
  353.                 self.vbox_pg2 = Gtk.VBox()
  354.                 self.vbox_pg2.set_spacing(2)
  355.                
  356.                 #volumedetect ------------------------------------
  357.                 self.make_volume_det()
  358.                 # add volumedetect frame        
  359.                 self.vbox_pg2.pack_start(self.f_volume_det['frame'], 1, 0, 0)
  360.                
  361.                 # black detect ----------------------------------
  362.                 self.make_black_detect()
  363.                 # add black_detect frame
  364.                 self.vbox_pg2.pack_start(self.f_blackd['frame'], 1, 0, 0)
  365.                
  366.                 # info frame----------------------
  367.                 self.make_info_frame()
  368.                 # add info frame
  369.                 self.vbox_pg2.pack_start(self.f_info['frame'], 1, 0, 0)
  370.                
  371.                 # end page2 stuff
  372.                
  373.                 # page 3 stuff  ffmpeg commands -----------------------------------------
  374.                
  375.                 # page3 VBox -------------------------
  376.                 self.vbox_pg3 = Gtk.VBox()
  377.                 self.vbox_pg3.set_spacing(2)
  378.                
  379.                 # audio codec -------------------
  380.                 self.make_audio_codec()
  381.                 #add frame audio codec
  382.                 self.vbox_pg3.pack_start(self.f_3acodec['frame'], 1, 0, 0)
  383.                
  384.                 # video codec -------------------
  385.                 self.make_video_codec()
  386.                 #add frame video codec
  387.                 self.vbox_pg3.pack_start(self.f_3vcodec['frame'], 1, 0, 0)
  388.                
  389.                 # other options ----------------
  390.                 self.make_other_opts()
  391.                 #add frame other options
  392.                 self.vbox_pg3.pack_start(self.f3_oth['frame'], 1, 0, 0)
  393.                
  394.                 # page3 output ---------------------
  395.                 self.f3_out = self.make_frame_ag(3, "  FFmpeg command  " , 6, 8, 8, True)
  396.                 self.f3_out_label = Gtk.Label("")
  397.                 self.f3_out_label.set_width_chars(106)
  398.                 self.f3_out_label.set_max_width_chars(106)
  399.                 self.f3_out_label.set_line_wrap(True)
  400.                 self.f3_out['grid'].attach(self.f3_out_label, 0, 0, 1, 1)
  401.                 # add frame page3 output
  402.                 self.vbox_pg3.pack_start(self.f3_out['frame'], 1, 0, 0)
  403.                
  404.                 # run ffmpeg ---------------------------------
  405.                 self.make_run_ffmpeg()
  406.                 # add frame run ffmpeg
  407.                 self.vbox_pg3.pack_start(self.f3_run['frame'], 1, 0, 0)
  408.                
  409.                
  410.                 #---------------------------------------------------
  411.                 # main vbox
  412.                 self.main_vbox = Gtk.VBox()
  413.                
  414.                 # notebook      
  415.                 self.notebook = Gtk.Notebook()
  416.                 self.notebook.set_tab_pos(0)  #GTK_POS_LEFT
  417.                 self.notebook.set_show_border (False)
  418.                 self.main_vbox.pack_start(self.notebook, 1, 0, 0)
  419.                
  420.                 #page 1
  421.                 self.tab1label = Gtk.Label("Video filters")
  422.                 self.page1_ali = Gtk.Alignment()
  423.                 self.page1_ali.set_padding(0, 6, 6, 6)
  424.                 self.page1_ali.add(self.vbox1)
  425.                 self.notebook.append_page(self.page1_ali, self.tab1label)
  426.                
  427.                 #page 2
  428.                 self.tab2label = Gtk.Label("Utility")
  429.                 self.page2_ali = Gtk.Alignment()
  430.                 self.page2_ali.set_padding(0, 6, 6, 6)
  431.                 self.page2_ali.add(self.vbox_pg2)
  432.                 self.notebook.append_page(self.page2_ali, self.tab2label)
  433.                
  434.                 #page 3
  435.                 self.tab3label = Gtk.Label("Encode")
  436.                 self.page3_ali = Gtk.Alignment()
  437.                 self.page3_ali.set_padding(0, 6, 6, 6)
  438.                 self.page3_ali.add(self.vbox_pg3)
  439.                 self.notebook.append_page(self.page3_ali, self.tab3label)
  440.                
  441.                 #---------------------------------------------------
  442.                
  443.                 # low part BUTTONS AND TERM --------------------------
  444.                 self.make_low_part()
  445.                 # add low part-----------------
  446.                 self.main_vbox.pack_start(self.gridterm, 1, 1, 0)
  447.                
  448.                 #---------------------------------------------------------
  449.                 self.window.add (self.main_vbox)
  450.                 #---------------------------------
  451.                 self.window.show_all()          
  452.  
  453.                
  454.         def make_deinterlace(self):
  455.                 self.f_deint = self.make_frame_ag(3, "" , 6, 8, 8, True)
  456.                 self.checkdeint = Gtk.CheckButton("deinterlace")
  457.                 self.checkdeint.connect("toggled", self.on_deint_clicked)
  458.                 self.f_deint['grid'].attach(self.checkdeint, 0, 0, 1, 1)
  459.                
  460.                 self.box_deint = self.make_combobox(
  461.                   "yadif",
  462.                   "postprocessing linear blend",
  463.                   "detect (use only with Test)"
  464.                   )
  465.                 self.f_deint['grid'].attach(self.box_deint['cbox'], 1, 0, 1, 1)
  466.                 self.box_deint['cbox'].connect("changed", self.on_deint_clicked)
  467.                 #
  468.                 self.f_deint['grid'].attach(self.make_label(""), 2, 0, 1, 1)
  469.        
  470.         def make_map(self):
  471.                 #
  472.                 self.f_map = self.make_frame_ag(3, "" , 6, 8, 8, True)
  473.                 #
  474.                 self.check_map = Gtk.CheckButton("map")
  475.                 self.check_map.connect("toggled", self.on_map_clicked) # toggled or activate (enter)
  476.                 self.f_map['grid'].attach(self.check_map, 0, 0, 1, 1)
  477.                 #
  478.                 self.mapstream_label = self.make_label("streams: ")
  479.                 self.f_map['grid'].attach(self.mapstream_label, 1, 0, 1, 1)
  480.                 #
  481.                 self.entry_map = Gtk.Entry()
  482.                 self.entry_map.set_tooltip_text(Tips.map_tooltip_str)
  483.                 self.entry_map.connect("changed", self.on_map_clicked)
  484.                 self.f_map['grid'].attach(self.entry_map, 2, 0, 1, 1)
  485.                 #
  486.                 self.map_label = Gtk.Label("")
  487.                 self.f_map['grid'].attach(self.map_label, 3, 0, 1, 1)
  488.                 # placeholder
  489.                 self.f_map['grid'].attach(self.make_label("Version: " + Vars.version + "   "), 4, 0, 1, 1)
  490.                
  491.         def make_time_cut(self):
  492.                 #
  493.                 self.f_time = self.make_frame_ag(3, "" , 6, 8, 8, True)
  494.                 #
  495.                 self.check_time = Gtk.CheckButton("time")
  496.                 self.check_time.connect("toggled", self.on_time_clicked) # toggled or activate (enter)
  497.                 self.f_time['grid'].attach(self.check_time, 0, 0, 1, 1)
  498.                 #
  499.                 self.label_time_in = self.make_label("from: ")
  500.                 self.f_time['grid'].attach(self.label_time_in, 1, 0, 1, 1)
  501.                 self.entry_timein = Gtk.Entry()
  502.                 self.entry_timein.set_tooltip_text(Tips.time_tooltip_str)
  503.                 self.entry_timein.set_max_length(8)
  504.                 self.entry_timein.set_max_width_chars(8)
  505.                 self.entry_timein.set_width_chars(8)
  506.                 #self.entry_timein.set_halign(1)
  507.                 self.entry_timein.connect("changed", self.on_time_clicked)
  508.                 self.f_time['grid'].attach(self.entry_timein, 2, 0, 1, 1)
  509.                
  510.                 self.label_time_out = self.make_label("to: ")
  511.                 self.f_time['grid'].attach(self.label_time_out, 3, 0, 1, 1)
  512.                 self.entry_timeout = Gtk.Entry()
  513.                 self.entry_timeout.set_tooltip_text(Tips.time_tooltip_str)
  514.                 self.entry_timeout.set_max_length(8)
  515.                 self.entry_timeout.set_max_width_chars(8)
  516.                 self.entry_timeout.set_width_chars(8)
  517.                 self.entry_timeout.connect("changed", self.on_time_clicked)
  518.                 self.f_time['grid'].attach(self.entry_timeout, 4, 0, 1, 1)
  519.                 self.time_label = Gtk.Label('')
  520.                 self.f_time['grid'].attach(self.time_label, 5, 0, 2, 1)
  521.        
  522.         def make_scale(self):
  523.                 self.frame_scale = self.make_frame_ag(3, '  scale  ' , 6, 8, 8, True)
  524.                 # ------------------------------
  525.                 self.box1 = self.make_combobox(
  526.                   "None",
  527.                   "W",
  528.                   "H"
  529.                   )
  530.                 self.frame_scale['grid'].attach(self.box1['cbox'], 0, 0, 1, 1)
  531.                 self.box1['cbox'].connect("changed", self.on_scale_clicked)
  532.                 #------------------------------------
  533.                 self.box2 = self.make_combobox(
  534.                   "16/9",
  535.                   "4/3"
  536.                   )
  537.                 self.frame_scale['grid'].attach(self.box2['cbox'], 0, 1, 1, 1)
  538.                 self.box2['cbox'].connect("changed", self.on_scale_clicked)
  539.                 #-----------------------------------------
  540.                 self.size_spinner = self.make_slider_and_spinner(720, 200, 2000, 8, 32, 0)
  541.                 self.frame_scale['grid'].attach(self.size_spinner['slider'], 1, 0, 1, 1)
  542.                 self.frame_scale['grid'].attach(self.size_spinner['spinner'], 2, 0, 1, 1)
  543.                 #self.size_spinner['slider'].set_size_request(150,-1)
  544.                 self.size_spinner['adj'].set_value(720.001) #mettere decimali se no non si vede
  545.                 self.size_spinner['adj'].connect("value-changed", self.on_scale_clicked)
  546.                 #-------------------------------------------------------
  547.                 self.check = Gtk.CheckButton("lanczos")
  548.                 self.check.connect("toggled", self.on_scale_clicked)
  549.                 self.frame_scale['grid'].attach(self.check, 2, 1, 1, 1)
  550.                
  551.         def make_crop(self):
  552.                 self.frame_crop = self.make_frame_ag(3, '' , 6, 2, 0, True)
  553.                 # -------
  554.                 self.checkcr = Gtk.CheckButton("crop")
  555.                 self.checkcr.connect("toggled", self.on_crop_clicked)
  556.                 self.frame_crop['grid'].attach(self.checkcr, 0, 0, 1, 1)
  557.                 #----------
  558.                 self.butcrode = Gtk.Button(label="Crop detect")
  559.                 self.butcrode.connect("clicked", self.on_butcrode_clicked)
  560.                 self.butcrode.set_sensitive(False)
  561.                 self.frame_crop['grid'].attach(self.butcrode, 0, 1, 1, 1)
  562.                 #-----------
  563.                 self.labelcropinw = self.make_label("input W: ")
  564.                 self.frame_crop['grid'].attach(self.labelcropinw, 1, 0, 1, 1)
  565.                 self.labelcropinh = self.make_label("input H: ")
  566.                 self.frame_crop['grid'].attach(self.labelcropinh, 1, 1, 1, 1)
  567.                 #
  568.                 self.spincw = self.make_spinbut(640 , 100 , 1920 , 10 , 1 , 0, True )
  569.                 self.spincw["adj"].connect("value-changed", self.on_crop_clicked)
  570.                 self.frame_crop['grid'].attach(self.spincw["spinner"], 2, 0, 1, 1)
  571.                 #
  572.                 self.spinch = self.make_spinbut(480 , 100 , 1280 , 10 , 1 , 0, True )
  573.                 self.spinch["adj"].connect("value-changed", self.on_crop_clicked)
  574.                 self.frame_crop['grid'].attach(self.spinch["spinner"], 2, 1, 1, 1)
  575.                 #
  576.                
  577.                 self.labelcroptop = self.make_label("crop Top: ")
  578.                 self.frame_crop['grid'].attach(self.labelcroptop, 3, 0, 1, 1)
  579.                 self.labelcropbot = self.make_label("crop Bottom: ")
  580.                 self.frame_crop['grid'].attach(self.labelcropbot, 3, 1, 1, 1)
  581.                 #
  582.                
  583.                 self.spinct = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  584.                 self.spinct["adj"].connect("value-changed", self.on_crop_clicked)
  585.                 self.frame_crop['grid'].attach(self.spinct["spinner"], 4, 0, 1, 1)
  586.                 #
  587.                 self.spincb = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  588.                 self.spincb["adj"].connect("value-changed", self.on_crop_clicked)
  589.                 self.frame_crop['grid'].attach(self.spincb["spinner"], 4, 1, 1, 1)
  590.                 #
  591.                
  592.                 self.labelcroplef = self.make_label("crop Left: ")
  593.                 self.frame_crop['grid'].attach(self.labelcroplef, 5, 0, 1, 1)
  594.                 self.labelcroprig = self.make_label("crop Right: ")
  595.                 self.frame_crop['grid'].attach(self.labelcroprig, 5, 1, 1, 1)
  596.                 #
  597.                 self.spincl = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  598.                 self.spincl["adj"].connect("value-changed", self.on_crop_clicked)
  599.                 self.frame_crop['grid'].attach(self.spincl["spinner"], 6, 0, 1, 1)
  600.                 #
  601.                 self.spincr = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  602.                 self.spincr["adj"].connect("value-changed", self.on_crop_clicked)
  603.                 self.frame_crop['grid'].attach(self.spincr["spinner"], 6, 1, 1, 1)
  604.                
  605.                
  606.         def make_denoise(self):
  607.                 self.frame_dn = self.make_frame_ag(3, '  denoise / unsharp luma chroma / frame rate   ' , 6, 8, 8, True)
  608.                 # denoise ----------------------
  609.                 self.checkdn = Gtk.CheckButton("hqdn3d:")
  610.                 self.checkdn.set_alignment(1.0, 0.5)
  611.                 self.checkdn.set_halign(2)
  612.                 self.checkdn.connect("toggled", self.on_dn_clicked)
  613.                 self.frame_dn['grid'].attach(self.checkdn, 0, 0, 1, 1)
  614.                 #
  615.                 self.spindn = self.make_spinbut(4 , 2 , 8 , 1 , 10 , 0, True )
  616.                 self.spindn["adj"].connect("value-changed", self.on_dn_clicked)
  617.                 #self.spindn['spinner'].set_max_length(1)
  618.                 #self.spindn['spinner'].set_width_chars(1)
  619.                 self.frame_dn['grid'].attach(self.spindn["spinner"], 1, 0, 1, 1)
  620.                 # -----------------------
  621.                 # unsharp unsharp=5:5:luma:5:5:chroma, luma chroma -1.5 to 1.5
  622.                 self.checkuns = Gtk.CheckButton("unsharp l/c:")
  623.                 self.checkuns.set_alignment(1.0, 0.5)
  624.                 self.checkuns.set_halign(2)
  625.                 self.checkuns.connect("toggled", self.on_uns_clicked)
  626.                 self.frame_dn['grid'].attach(self.checkuns, 2, 0, 1, 1)
  627.                 self.spinunsl = self.make_spinbut(0.0 , -1.5 , 1.5 , .1 , .01 , 2, True )
  628.                 self.spinunsl["adj"].connect("value-changed", self.on_uns_clicked)
  629.                 self.frame_dn['grid'].attach(self.spinunsl["spinner"], 3, 0, 1, 1)
  630.                 self.spinunsc = self.make_spinbut(0.0 , -1.5 , 1.5 , .1 , .01 , 2, True )
  631.                 self.spinunsc["adj"].connect("value-changed", self.on_uns_clicked)
  632.                 self.frame_dn['grid'].attach(self.spinunsc["spinner"], 4, 0, 1, 1)
  633.                 # framerate --------------------------
  634.                 self.checkfr = Gtk.CheckButton("frame rate:")
  635.                 self.checkfr.set_alignment(1.0, 0.5)
  636.                 self.checkfr.set_halign(2)
  637.                 self.checkfr.connect("toggled", self.on_fr_clicked)
  638.                 self.frame_dn['grid'].attach(self.checkfr, 5, 0, 1, 1)
  639.                 self.spinfr = self.make_spinbut(0 , 0 , 100 , 1 , 10 , 2, True )
  640.                 self.spinfr["adj"].connect("value-changed", self.on_fr_clicked)
  641.                 self.frame_dn['grid'].attach(self.spinfr["spinner"], 6, 0, 1, 1)
  642.                
  643.         def make_low_part(self):
  644.                 # grid
  645.                 self.gridterm = Gtk.Grid()
  646.                 self.gridterm.set_row_spacing(2)
  647.                 self.gridterm.set_column_spacing(2)
  648.                 self.gridterm.set_column_homogeneous(True) # True
  649.                 # filechooserbutton
  650.                 self.filechooserbutton = Gtk.FileChooserButton(title="FileChooserButton")
  651.                 #
  652.                 #self.filechooserbutton.set_action(2) # 0 open file, 1 save file, 2 select folder, 3 create folder
  653.                 #
  654.                 self.filechooserbutton.connect("file-set", self.file_changed)  
  655.                 # filter
  656.                 self.filter = Gtk.FileFilter()
  657.                 self.filter.set_name("Video")
  658.                 self.filter.add_mime_type("video/x-matroska")
  659.                 self.filter.add_mime_type("video/mp4")
  660.                 self.filter.add_mime_type("video/x-flv")
  661.                 self.filter.add_mime_type("video/avi")
  662.                 self.filter.add_mime_type("video/mpg")
  663.                 self.filter.add_mime_type("video/mpeg")
  664.                 self.filter.add_mime_type("video/x-ms-wmv")
  665.                 self.filter.add_mime_type("video/webm")
  666.                 self.filter.add_mime_type("video/ogg")
  667.                 self.filter.add_mime_type("video/quicktime")
  668.                 self.filechooserbutton.add_filter(self.filter)
  669.                 # terminal
  670.                 self.terminal     = Vte.Terminal()
  671.                 self.terminal.fork_command_full(
  672.                         Vte.PtyFlags.DEFAULT, #default is fine
  673.                         os.getcwd(), #where to start the command?   os.environ['HOME']
  674.                         ["/bin/sh"], #where is the emulator?
  675.                         [], #it's ok to leave this list empty
  676.                         GLib.SpawnFlags.DO_NOT_REAP_CHILD,
  677.                         None, #at least None is required
  678.                         None,
  679.                         )
  680.                 self.scroller = Gtk.ScrolledWindow()
  681.                 self.scroller.set_hexpand(True)
  682.                 self.scroller.set_vexpand(True)
  683.                 self.scroller.add(self.terminal)
  684.                 self.scroller.set_min_content_height(90)
  685.                 #ff command button
  686.                 self.butplay = Gtk.Button(label="FFplay")
  687.                 self.butplay.connect("clicked", self.on_butplay_clicked)
  688.                 self.butplay.set_sensitive(False)
  689.                 self.butprobe = Gtk.Button(label="FFprobe")
  690.                 self.butprobe.connect("clicked", self.on_butprobe_clicked)
  691.                 self.butprobe.set_sensitive(False)
  692.                 self.buttest = Gtk.Button(label="Test")
  693.                 self.buttest.connect("clicked", self.on_buttest_clicked)
  694.                 self.buttest.set_sensitive(False)
  695.                 self.buttestspl = Gtk.Button(label="Test split")
  696.                 self.buttestspl.connect("clicked", self.on_buttestspl_clicked)
  697.                 self.buttestspl.set_sensitive(False)
  698.                
  699.                 self.gridterm.attach(self.filechooserbutton, 0, 0, 2, 1)
  700.                 self.gridterm.attach(self.butprobe, 2, 0, 1, 1)
  701.                 self.gridterm.attach(self.butplay, 3, 0, 1, 1)
  702.                 self.gridterm.attach(self.buttest, 4, 0, 1, 1)
  703.                 self.gridterm.attach(self.buttestspl, 5, 0, 1, 1)
  704.                
  705.                 self.gridterm.attach(self.scroller, 0, 1, 6, 1)
  706.  
  707.         def make_volume_det(self):
  708.                 self.f_volume_det = self.make_frame_ag(3, "" , 6, 8, 8, True)
  709.                 #
  710.                 self.but_volume_det = Gtk.Button(label="Volume detect")
  711.                 self.but_volume_det.connect("clicked", self.on_but_volume_det_clicked)
  712.                 self.f_volume_det['grid'].attach(self.but_volume_det, 0, 0, 1, 1)
  713.                 self.but_volume_det.set_sensitive(False)
  714.                 #
  715.                 self.voldet_spin = Gtk.Spinner()
  716.                 self.voldet_spin.set_sensitive(False)
  717.                 self.f_volume_det['grid'].attach(self.voldet_spin, 1, 0, 1, 1)
  718.                 #
  719.                 self.label_maxvol = Gtk.Label("")
  720.                 self.f_volume_det['grid'].attach(self.label_maxvol, 2, 0, 1, 1)
  721.                 self.label_meanvol = Gtk.Label("")
  722.                 self.f_volume_det['grid'].attach(self.label_meanvol, 3, 0, 1, 1)
  723.                 #
  724.                 self.check_vol = Gtk.CheckButton("volume")
  725.                 self.check_vol.connect("toggled", self.on_volume_clicked)
  726.                 self.f_volume_det['grid'].attach(self.check_vol, 0, 1, 1, 1)
  727.                 #
  728.                 self.label_db = self.make_label("dB: ")
  729.                 self.f_volume_det['grid'].attach(self.label_db, 1, 1, 1, 1)
  730.                 self.spin_db = self.make_spinbut(0 , -6 , 20 , 0.1 , 1 , 2, True )
  731.                 self.spin_db["adj"].connect("value-changed", self.on_volume_clicked)
  732.                 self.f_volume_det['grid'].attach(self.spin_db["spinner"], 2, 1, 1, 1)
  733.                 self.label_ratio = self.make_label("ratio: ")
  734.                 self.f_volume_det['grid'].attach(self.label_ratio, 3, 1, 1, 1)
  735.                 self.label_ratio_val = Gtk.Label("")
  736.                 self.f_volume_det['grid'].attach(self.label_ratio_val, 4, 1, 1, 1)
  737.  
  738.         def make_black_detect(self):
  739.                 self.f_blackd = self.make_frame_ag(3, "" , 6, 8, 8, True)
  740.                 #
  741.                 self.but_black_det = Gtk.Button(label="Black detect")
  742.                 self.but_black_det.connect("clicked", self.on_but_black_det_clicked)
  743.                 self.but_black_det.set_sensitive(False)
  744.                 self.f_blackd['grid'].attach(self.but_black_det, 0, 0, 1, 1)
  745.                 #
  746.                 self.blackdet_spin = Gtk.Spinner()
  747.                 self.blackdet_spin.set_sensitive(False)
  748.                 self.f_blackd['grid'].attach(self.blackdet_spin, 1, 0, 1, 1)
  749.                 #
  750.                 scrolledwindow = Gtk.ScrolledWindow()
  751.                 #scrolledwindow.set_hexpand(True)
  752.                 scrolledwindow.set_vexpand(True)
  753.                 self.textview = Gtk.TextView()
  754.                 self.textbuffer = self.textview.get_buffer()
  755.                 self.textview.set_editable(False)
  756.                 self.textview.set_cursor_visible(False)
  757.                 self.textview.modify_font(Pango.font_description_from_string('Monospace 9'))
  758.                 scrolledwindow.add(self.textview)
  759.                 self.textbuffer.set_text(Tips.black_dt_init_str)
  760.                 self.f_blackd['grid'].attach(scrolledwindow, 2, 0, 3, 3)
  761.                 #
  762.                 self.progressbar_black_det = Gtk.ProgressBar()
  763.                 self.f_blackd['grid'].attach(self.progressbar_black_det, 0, 1, 1, 1)
  764.                
  765.         def make_info_frame(self):
  766.                 self.f_info = self.make_frame_ag(3, "  info  " , 6, 8, 8, True)
  767.                 scrolledwindow1 = Gtk.ScrolledWindow()
  768.                 scrolledwindow1.set_hexpand(True)
  769.                 scrolledwindow1.set_vexpand(True)
  770.                 self.textview_info = Gtk.TextView()
  771.                 self.textbuffer_info = self.textview_info.get_buffer()
  772.                 self.textview_info.set_editable(False)
  773.                 self.textview_info.set_cursor_visible(False)
  774.                 self.textview_info.modify_font(Pango.font_description_from_string('Monospace 9'))
  775.                 scrolledwindow1.add(self.textview_info)
  776.                 self.f_info['grid'].attach(scrolledwindow1, 0, 0, 3, 3)
  777.        
  778.         def make_audio_codec(self):
  779.                 #
  780.                 self.f_3acodec = self.make_frame_ag(3, "  audio codec  " , 6, 8, 8, True)
  781.                 #
  782.                 self.box_ac = self.make_combobox(
  783.                   "libfaac quality (Q)",
  784.                   "aacplus (64 Kb/s)",
  785.                   "aacplus (32 Kb/s)",
  786.                   "no audio",
  787.                   "audio copy",
  788.                   "aac_fdk CBR (Kb/s)",
  789.                   "lame mp3 CBR (Kb/s)",
  790.                   "lame mp3 VBR (Q)",
  791.                   "aac native CBR (Kb/s)"
  792.                   )
  793.                 self.f_3acodec['grid'].attach(self.box_ac['cbox'], 0, 0, 1, 1)
  794.                 self.box_ac['cbox'].connect("changed", self.on_codec_audio_changed)
  795.                 #
  796.                 self.spin_ac = self.make_spinbut(100 , 80 , 160 , 10 , 1 , 0, True )
  797.                 self.spin_ac["adj"].connect("value-changed", self.on_codec_clicked)
  798.                 self.f_3acodec['grid'].attach(self.spin_ac["spinner"], 1, 0, 1, 1)
  799.                 self.spin_ac['spinner'].set_tooltip_text(Tips.faac_q_str)
  800.                 #
  801.                 self.box_audio_rate = self.make_combobox(
  802.                         "48000",
  803.                         "44100"
  804.                         )
  805.                 self.box_audio_rate['cbox'].set_tooltip_text(Tips.audio_samp_str)
  806.                 self.box_audio_rate['cbox'].connect("changed", self.on_codec_audio_changed)
  807.                 self.f_3acodec['grid'].attach(self.box_audio_rate['cbox'], 2, 0, 1, 1)
  808.                 #
  809.                 self.box_audio_ch = self.make_combobox(
  810.                         "2",
  811.                         "1"
  812.                         )
  813.                 self.box_audio_ch['cbox'].set_tooltip_text(Tips.audio_channels_str)
  814.                 self.box_audio_ch['cbox'].connect("changed", self.on_codec_audio_changed)
  815.                 self.f_3acodec['grid'].attach(self.box_audio_ch['cbox'], 3, 0, 1, 1)
  816.        
  817.         def make_video_codec(self):
  818.                 self.f_3vcodec = self.make_frame_ag(3, "  video codec  " , 6, 8, 8, True)
  819.                 self.box_vcodec = self.make_combobox(
  820.                         "libx264",
  821.                         "video copy",
  822.                         "mpeg4"
  823.                         )
  824.                 self.f_3vcodec['grid'].attach(self.box_vcodec['cbox'], 0, 0, 1, 1)
  825.                 self.box_vcodec['cbox'].connect("changed", self.on_codec_video_changed)
  826.                 #
  827.                 self.box_vrc = self.make_combobox(
  828.                         "crf",
  829.                         "bitrate kb/s"
  830.                         )
  831.                 self.f_3vcodec['grid'].attach(self.box_vrc['cbox'], 1, 0, 1, 1)
  832.                 self.box_vrc['cbox'].connect("changed", self.on_codec_vrc_changed)
  833.                 #
  834.                 self.spin_vc = self.make_spinbut(21.3 , 15 , 40 , 0.1 , 1 , 1, True )
  835.                 self.spin_vc["adj"].connect("value-changed", self.on_codec_vrc_value_change)
  836.                 self.f_3vcodec['grid'].attach(self.spin_vc["spinner"], 2, 0, 1, 1)
  837.                 #
  838.                 self.f3v_label_out = Gtk.Label("")
  839.                 self.f_3vcodec['grid'].attach(self.f3v_label_out, 3, 0, 2, 1)
  840.                 # x264 preset
  841.                 self.box_3v_preset = self.make_combobox(
  842.                         "superfast",
  843.                         "veryfast",
  844.                         "faster",
  845.                         "fast",
  846.                         "medium",
  847.                         "slow"
  848.                         ) #(-superfast -veryfast -fast, -medium, -slow, default: faster)
  849.                 self.box_3v_preset['cbox'].set_active(2)
  850.                 self.box_3v_preset['cbox'].set_tooltip_text(Tips.preset_tooltip_str)
  851.                 self.f_3vcodec['grid'].attach(self.box_3v_preset['cbox'], 0, 1, 1, 1)
  852.                 self.box_3v_preset['cbox'].connect("changed", self.on_preset_changed)
  853.                 # x264 tune
  854.                 self.box_3v_tune = self.make_combobox(
  855.                         "film",
  856.                         "animation",
  857.                         "grain",
  858.                         "stillimage"
  859.                         ) # film,animation,grain,stillimage
  860.                 self.box_3v_tune['cbox'].set_active(0)
  861.                 self.box_3v_tune['cbox'].set_tooltip_text(Tips.tune_tooltip_str)
  862.                 self.f_3vcodec['grid'].attach(self.box_3v_tune['cbox'], 1, 1, 1, 1)
  863.                 self.box_3v_tune['cbox'].connect("changed", self.on_preset_changed)
  864.                 # x264 opts
  865.                 self.box_3v_x264opts = self.make_combobox(
  866.                         "x264opts 1",
  867.                         "no opts (ssim)"
  868.                         )
  869.                 self.box_3v_x264opts['cbox'].set_active(0)
  870.                 self.f_3vcodec['grid'].attach(self.box_3v_x264opts['cbox'], 2, 1, 1, 1)
  871.                 self.box_3v_x264opts['cbox'].connect("changed", self.on_codec_clicked)
  872.                 # opencl
  873.                 self.check_3v_opencl = Gtk.CheckButton("opencl")
  874.                 self.check_3v_opencl.connect("toggled", self.on_codec_clicked)
  875.                 self.f_3vcodec['grid'].attach(self.check_3v_opencl, 3, 1, 1, 1)
  876.                
  877.                
  878.                
  879.         def make_other_opts(self):
  880.                 #
  881.                 self.f3_oth = self.make_frame_ag(3, "  other options  " , 6, 8, 8, True)
  882.                 #
  883.                 self.check_2pass = Gtk.CheckButton("2 pass")
  884.                 self.check_2pass.connect("toggled", self.on_f3_other_clicked)
  885.                 self.f3_oth['grid'].attach(self.check_2pass, 0, 0, 1, 1)
  886.                 self.check_2pass.set_sensitive(False)
  887.                 #
  888.                 self.check_nometa = Gtk.CheckButton("no metatag")
  889.                 self.check_nometa.connect("toggled", self.on_f3_other_clicked)
  890.                 self.check_nometa.set_tooltip_text(Tips.nometa_tooltip_srt)
  891.                 self.f3_oth['grid'].attach(self.check_nometa, 1, 0, 1, 1)
  892.                 #
  893.                 #
  894.                 self.check_scopy = Gtk.CheckButton("sub copy")
  895.                 self.check_scopy.connect("toggled", self.on_f3_other_clicked)
  896.                 self.check_scopy.set_tooltip_text(Tips.subcopy_tooltip_srt)
  897.                 self.f3_oth['grid'].attach(self.check_scopy, 2, 0, 1, 1)
  898.                 #
  899.                 self.check_cpu = Gtk.CheckButton("cpu 3")
  900.                 self.check_cpu.connect("toggled", self.on_f3_other_clicked)
  901.                 self.check_cpu.set_tooltip_text(Tips.cpu_tooltip_srt)
  902.                 self.f3_oth['grid'].attach(self.check_cpu, 3, 0, 1, 1)
  903.                 #
  904.                 self.check_debug = Gtk.CheckButton("debug")
  905.                 self.check_debug.connect("toggled", self.on_f3_other_clicked)
  906.                 self.check_debug.set_tooltip_text(Tips.debug_tooltip_srt)
  907.                 self.f3_oth['grid'].attach(self.check_debug, 4, 0, 1, 1)
  908.                 #
  909.                 self.box_oth_container = self.make_combobox(
  910.                   "MKV",
  911.                   "MP4",
  912.                   "AVI"
  913.                   )
  914.                 self.f3_oth['grid'].attach(self.box_oth_container['cbox'], 5, 0, 1, 1)
  915.                 self.box_oth_container['cbox'].connect("changed", self.on_container_changed)
  916.                
  917.         def make_run_ffmpeg(self):
  918.                 self.f3_run = self.make_frame_ag(3, "  execute in terminal  " , 6, 8, 8, True)
  919.                 #
  920.                 self.f3_run_indir_label = Gtk.Label("")
  921.                 self.f3_run['grid'].attach(self.f3_run_indir_label, 0, 0, 1, 1)
  922.                 self.f3_run_file = Gtk.Label("no input file")
  923.                 self.f3_run_file.set_tooltip_text('')
  924.                 self.f3_run['grid'].attach(self.f3_run_file, 1, 0, 2, 1)
  925.                 self.f3_dur_file = Gtk.Label("")
  926.                 self.f3_run['grid'].attach(self.f3_dur_file, 3, 0, 1, 1)
  927.                 #
  928.                 self.but_f3_run = Gtk.Button(label="RUN")
  929.                 self.but_f3_run.connect("clicked", self.on_but_f3_run_clicked)
  930.                 self.but_f3_run.set_sensitive(False)
  931.                 self.f3_run['grid'].attach(self.but_f3_run, 3, 1, 1, 1)
  932.                 #
  933.                 #self.f3_run_outdir_label = Gtk.Label("out dir:  " + os.getcwd())
  934.                 self.f3_run_outdir_label = Gtk.Label("")
  935.                 self.f3_run_outdir_label.set_markup("<b>out dir:  </b>" + os.getcwd())
  936.                 self.f3_run['grid'].attach(self.f3_run_outdir_label, 0, 1, 2, 1)
  937.                 #
  938.                 # filechooserbutton
  939.                 self.out_dir_chooserbut = Gtk.FileChooserButton(title="Dir out")
  940.                 self.out_dir_chooserbut.set_action(2)
  941.                 #self.filechooserbutton.set_action(2) # 0 open file, 1 save file, 2 select folder, 3 create folder
  942.                 acturi = "file://" + os.getcwd()
  943.                 self.out_dir_chooserbut.set_uri(acturi)
  944.                 self.f3_run['grid'].attach(self.out_dir_chooserbut, 2, 1, 1, 1)
  945.                 self.out_dir_chooserbut.connect("selection-changed", self.dir_changed)
  946.                 #
  947.                 self.reportdata = Gtk.Label('')
  948.                 self.f3_run['grid'].attach(self.reportdata, 0, 3, 4, 1)
  949.                
  950.                
  951.         # event handlers -----------------------------------------------
  952.        
  953.         def on_window_destroy(self, *args):
  954.                 Gtk.main_quit(*args)
  955.        
  956.         def on_scale_clicked(self, button):
  957.                 if (self.box1['cbox'].get_active() != 0):
  958.                         self.calcola_scale()
  959.                 else:
  960.                         Vars.scalestr = ""
  961.                         self.outfilter()
  962.                  
  963.         def on_deint_clicked(self, button):
  964.                 Vars.deint_str = ""
  965.                 if self.checkdeint.get_active():
  966.                         deint_val = self.box_deint['cbox'].get_active()
  967.                         if (deint_val == 0):
  968.                                 Vars.deint_str = "yadif"
  969.                         elif (deint_val == 1):
  970.                                 Vars.deint_str = "pp=lb"
  971.                         elif (deint_val == 2):
  972.                                 Vars.deint_str = "idet"
  973.                 self.outfilter()
  974.                        
  975.         def on_dn_clicked(self, button):
  976.                 Vars.dn_str = ""
  977.                 if self.checkdn.get_active():
  978.                         dn_val = int( self.spindn['adj'].get_value() )
  979.                         Vars.dn_str = "hqdn3d=" + str(dn_val)
  980.                 self.outfilter()
  981.                
  982.         def on_uns_clicked(self, button):
  983.                 Vars.uns_str = ""
  984.                 if self.checkuns.get_active():
  985.                         luma = self.spinunsl['adj'].get_value()
  986.                         chroma = self.spinunsc['adj'].get_value()
  987.                         Vars.uns_str = "unsharp=5:5:" + "{0:0.2f}".format(luma) + ":5:5:" + "{0:0.2f}".format(chroma)
  988.                 self.outfilter()
  989.                        
  990.         def on_fr_clicked(self, button):
  991.                 Vars.fr_str = ""
  992.                 if self.checkfr.get_active():
  993.                         fr_val = self.spinfr['adj'].get_value()
  994.                         Vars.fr_str = "-r " + "{0:0.2f}".format(fr_val)
  995.                 self.outfilter()
  996.                    
  997.         def on_crop_clicked(self, button):
  998.                 ok = 0
  999.                 Vars.crop_str = ""
  1000.                 if self.checkcr.get_active():
  1001.                         ok = 1
  1002.                 if ok:
  1003.                         inW = int(self.spincw['adj'].get_value())
  1004.                         inH = int(self.spinch['adj'].get_value())
  1005.                         crT = int(self.spinct['adj'].get_value())
  1006.                         crB = int(self.spincb['adj'].get_value())
  1007.                         crL = int(self.spincl['adj'].get_value())
  1008.                         crR = int(self.spincr['adj'].get_value())      
  1009.                         finW = inW - crL - crR
  1010.                         finH = inH - crT - crB
  1011.                         if ( (finW < 100) or (finH < 100) ):
  1012.                                 Vars.crop_str = ""
  1013.                         else:
  1014.                                 Vars.crop_str = "crop=" + str(finW) + ":" \
  1015.                                  + str(finH) + ":" \
  1016.                                  + str(crL)  + ":" \
  1017.                                  + str(crT)
  1018.                 if (self.box2['cbox'].get_active() != 2):
  1019.                         self.outfilter()
  1020.                 else:
  1021.                         self.on_scale_clicked(button)
  1022.                
  1023.         def file_changed(self, filechooserbutton):
  1024.                 if Vars.ENCODING:
  1025.                         return
  1026.                 if self.filechooserbutton.get_filename():
  1027.                         self.butplay.set_sensitive(True) # low part ---------
  1028.                         self.butprobe.set_sensitive(True)
  1029.                         self.preview_logic() # test and testsplit
  1030.                         self.butcrode.set_sensitive(True) # crop ----------
  1031.                         self.spinct['adj'].set_value(0)
  1032.                         self.spincb['adj'].set_value(0)
  1033.                         self.spincl['adj'].set_value(0)
  1034.                         self.spincr['adj'].set_value(0)                
  1035.                         self.but_volume_det.set_sensitive(True) # volume detect
  1036.                         Vars.VOLDT_DONE = False
  1037.                         self.but_volume_det.set_label("Volume detect")
  1038.                         self.label_meanvol.set_label("")
  1039.                         self.label_maxvol.set_label("")
  1040.                         self.spin_db["adj"].set_value(0)
  1041.                         self.but_black_det.set_sensitive(True) # black  detect
  1042.                         self.but_black_det.set_label("Black detect")
  1043.                         self.textbuffer.set_text(Tips.black_dt_init_str)
  1044.                         self.progressbar_black_det.set_fraction(0.0)
  1045.                         self.but_f3_run.set_sensitive(True) # ffmpeg run
  1046.                         Vars.BLACKDT_DONE = False
  1047.                         #
  1048.                         command = "clear" + "\n"
  1049.                         length = len(command)
  1050.                         self.terminal.feed_child(command, length)
  1051.                         # need ffprobe
  1052.                         Vars.filename = self.filechooserbutton.get_filename()
  1053.                         Vars.dirname = os.path.dirname(Vars.filename)
  1054.                         Vars.basename = os.path.basename(Vars.filename)
  1055.                         self.check_out_file()
  1056.                         self.f3_run_indir_label.set_label(Vars.dirname)
  1057.                         file_4_label = (Vars.basename)
  1058.                         if ( len(file_4_label) > 50):
  1059.                                 a = file_4_label[:46]
  1060.                                 b = '  ...  '
  1061.                                 c = file_4_label[-4:]
  1062.                                 file_4_label = a + b + c
  1063.                         self.f3_run_file.set_label(file_4_label)
  1064.                         self.but_f3_run.set_tooltip_text("")
  1065.                         self.reportdata.set_text("")
  1066.                         command = [Vars.ffprobe_ex, '-show_format', '-show_streams', '-pretty', '-loglevel', 'quiet', Vars.filename]
  1067.                         p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  1068.                         out, err =  p.communicate()
  1069.                         out = out.decode() # python3
  1070.                         self.get_info(out)
  1071.                        
  1072.         def get_info(self, out):
  1073.                 def form_to_3(in_str):
  1074.                         '''
  1075.                        bitrate and size info formatting
  1076.                        '''
  1077.                         a = in_str.split(' ')[0]
  1078.                         if len(in_str.split(' ')) == 2:
  1079.                                 b = in_str.split(' ')[1]
  1080.                                 a = float(a)
  1081.                                 result = "{0:.3f}".format(a ) + " " + b
  1082.                         else:
  1083.                                 result = 'N/A'
  1084.                         return result
  1085.                
  1086.                 def set_w_h_video_scale():
  1087.                         self.spincw['adj'].set_value(float(Vars.video_width)) #mettere decimali se no non si vede
  1088.                         self.spincw['spinner'].set_sensitive(False)
  1089.                         self.spinch['adj'].set_value(float(Vars.video_height)) #mettere decimali se no non si vede
  1090.                         self.spinch['spinner'].set_sensitive(False)
  1091.                         if Vars.add_1_1_dar:
  1092.                                 self.box2['list'].append(["as input"])
  1093.                         Vars.add_1_1_dar = False                  
  1094.                                
  1095.                 #x Vars.info_str
  1096.                 Vars.info_str = ""
  1097.                 is_stream = 0
  1098.                 is_format = 0
  1099.                 for line in out.split('\n'):
  1100.                         line = line.strip()
  1101.                         if line.startswith('[STREAM]'):
  1102.                                 Vars.info_str = Vars.info_str + "STREAM "
  1103.                                 is_stream = 1
  1104.                         elif line.startswith('[/STREAM]'):
  1105.                                 Vars.info_str = Vars.info_str + "\n"
  1106.                                 is_stream = 0
  1107.                         elif (line.startswith('index=') and (is_stream == 1)):
  1108.                                 s = line
  1109.                                 info = s.split('=')[1]  
  1110.                                 Vars.info_str = Vars.info_str + info + ": "
  1111.                         elif (line.startswith('codec_name=') and (is_stream == 1)):
  1112.                                 s = line
  1113.                                 info = s.split('=')[1]  
  1114.                                 Vars.info_str = Vars.info_str + info + ", "
  1115.                         elif (line.startswith('profile=') and (is_stream == 1)):
  1116.                                 s = line
  1117.                                 info = s.split('=')[1]  
  1118.                                 Vars.info_str = Vars.info_str + "profile=" +info + ", "
  1119.                         elif (line.startswith('codec_type=') and (is_stream == 1)):
  1120.                                 s = line
  1121.                                 info = s.split('=')[1]  
  1122.                                 Vars.info_str = Vars.info_str + info + ", "
  1123.                         elif (line.startswith('width=') and (is_stream == 1)):
  1124.                                 s = line
  1125.                                 info = s.split('=')[1]
  1126.                                 Vars.video_width = info
  1127.                                 Vars.info_str = Vars.info_str + info + "x"
  1128.                         elif (line.startswith('height=') and (is_stream == 1)):
  1129.                                 s = line
  1130.                                 info = s.split('=')[1]
  1131.                                 Vars.video_height = info
  1132.                                 Vars.info_str = Vars.info_str + info + ", "
  1133.                         elif (line.startswith('sample_aspect_ratio=') and (is_stream == 1)):
  1134.                                 s = line
  1135.                                 info = s.split('=')[1]  
  1136.                                 Vars.info_str = Vars.info_str + "SAR=" + info + ", "
  1137.                         elif (line.startswith('display_aspect_ratio=') and (is_stream == 1)):
  1138.                                 s = line
  1139.                                 info = s.split('=')[1]  
  1140.                                 Vars.info_str = Vars.info_str + "DAR=" + info + ", "
  1141.                         elif (line.startswith('pix_fmt=') and (is_stream == 1)):
  1142.                                 s = line
  1143.                                 info = s.split('=')[1]  
  1144.                                 Vars.info_str = Vars.info_str + info + ", "
  1145.                         elif (line.startswith('sample_rate=') and (is_stream == 1)):
  1146.                                 s = line
  1147.                                 info = s.split('=')[1]
  1148.                                 info = form_to_3(info)
  1149.                                 Vars.info_str = Vars.info_str + "samplerate=" + info + ", "
  1150.                         elif (line.startswith('channels=') and (is_stream == 1)):
  1151.                                 s = line
  1152.                                 info = s.split('=')[1]  
  1153.                                 Vars.info_str = Vars.info_str + "channels=" + info + ", "
  1154.                         elif (line.startswith('r_frame_rate=') and (is_stream == 1)):
  1155.                                 s = line
  1156.                                 info = s.split('=')[1]
  1157.                                 a = info.split('/')[0]
  1158.                                 b = info.split('/')[1]
  1159.                                 a = float(a)
  1160.                                 b = float(b)
  1161.                                 if ((a > 0) and (b > 0)):
  1162.                                         c = float(a)/float(b)
  1163.                                         info = "{0:0.2f}".format(c)
  1164.                                         self.spinfr["adj"].set_value(c)
  1165.                                         Vars.info_str = Vars.info_str + info + " fps, "
  1166.                         elif (line.startswith('bit_rate=') and (is_stream == 1)):
  1167.                                 s = line
  1168.                                 info = s.split('=')[1]
  1169.                                 info = form_to_3(info)
  1170.                                 Vars.info_str = Vars.info_str + "bitrate=" + info + ", "
  1171.                         elif (line.startswith('TAG:language=') and (is_stream == 1)):
  1172.                                 s = line
  1173.                                 info = s.split('=')[1]  
  1174.                                 Vars.info_str = Vars.info_str + "lang=" + info + ", "
  1175.                         elif line.startswith('[FORMAT]'):
  1176.                                 Vars.info_str = Vars.info_str + "FORMAT "
  1177.                                 is_format = 1
  1178.                         elif line.startswith('[/FORMAT]'):
  1179.                                 #Vars.info_str = Vars.info_str + "/n"
  1180.                                 is_format = 0
  1181.                         elif (line.startswith('nb_streams=') and (is_format == 1)):
  1182.                                 s = line
  1183.                                 info = s.split('=')[1]  
  1184.                                 Vars.info_str = Vars.info_str + "streams=" + info + ", "
  1185.                         elif (line.startswith('duration=') and (is_format == 1)):
  1186.                                 s = line
  1187.                                 info = s.split('=')[1]  
  1188.                                 info = info.split('.')[0]
  1189.                                 self.entry_timein.set_text('0')
  1190.                                 self.entry_timeout.set_text(info)
  1191.                                 Vars.duration = info
  1192.                                 Vars.duration_in_sec = self.hms2sec(info)
  1193.                                 Vars.info_str = Vars.info_str + "duration=" + info + ", "
  1194.                         elif (line.startswith('size=') and (is_format == 1)):
  1195.                                 s = line
  1196.                                 info = s.split('=')[1]
  1197.                                 info = form_to_3(info)
  1198.                                 Vars.info_str = Vars.info_str + "size=" + info + ", "
  1199.                         elif (line.startswith('bit_rate=') and (is_format == 1)):
  1200.                                 s = line
  1201.                                 info = s.split('=')[1]
  1202.                                 info = form_to_3(info)
  1203.                                 Vars.info_str = Vars.info_str + "bitrate=" + info + ", "
  1204.                 set_w_h_video_scale()
  1205.                 self.f3_dur_file.set_label(Vars.duration)
  1206.                 self.textbuffer_info.set_text(Vars.info_str)
  1207.                 self.f3_run_file.set_tooltip_text(Vars.info_str)
  1208.                 self.check_map.set_tooltip_text(Vars.info_str)
  1209.                 self.check_time.set_tooltip_text(Vars.info_str)
  1210.                 Vars.in_file_size = self.humansize(Vars.filename)
  1211.        
  1212.         def on_butplay_clicked(self, button):
  1213.                 if Vars.ENCODING:
  1214.                         return
  1215.                 command = Vars.ffplay_ex + " \"" + Vars.filename + "\"" +"\n"
  1216.                 length = len(command)
  1217.                 self.terminal.feed_child(command, length)
  1218.                
  1219.         def on_butprobe_clicked(self, button):
  1220.                 if Vars.ENCODING:
  1221.                         return
  1222.                 command = Vars.ffprobe_ex + " \"" + Vars.filename + "\""  + " 2>&1 | grep 'Input\|Duration\|Stream'" + "\n"
  1223.                 length = len(command)
  1224.                 self.terminal.feed_child(command, length)        
  1225.                
  1226.         def on_butcrode_clicked(self, button):
  1227.                 # need ffmpeg
  1228.                 self.butcrode.set_sensitive(False)
  1229.                 if (Vars.time_start == 0):
  1230.                         command = Vars.ffmpeg_ex + " -i " + "\"" + Vars.filename + "\"" + " -ss 60 -t 1 -vf cropdetect -f null - 2>&1 | awk \'/crop/ { print $NF }\' | tail -1"
  1231.                 else:
  1232.                         command = Vars.ffmpeg_ex + " -i " + "\"" + Vars.filename + "\"" + " -ss " + str(Vars.time_start) + " -t 1 -vf cropdetect -f null - 2>&1 | awk \'/crop/ { print $NF }\' | tail -1"
  1233.                 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  1234.                 out, err =  p.communicate()
  1235.                 out = out.decode()
  1236.                 for line in out.split('\n'):
  1237.                         line = line.strip()
  1238.                         if "crop=" in line:  
  1239.                                 aa = line.split('crop=', 1)
  1240.                                 aaa = aa[1]
  1241.                                 listdata = aaa.split(':')
  1242.                                 w = listdata[0]
  1243.                                 h = listdata[1]
  1244.                                 offx = listdata[2]
  1245.                                 offy = listdata[3]
  1246.                                 ctop = int(offy)
  1247.                                 cbot = int(Vars.video_height) - int(offy) -int(h)
  1248.                                 cleft = int(offx)
  1249.                                 cright = int(Vars.video_width) - int(offx) -int(w)
  1250.                                 self.spinct['adj'].set_value(float(ctop))
  1251.                                 self.spincb['adj'].set_value(float(cbot))
  1252.                                 self.spincl['adj'].set_value(float(cleft))
  1253.                                 self.spincr['adj'].set_value(float(cright))  
  1254.                                 break
  1255.                 self.butcrode.set_sensitive(True)
  1256.                
  1257.         def on_buttest_clicked(self, button):
  1258.                 if Vars.ENCODING:
  1259.                         return
  1260.                 # -map preview: only with none -vf option  
  1261.                 if (Vars.maps_str == ""):
  1262.                         command = Vars.ffplay_ex + " \"" + Vars.filename + "\" " \
  1263.                          + " " + Vars.time_final_str + " " \
  1264.                          + "-vf "  + Vars.filter_test_str + "\n"
  1265.                 else:
  1266.                         command = Vars.ffmpeg_ex + " -i \"" + Vars.filename + "\" " \
  1267.                          + Vars.maps_str  + " " + Vars.time_final_str + " "
  1268.                         if (Vars.filter_test_str != ""):
  1269.                                 command = command + " -vf "  + Vars.filter_test_str
  1270.                         command = command +  " -c copy -f matroska - | ffplay -" + "\n"
  1271.                 length = len(command)
  1272.                 self.terminal.feed_child(command, length)
  1273.                
  1274.         def on_buttestspl_clicked(self, button):
  1275.                 if Vars.ENCODING:
  1276.                         return
  1277.                 command = Vars.ffplay_ex + " \"" + Vars.filename + "\" " \
  1278.                   + " " + Vars.time_final_str + " " \
  1279.                   + " -vf \"split[a][b]; [a]pad=iw:(ih*2)+4:color=888888 [src]; [b]" \
  1280.                   + Vars.filter_test_str + " [filt]; [src][filt]overlay=((main_w - w)/2):main_h/2\"" + "\n"
  1281.                 length = len(command)
  1282.                 self.terminal.feed_child(command, length)
  1283.        
  1284.         def on_but_volume_det_clicked(self, button):
  1285.                        
  1286.                 def finish_voldet(meanv, maxv):
  1287.                         self.label_meanvol.set_label(meanv)
  1288.                         self.label_maxvol.set_label(maxv)
  1289.                         self.but_volume_det.set_label("volume detected")
  1290.                         self.voldet_spin.stop()
  1291.                         self.voldet_spin.set_sensitive(False)
  1292.                
  1293.                 def my_thread():        
  1294.                         command = [Vars.ffmpeg_ex, '-i', Vars.filename, '-vn', '-af', 'volumedetect', '-f', 'null', '/dev/null']
  1295.                         p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
  1296.                         while True:
  1297.                                 line = p.stderr.read()
  1298.                                 if not line:
  1299.                                         break
  1300.                                 out = line.decode()
  1301.                                 for line in out.split('\n'):
  1302.                                         line = line.strip()
  1303.                                         if "mean_volume:" in line:  
  1304.                                                 aa = line.split('mean_volume:', 1)
  1305.                                                 mea_vol_det = aa[1]
  1306.                                                 meanv = "Mean vol: " + mea_vol_det
  1307.                                         if "max_volume:" in line:  
  1308.                                                 aa = line.split('max_volume:', 1)
  1309.                                                 max_vol_det = aa[1]
  1310.                                                 maxv = "Max vol: " + max_vol_det
  1311.                         p.communicate()
  1312.                         finish_voldet(meanv, maxv)
  1313.                
  1314.                 if Vars.VOLDT_DONE:
  1315.                         return
  1316.                 Vars.VOLDT_DONE = True
  1317.                 self.voldet_spin.set_sensitive(True)
  1318.                 self.voldet_spin.start()        
  1319.                 self.but_volume_det.set_label("Wait")  
  1320.                 threading.Thread(target=my_thread).start()
  1321.        
  1322.         def on_volume_clicked(self, button):
  1323.                 ratio = 1
  1324.                 self.label_ratio_val.set_label("")
  1325.                 Vars.final_af_str = ""
  1326.                 db = self.spin_db["adj"].get_value()
  1327.                 if (db != 0.00):
  1328.                         ratio = pow(10, (db/20.0))
  1329.                         ratio = float("{0:.3f}".format(ratio))    
  1330.                         self.label_ratio_val.set_label(str(ratio))      
  1331.                 if self.check_vol.get_active():
  1332.                         Vars.final_af_str = "-af volume=" + str(ratio)
  1333.                 else:
  1334.                         Vars.final_af_str = ""
  1335.                 self.make_p3_out_command()
  1336.        
  1337.         def on_map_clicked(self, button):
  1338.                 self.map_label.set_label("")
  1339.                 Vars.maps_str = ""
  1340.                 if self.check_map.get_active():
  1341.                         map_in = self.entry_map.get_chars(0, -1)
  1342.                         if (map_in != ""):
  1343.                                 map_l = map_in.split(",")
  1344.                                 for i in range(len(map_l)):
  1345.                                         if Vars.maps_str == "":
  1346.                                                 Vars.maps_str = Vars.maps_str + "-map " + map_l[i]
  1347.                                         else:
  1348.                                                 Vars.maps_str = Vars.maps_str + " -map " + map_l[i]
  1349.                                 self.map_label.set_label(Vars.maps_str)
  1350.                 self.outfilter()
  1351.                 self.make_p3_out_command()
  1352.                
  1353.         def on_time_clicked(self, button):
  1354.                 Vars.time_final_str = ""
  1355.                        
  1356.                 if self.check_time.get_active():
  1357.                         timein = 0
  1358.                         timeout = 0
  1359.                         timedur = 0
  1360.                         timein_ent = self.entry_timein.get_chars(0, -1)
  1361.                         if (timein_ent != ""):
  1362.                                 if timein_ent.isdigit():
  1363.                                         timein = int(timein_ent)
  1364.                                         if Vars.duration_in_sec != 0:
  1365.                                                 if timein > Vars.duration_in_sec:
  1366.                                                         self.entry_timein.set_text(str(Vars.duration_in_sec -1))
  1367.                                                         return
  1368.                                 else:  
  1369.                                         timein = self.hms2sec(timein_ent)
  1370.                                         if Vars.duration_in_sec != 0:
  1371.                                                 if timein > Vars.duration_in_sec:
  1372.                                                         self.entry_timein.set_text(str(self.sec2hms(Vars.duration_in_sec -1)))
  1373.                                                         return
  1374.                         timeout_ent = self.entry_timeout.get_chars(0, -1)
  1375.                         if (timeout_ent != ""):
  1376.                                 if timeout_ent.isdigit():
  1377.                                         timeout = int(timeout_ent)
  1378.                                         if Vars.duration_in_sec != 0:
  1379.                                                 if timeout > Vars.duration_in_sec:
  1380.                                                         self.entry_timeout.set_text(str(Vars.duration_in_sec))
  1381.                                                         return
  1382.                                         timedur = timeout - timein
  1383.                                 else:  
  1384.                                         timeout = self.hms2sec(timeout_ent)
  1385.                                         if Vars.duration_in_sec != 0:
  1386.                                                 if timeout > Vars.duration_in_sec:
  1387.                                                         self.entry_timeout.set_text(Vars.duration)
  1388.                                                         return
  1389.                                         timedur = timeout - timein
  1390.                         if (timein != 0):
  1391.                                 Vars.time_final_str = Vars.time_final_str + " -ss " + str(timein)
  1392.                                 Vars.time_start = timein
  1393.                         if (timedur > 0):
  1394.                                 Vars.time_final_str = Vars.time_final_str + " -t " + str(timedur)
  1395.                 self.time_label.set_text(Vars.time_final_str)
  1396.                 self.outfilter()
  1397.                 self.make_p3_out_command()
  1398.        
  1399.         def on_codec_audio_changed(self, button):
  1400.                 a_cod = self.box_ac['cbox'].get_active()
  1401.                 self.spin_ac['spinner'].set_sensitive(True)
  1402.                 self.box_audio_ch['cbox'].set_sensitive(True)
  1403.                 self.box_audio_rate['cbox'].set_sensitive(True)
  1404.                 if (a_cod == 0): # aaq  
  1405.                         self.adj_change(self.spin_ac['adj'], 100, 80, 160, 10, 1)
  1406.                         self.spin_ac['adj'].set_value(100.0)
  1407.                         self.spin_ac['spinner'].set_tooltip_text(Tips.faac_q_str)
  1408.                 elif (a_cod == 5): # aac_fdk cbr 64-320 def 128
  1409.                         self.adj_change(self.spin_ac['adj'], 128, 64, 320, 8, 2)
  1410.                         self.spin_ac['adj'].set_value(128.0)
  1411.                         self.spin_ac['spinner'].set_tooltip_text(Tips.aak_cbr_str)
  1412.                 elif (a_cod == 6): # lame cbr 32 - 320 def 128
  1413.                         self.adj_change(self.spin_ac['adj'], 128, 32, 320, 8, 2)
  1414.                         self.spin_ac['adj'].set_value(128.0)
  1415.                         self.spin_ac['spinner'].set_tooltip_text(Tips.lamecbr_str)
  1416.                 elif (a_cod == 7): # lame vbr 0 - 9 def 6
  1417.                         self.adj_change(self.spin_ac['adj'], 6, 0, 9, 1, 1)
  1418.                         self.spin_ac['adj'].set_value(6.0)
  1419.                         self.spin_ac['spinner'].set_tooltip_text(Tips.lamevbr_str)
  1420.                 elif (a_cod == 8): # aac cbr 32 - 320 def 128
  1421.                         self.adj_change(self.spin_ac['adj'], 128, 32, 320, 8, 2)
  1422.                         self.spin_ac['adj'].set_value(128.0)
  1423.                         self.spin_ac['spinner'].set_tooltip_text(Tips.aac_cbr_str)
  1424.                 elif (a_cod == 3 or a_cod == 4): # copy none
  1425.                         self.spin_ac['spinner'].set_sensitive(False)
  1426.                         self.spin_ac['spinner'].set_tooltip_text('')
  1427.                         self.box_audio_ch['cbox'].set_sensitive(False)
  1428.                         self.box_audio_rate['cbox'].set_sensitive(False)
  1429.                 else:
  1430.                         self.spin_ac['spinner'].set_sensitive(False)
  1431.                         self.spin_ac['spinner'].set_tooltip_text('')
  1432.                         self.box_audio_ch['cbox'].set_sensitive(False)
  1433.                         self.box_audio_ch['cbox'].set_active(0)
  1434.                 self.on_codec_clicked(button)
  1435.  
  1436.         def on_codec_video_changed(self, button):
  1437.                 self.on_codec_clicked(button)
  1438.  
  1439.         def on_codec_vrc_changed(self, button):
  1440.                 video_rc = self.box_vrc['cbox'].get_active() #0 crf, 1 kb/s
  1441.                 video_codec = self.box_vcodec['cbox'].get_active() #0 x264, 1 -vcopy
  1442.                 model = self.box_vcodec['cbox'].get_model()
  1443.                 item = model[video_codec]
  1444.                 video_codec = item[0]
  1445.                 if (video_rc == 0):
  1446.                         self.adj_change(self.spin_vc['adj'], 21.3, 15, 40, 0.1, 1)
  1447.                         self.spin_vc['adj'].set_value(21.3)
  1448.                         self.check_2pass.set_active(False)
  1449.                         self.check_2pass.set_sensitive(False)
  1450.                 elif (video_rc == 1) and (video_codec == "mpeg4" ):
  1451.                         self.adj_change(self.spin_vc['adj'], 1200, 300, 5000, 100, 100)
  1452.                         self.spin_vc['adj'].set_value(1200.0)
  1453.                         self.check_2pass.set_sensitive(True)
  1454.                 elif (video_rc == 1):
  1455.                         self.adj_change(self.spin_vc['adj'], 730, 300, 4000, 10, 100)
  1456.                         self.spin_vc['adj'].set_value(730.0)
  1457.                         self.check_2pass.set_sensitive(True)    
  1458.  
  1459.         def on_codec_clicked(self, button):
  1460.                 Vars.final_codec_str = ""
  1461.                 #audio
  1462.                 Vars.audio_codec_str = self.audio_codec()
  1463.                 #self.f3ac_label_out.set_label(Vars.audio_codec_str)
  1464.                 # video
  1465.                 Vars.v_copy = 0
  1466.                 Vars.vcodec_is = ""
  1467.                 video_codec = self.box_vcodec['cbox'].get_active() #0 x264, 1 -vcopy
  1468.                 model = self.box_vcodec['cbox'].get_model()
  1469.                 item = model[video_codec]
  1470.                 video_codec = item[0]
  1471.                 if (video_codec == "video copy"):
  1472.                         Vars.v_copy = 1
  1473.                         Vars.video_codec_str_more = self.v_codec_vcopy()
  1474.                 elif (video_codec == "mpeg4"): #mpeg4
  1475.                         Vars.video_codec_str_more = self.v_codec_mpeg4()
  1476.                         Vars.vcodec_is = "mpeg4"
  1477.                 elif (video_codec == "libx264"):
  1478.                         #libx264
  1479.                         Vars.video_codec_str_more = self.v_codec_x264()
  1480.                 #
  1481.                 self.on_codec_vrc_value_change(button)
  1482.                        
  1483.         def on_codec_vrc_value_change(self, button):
  1484.                 Vars.video_codec_str = ""
  1485.                 if (Vars.vcodec_is == "") and (Vars.v_copy == 0): #x264
  1486.                         #rc
  1487.                         video_rc = self.box_vrc['cbox'].get_active() #0 crf, 1 kb/s
  1488.                         if (video_rc == 0):    
  1489.                                 video_codec_str = "-c:v libx264 " + "-crf " + str(self.spin_vc['adj'].get_value()) + " " + Vars.video_codec_str_more
  1490.                         elif (video_rc == 1):
  1491.                                 video_codec_str = "-c:v libx264 " + "-b:v " + str(int(self.spin_vc['adj'].get_value())) + "k " + Vars.video_codec_str_more
  1492.                 elif (Vars.vcodec_is == "mpeg4"):
  1493.                         video_codec_str = "-c:v mpeg4 -mpv_flags strict_gop " + "-b:v " + str(int(self.spin_vc['adj'].get_value())) + "k"
  1494.                 else:
  1495.                         video_codec_str = Vars.video_codec_str_more
  1496.                        
  1497.                 Vars.video_codec_str = video_codec_str
  1498.                        
  1499.                 #self.f3v_label_out.set_label(video_codec_str)
  1500.                 Vars.final_codec_str = Vars.audio_codec_str + " " + video_codec_str
  1501.                 self.make_p3_out_command()
  1502.                
  1503.         def audio_codec(self):
  1504.                 Vars.a_copy = 0
  1505.                 Vars.audionone = 0
  1506.                 # channels
  1507.                 audio_codec_str = ""
  1508.                 channel_str = "-ac 2"
  1509.                 channel = self.box_audio_ch['cbox'].get_active()
  1510.                 if channel == 1:
  1511.                         channel_str = "-ac 1"
  1512.                 # sample rate
  1513.                 sample_str = "-ar 48k"
  1514.                 sample = self.box_audio_rate['cbox'].get_active()
  1515.                 if sample == 1:
  1516.                         sample_str = "-ar 44.1k"
  1517.                 #
  1518.                 audio_codec = self.box_ac['cbox'].get_active()
  1519.                 if self.box_oth_container['cbox'].get_active() == 2: # avi protection
  1520.                         if (audio_codec != 3) and (audio_codec != 4) and (audio_codec != 6) and (audio_codec != 7):
  1521.                                 self.box_ac['cbox'].set_active(6)    # only lame or acopy or noaudio
  1522.                                 audio_codec = 6
  1523.                 if (audio_codec == 0):
  1524.                         audio_codec_str = "-c:a libfaac -q:a "
  1525.                         quality_faac = self.spin_ac['adj'].get_value()
  1526.                         audio_codec_str = audio_codec_str + str(int(quality_faac)) + " " + sample_str + " " + channel_str
  1527.                 elif (audio_codec == 1):
  1528.                         audio_codec_str = "-c:a libaacplus -b:a 64k" + " " + sample_str + " " + channel_str
  1529.                 elif (audio_codec == 2):
  1530.                         audio_codec_str = "-c:a libaacplus -b:a 32k" + " " + sample_str + " " + channel_str
  1531.                 elif (audio_codec == 3):
  1532.                         audio_codec_str = "-an"
  1533.                         Vars.audionone = 1
  1534.                 elif (audio_codec == 4):
  1535.                         audio_codec_str = "-c:a copy"
  1536.                         Vars.a_copy = 1
  1537.                 elif (audio_codec == 5):
  1538.                         audio_codec_str = "-c:a libfdk_aac -b:a "
  1539.                         aakcbr = self.spin_ac['adj'].get_value()
  1540.                         audio_codec_str = audio_codec_str + str(int(aakcbr)) + "k " + sample_str + " " + channel_str
  1541.                 elif (audio_codec == 6):
  1542.                         audio_codec_str = "-c:a libmp3lame -b:a "
  1543.                         lamecbr = self.spin_ac['adj'].get_value()
  1544.                         audio_codec_str = audio_codec_str + str(int(lamecbr)) + "k " + sample_str + " " + channel_str
  1545.                 elif (audio_codec == 7):
  1546.                         audio_codec_str = "-c:a libmp3lame -q:a "  
  1547.                         lamevbr = self.spin_ac['adj'].get_value()
  1548.                         audio_codec_str = audio_codec_str + str(int(lamevbr)) + " " + sample_str + " " + channel_str
  1549.                 elif (audio_codec == 8):
  1550.                         audio_codec_str = "-c:a aac -strict -2 -b:a "
  1551.                         aaccbr = self.spin_ac['adj'].get_value()
  1552.                         audio_codec_str = audio_codec_str + str(int(aaccbr)) + "k " + sample_str + " " + channel_str
  1553.                 return audio_codec_str
  1554.                
  1555.         def v_codec_vcopy(self):        
  1556.                 self.box_vrc['cbox'].set_active(0)
  1557.                 self.box_vrc['cbox'].set_sensitive(False)
  1558.                 self.spin_vc['spinner'].set_sensitive(False)
  1559.                 self.box_3v_preset['cbox'].set_active(2)
  1560.                 self.box_3v_preset['cbox'].set_sensitive(False)
  1561.                 self.box_3v_tune['cbox'].set_active(0)
  1562.                 self.box_3v_tune['cbox'].set_sensitive(False)
  1563.                 self.box_3v_x264opts['cbox'].set_active(0)
  1564.                 self.box_3v_x264opts['cbox'].set_sensitive(False)
  1565.                 self.check_3v_opencl.set_active(False)
  1566.                 self.check_3v_opencl.set_sensitive(False)
  1567.                 self.check_cpu.set_active(False)
  1568.                 self.check_cpu.set_sensitive(False)
  1569.                 video_codec_str = "-c:v copy"
  1570.                 return video_codec_str
  1571.  
  1572.         def v_codec_x264(self):
  1573.                 video_codec_str = ""
  1574.                 if "mpeg4" in Vars.p3_command:
  1575.                         self.box_vrc['cbox'].set_active(0)
  1576.                         self.box_3v_tune['cbox'].set_active(0)
  1577.                 self.box_vrc['cbox'].set_sensitive(True)
  1578.                 self.spin_vc['spinner'].set_sensitive(True)
  1579.                 self.box_3v_preset['cbox'].set_sensitive(True)
  1580.                 self.box_3v_tune['cbox'].set_sensitive(True)
  1581.                 self.box_3v_x264opts['cbox'].set_sensitive(True)
  1582.                 self.check_3v_opencl.set_sensitive(True)
  1583.                 self.check_cpu.set_sensitive(True)
  1584.                 #preset
  1585.                 preset_str = ""
  1586.                 preset = self.box_3v_preset['cbox'].get_active()
  1587.                 # 0 -superfast, 1 -veryfast, 2 -faster, 3 -fast, 4 -medium, 5 -slow, default: 2 faster
  1588.                 if (preset == 0):
  1589.                         preset_str = "-preset superfast "
  1590.                 if (preset == 1):
  1591.                         preset_str = "-preset veryfast "
  1592.                 if (preset == 2):
  1593.                         preset_str = "-preset faster "
  1594.                 if (preset == 3):
  1595.                         preset_str = "-preset fast "
  1596.                 if (preset == 4):
  1597.                         preset_str = "-preset medium "
  1598.                 if (preset == 5):
  1599.                         preset_str = "-preset slow "
  1600.                 # tune  film, animation, grain, stillimage
  1601.                 tune = self.box_3v_tune['cbox'].get_active()
  1602.                 if (tune == 0):
  1603.                         preset_str = preset_str + "-tune film"
  1604.                 if (tune == 1):
  1605.                         preset_str = preset_str + "-tune animation"
  1606.                 if (tune == 2):
  1607.                         preset_str = preset_str + "-tune grain"
  1608.                 if (tune == 3):
  1609.                         preset_str = preset_str + "-tune stillimage"
  1610.                 video_codec_str = preset_str
  1611.                 #x264opts      
  1612.                 if (self.box_3v_x264opts['cbox'].get_active() == 1):
  1613.                         video_codec_str = video_codec_str + " -x264opts ssim"
  1614.                 else:
  1615.                         video_codec_str = video_codec_str + " -x264opts ref=4:bframes=4:direct=auto:aq-strength=1.3:ssim"
  1616.                 #opencl
  1617.                 if self.check_3v_opencl.get_active():
  1618.                         video_codec_str = video_codec_str + ":opencl"
  1619.                 return video_codec_str
  1620.  
  1621.         def v_codec_mpeg4(self):
  1622.                 if not ("mpeg4" in Vars.p3_command):
  1623.                         self.box_vrc['cbox'].set_active(1)
  1624.                         self.on_codec_vrc_changed(1)
  1625.                 self.box_vrc['cbox'].set_sensitive(False)
  1626.                 self.spin_vc['spinner'].set_sensitive(True)
  1627.                 self.box_3v_preset['cbox'].set_sensitive(False)
  1628.                 self.box_3v_tune['cbox'].set_sensitive(False)
  1629.                 self.box_3v_x264opts['cbox'].set_sensitive(False)
  1630.                 self.check_3v_opencl.set_active(False)
  1631.                 self.check_3v_opencl.set_sensitive(False)
  1632.                 self.check_cpu.set_active(False)
  1633.                 self.check_cpu.set_sensitive(False)
  1634.                 video_codec_str = ""
  1635.                 return video_codec_str
  1636.  
  1637.         def on_preset_changed(self, button):
  1638.                 self.on_codec_clicked(button)
  1639.                
  1640.         def on_f3_other_clicked(self, button):
  1641.                 Vars.final_other_str = ""
  1642.                 Vars.debug = 0
  1643.                 Vars.first_pass = 0
  1644.                 Vars.n_pass = 1
  1645.                 if self.check_nometa.get_active():
  1646.                         Vars.final_other_str = "-map_metadata -1"
  1647.                 if self.check_2pass.get_active():
  1648.                                 Vars.first_pass = 1
  1649.                                 Vars.n_pass = 2
  1650.                 if self.check_cpu.get_active():
  1651.                         if (Vars.final_other_str == ""):
  1652.                                 Vars.final_other_str = "-threads 3"
  1653.                         else:
  1654.                                 Vars.final_other_str = Vars.final_other_str + " " + "-threads 3"
  1655.                 if self.check_scopy.get_active():
  1656.                         if (Vars.final_other_str == ""):
  1657.                                 Vars.final_other_str = "-c:s copy"
  1658.                         else:
  1659.                                 Vars.final_other_str = Vars.final_other_str + " " + "-c:s copy"
  1660.                 if self.check_debug.get_active():
  1661.                                 Vars.debug = 1
  1662.                 self.make_p3_out_command()
  1663.                
  1664.         def on_container_changed(self,button):
  1665.                 container = self.box_oth_container['cbox'].get_active() #0 mkv, 1 mp4
  1666.                 if (container == 1):
  1667.                         Vars.container_str = "-f mp4"
  1668.                         self.check_scopy.set_active(False)
  1669.                         self.check_scopy.set_sensitive(False)
  1670.                         Vars.ext = ".mp4"
  1671.                         self.check_out_file()
  1672.                         if Vars.is_avi:
  1673.                                 it = self.box_vcodec['list'].insert(0)
  1674.                                 self.box_vcodec['list'].set(it, {0: "libx264"})
  1675.                                 self.box_vcodec['cbox'].set_active(0)
  1676.                                 Vars.is_avi = 0
  1677.                 elif (container == 2):
  1678.                         Vars.container_str = "-f avi"
  1679.                         self.check_scopy.set_active(False)
  1680.                         self.check_scopy.set_sensitive(False)
  1681.                         audio_codec = self.box_ac['cbox'].get_active()
  1682.                         if (audio_codec != 3) and (audio_codec != 4) and (audio_codec != 6) and (audio_codec != 7):
  1683.                                 self.box_ac['cbox'].set_active(6) # lame cbr
  1684.                         model = self.box_vcodec['cbox'].get_model()
  1685.                         it = Gtk.TreeModel.get_iter(model,0)
  1686.                         self.box_vcodec['list'].remove(it)
  1687.                         self.box_vcodec['cbox'].set_active(1) # mpeg4
  1688.                         Vars.is_avi = 1
  1689.                         Vars.ext = ".avi"
  1690.                         self.check_out_file()
  1691.                 else:
  1692.                         Vars.container_str = "-f matroska"
  1693.                         self.check_scopy.set_sensitive(True)
  1694.                         Vars.ext = ".mkv"
  1695.                         self.check_out_file()
  1696.                         if Vars.is_avi:
  1697.                                 it = self.box_vcodec['list'].insert(0)
  1698.                                 self.box_vcodec['list'].set(it, {0: "libx264"})
  1699.                                 self.box_vcodec['cbox'].set_active(0)
  1700.                                 Vars.is_avi = 0
  1701.                 self.make_p3_out_command()
  1702.                
  1703.         def on_but_f3_run_clicked(self, button):
  1704.        
  1705.                 def restore_dir_button(): # if is changed during encoding
  1706.                         select = self.out_dir_chooserbut.get_uri()
  1707.                         decoded = url2pathname(select)
  1708.                         newdir = decoded.split('//')[1]
  1709.                         if (newdir != Vars.outdir):
  1710.                                 self.out_dir_chooserbut.disconnect_by_func(self.dir_changed)
  1711.                                 Vars.first_run = 0
  1712.                                 acturi = "file://" + os.getcwd()
  1713.                                 self.out_dir_chooserbut.set_uri(acturi)
  1714.                                 self.out_dir_chooserbut.connect("selection-changed", self.dir_changed)
  1715.                                
  1716.                 def restore_file_button():  # if is changed during encoding
  1717.                         select = self.filechooserbutton.get_filename()
  1718.                         if (select != Vars.filename):
  1719.                                 self.filechooserbutton.disconnect_by_func(self.file_changed)
  1720.                                 self.filechooserbutton.set_filename(Vars.filename)
  1721.                                 self.filechooserbutton.connect("file-set", self.file_changed)
  1722.                
  1723.                 def end_run():
  1724.                         if (Vars.eff_pass == 0) and (Vars.final_command1 == ""):
  1725.                                 Vars.out_file_size = self.humansize(Vars.newname)
  1726.                                 stri = " Done in: " + self.sec2hms(Vars.time_done) + " " + "\n"\
  1727.                                         + " in:  " + Vars.in_file_size + "\n"\
  1728.                                         + " out: " + Vars.out_file_size
  1729.                                 self.but_f3_run.set_tooltip_text(stri)
  1730.                                 stri2 = " Done in: " + self.sec2hms(Vars.time_done) + " "\
  1731.                                         + " input size:  " + Vars.in_file_size + " "\
  1732.                                         + " output size: " + Vars.out_file_size
  1733.                                 self.reportdata.set_text(stri2)
  1734.                                 if os.path.exists('x264_lookahead.clbin'):
  1735.                                         os.remove('x264_lookahead.clbin')
  1736.                                 self.but_f3_run.set_label('RUN')
  1737.                                 Vars.ENCODING = False
  1738.                                 restore_dir_button()
  1739.                                 restore_file_button()
  1740.                         elif (Vars.eff_pass == 0) and (Vars.final_command1 != ""):
  1741.                                 Vars.eff_pass = 1
  1742.                                 Vars.ENCODING = False
  1743.                                 self.on_but_f3_run_clicked(1)
  1744.                         elif (Vars.eff_pass == 1):
  1745.                                 Vars.out_file_size = self.humansize(Vars.newname)
  1746.                                 stri = " Done in: " + self.sec2hms(Vars.time_done + Vars.time_done_1) + " " + "\n"\
  1747.                                         + " pass 1: " + self.sec2hms(Vars.time_done) + " " + "\n"\
  1748.                                         + " pass 2: " + self.sec2hms(Vars.time_done_1) + " " + "\n"\
  1749.                                         + " in:  " + Vars.in_file_size + "\n"\
  1750.                                         + " out: " + Vars.out_file_size
  1751.                                 self.but_f3_run.set_tooltip_text(stri)
  1752.                                 stri2 = " Done in: " + self.sec2hms(Vars.time_done + Vars.time_done_1) + " "\
  1753.                                         + " pass 1: " + self.sec2hms(Vars.time_done) + " "\
  1754.                                         + " pass 2: " + self.sec2hms(Vars.time_done_1) + " "\
  1755.                                         + " input size:  " + Vars.in_file_size + " "\
  1756.                                         + " output size: " + Vars.out_file_size
  1757.                                 self.reportdata.set_text(stri2)
  1758.                                 if os.path.exists('ffmpeg2pass-0.log'):
  1759.                                         os.remove('ffmpeg2pass-0.log')
  1760.                                 if os.path.exists('ffmpeg2pass-0.log.mbtree'):
  1761.                                         os.remove('ffmpeg2pass-0.log.mbtree')
  1762.                                 if os.path.exists('x264_lookahead.clbin'):
  1763.                                         os.remove('x264_lookahead.clbin')
  1764.                                 Vars.eff_pass = 0
  1765.                                 self.but_f3_run.set_label('RUN')
  1766.                                 Vars.ENCODING = False
  1767.                                 restore_dir_button()
  1768.                                 restore_file_button()
  1769.                
  1770.                 def check_run(name):
  1771.                         t0 = time.time()
  1772.                         processname = name
  1773.                         for line in subprocess.Popen(["ps", "xa"], shell=False, stdout=subprocess.PIPE).stdout:
  1774.                                 fields = line.split()
  1775.                                 pid = fields[0]
  1776.                                 process = fields[4]
  1777.                                 if process.find(processname) >= 0:              
  1778.                                         if line.find(Vars.filename) >= 0:
  1779.                                                 mypid = pid
  1780.                                                 break
  1781.                         finn = True
  1782.                         while finn == True:
  1783.                                 time.sleep(1)
  1784.                                 try:
  1785.                                         finn = os.path.exists("/proc/" + mypid)
  1786.                                 except:
  1787.                                         Vars.eff_pass = 0
  1788.                                         self.but_f3_run.set_label('RUN')
  1789.                                         Vars.ENCODING = False
  1790.                                         restore_dir_button()
  1791.                                         restore_file_button()
  1792.                                         return
  1793.                         if Vars.eff_pass == 0:  
  1794.                                 tfin = time.time() - t0
  1795.                                 Vars.time_done = int(tfin)
  1796.                         else:
  1797.                                 tfin = time.time() - t0
  1798.                                 Vars.time_done_1 = int(tfin)
  1799.                         end_run()
  1800.                        
  1801.                 if Vars.ENCODING:
  1802.                         return
  1803.                
  1804.                 if Vars.eff_pass == 0:
  1805.                         self.check_out_file()
  1806.                         Vars.time_done = 0
  1807.                         Vars.time_done_1 = 0
  1808.                         if Vars.n_pass == 1:
  1809.                                 Vars.final_command1 = ""
  1810.                                 Vars.final_command2 = Vars.ffmpeg_ex + " -i \"" \
  1811.                                         + Vars.filename + "\" " \
  1812.                                         + Vars.p3_command \
  1813.                                         +  " \"" + Vars.newname + "\"" \
  1814.                                         + "\n"
  1815.                                 command = Vars.final_command2
  1816.                        
  1817.                         if Vars.n_pass == 2:
  1818.                                 Vars.final_command1 = Vars.ffmpeg_ex + " -i \"" \
  1819.                                         + Vars.filename + "\" " \
  1820.                                         + Vars.p3_comm_first_p \
  1821.                                         + "\n"
  1822.                                 Vars.final_command2 = Vars.ffmpeg_ex + " -i \"" \
  1823.                                         + Vars.filename + "\" " \
  1824.                                         + Vars.p3_command \
  1825.                                         +  " \"" + Vars.newname + "\"" \
  1826.                                         + "\n"
  1827.                                 command = Vars.final_command1
  1828.                 else:
  1829.                         command = Vars.final_command2
  1830.                        
  1831.                 if Vars.debug == 1:
  1832.                         if Vars.n_pass == 1:
  1833.                                 command = "echo $\'\n\n\n" + Vars.final_command2  + "\' \n"
  1834.                         if Vars.n_pass == 2:
  1835.                                 command = "echo $\'\n\n\n" + Vars.final_command1 + "\n" + Vars.final_command2 + "\' \n"
  1836.                 length = len(command)
  1837.                
  1838.                 self.terminal.feed_child(command, length)
  1839.                 #
  1840.                 if Vars.debug == 0:
  1841.                         self.but_f3_run.set_label('ENCODING ...')
  1842.                         self.but_f3_run.set_tooltip_text('')
  1843.                         self.reportdata.set_text('')
  1844.                         Vars.ENCODING = True
  1845.                         t = threading.Thread(name='child procs', target=check_run, args=(Vars.ffmpeg_ex,))
  1846.                         t.start()
  1847.              
  1848.         def dir_changed(self, button):
  1849.                 if Vars.ENCODING:
  1850.                         return
  1851.                 if Vars.first_run:
  1852.                         select = self.out_dir_chooserbut.get_uri()
  1853.                         decoded = url2pathname(select)
  1854.                         newdir = decoded.split('//')[1]
  1855.                         command = "cd " + "\"" + newdir + "\"" + "\n"
  1856.                         length = len(command)
  1857.                         self.terminal.feed_child(command, length)
  1858.                         self.f3_run_outdir_label.set_markup("<b>out dir:  </b>" + newdir)
  1859.                         Vars.outdir = newdir
  1860.                         self.check_out_file()
  1861.                 else:
  1862.                         Vars.first_run = 1
  1863.                        
  1864.                
  1865.         def on_but_black_det_clicked(self, button):
  1866.        
  1867.                 def update_progress(pprog):
  1868.                         self.progressbar_black_det.set_fraction(pprog)
  1869.                         return False
  1870.                
  1871.                 def update_textv(stri):
  1872.                         self.textbuffer.insert_at_cursor(stri, -1)
  1873.                         i = self.textbuffer.get_end_iter()
  1874.                         self.textview.scroll_to_iter(i, 0.0, True, 0.0, 1.0)
  1875.                         return False
  1876.  
  1877.                 def stopped():
  1878.                         stri = "Stopped by user.\n"
  1879.                         self.textbuffer.insert_at_cursor(stri, -1)
  1880.                         i = self.textbuffer.get_end_iter()
  1881.                         self.textview.scroll_to_iter(i, 0.0, True, 0.0, 1.0)
  1882.                         self.but_black_det.set_label("Black detect")
  1883.                         self.blackdet_spin.stop()
  1884.                         self.blackdet_spin.set_sensitive(False)
  1885.                         self.progressbar_black_det.set_fraction(0.0)
  1886.                         Vars.blackdet_is_run = False
  1887.                        
  1888.                 def bd_finish(detected):
  1889.                         if detected == 0:
  1890.                                 stri = "none black gap found"
  1891.                                 self.textbuffer.insert_at_cursor(stri, -1)
  1892.                         self.blackdet_spin.stop()
  1893.                         self.blackdet_spin.set_sensitive(False)
  1894.                         self.but_black_det.set_label("black detectet")
  1895.                         self.progressbar_black_det.set_fraction(1.0)
  1896.                         Vars.blackdet_is_run = False
  1897.                         Vars.BLACKDT_DONE = True
  1898.                
  1899.                 def my_thread():
  1900.                         Vars.blackdet_is_run = True
  1901.                         command = [Vars.ffmpeg_ex, '-i', Vars.filename, '-y', '-an', '-vf', 'blackdetect=d=.5', '-f', 'null', '/dev/null']
  1902.                         p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, universal_newlines=True)
  1903.                         self.bd_th = False
  1904.                         detected = 0
  1905.                         time_step = 0
  1906.                         pprog = 0.0
  1907.                         stri = ""
  1908.                        
  1909.                         while True:
  1910.                                 line = p.stderr.readline()
  1911.                                
  1912.                                 if 'time=' in line:
  1913.                                         if time_step == 9:
  1914.                                                 tml = line.split('time=')[1]
  1915.                                                 tml2 = tml.split(' ')[0]
  1916.                                                 time_n = tml2.split('.')[0]
  1917.                                                 time_n = self.hms2sec(time_n)
  1918.                                                 time_step = 0
  1919.                                                 pprog = float(time_n)/Vars.duration_in_sec
  1920.                                                 GLib.idle_add(update_progress, pprog)
  1921.                                                 time.sleep(0.002)
  1922.                                         else:
  1923.                                                 time_step = time_step + 1
  1924.                                                
  1925.                                 if 'blackdetect ' in line:
  1926.                                                 a = line
  1927.                                                 b = a.split('black_start:')[1]
  1928.                                                 start = b.split(' ')[0]
  1929.                                                 st_in_s = float(start)
  1930.                                                 st_in_s = int(st_in_s)
  1931.                                                 start_in_s = ("%4.1i"% (st_in_s))  
  1932.                                                 start = self.sec2hms(start)
  1933.                                                 stri = "black start at: " + start_in_s + " s, " + start + " "
  1934.                                                 c = b.split('black_duration:')[1]
  1935.                                                 durat = c.split(' ')[0]
  1936.                                                 dur_s = float(durat)
  1937.                                                 dur_s = "{0:0=3.1f}".format(dur_s)
  1938.                                                 stri = stri + "duration: " + dur_s + "s\n"
  1939.                                                 detected = 1
  1940.                                                 GLib.idle_add(update_textv, stri)
  1941.                                                 time.sleep(0.002)
  1942.                                 if line == '' and p.poll() != None:
  1943.                                         break
  1944.                                 if self.bd_th:
  1945.                                         p.communicate("q")
  1946.                                         break
  1947.                         try:
  1948.                                 p.communicate()
  1949.                         except:
  1950.                                 stopped()
  1951.                                 return
  1952.                         bd_finish(detected)    
  1953.                        
  1954.                 if Vars.BLACKDT_DONE:
  1955.                         return          
  1956.                        
  1957.                 if Vars.blackdet_is_run == False:
  1958.                         self.but_black_det.set_label("Stop")
  1959.                         self.textbuffer.set_text('') # clear for insert from start
  1960.                         stri = "Detected:\n\n"
  1961.                         start = self.textbuffer.get_start_iter()
  1962.                         self.textbuffer.insert(start, stri, -1)
  1963.                         self.blackdet_spin.set_sensitive(True)
  1964.                         self.blackdet_spin.start()                              
  1965.                         thread = threading.Thread(target=my_thread)
  1966.                         thread.daemon = True
  1967.                         thread.start()
  1968.                        
  1969.                 elif Vars.blackdet_is_run == True:
  1970.                         self.bd_th = True
  1971.        
  1972.                
  1973.         # -------------------------------------  
  1974.        
  1975.         def preview_logic(self):
  1976.                 if ( not(('-map ' in Vars.p3_command ) or ('-vf ' in Vars.p3_command )) ):
  1977.                         self.buttest.set_sensitive(False)
  1978.                         self.buttestspl.set_sensitive(False)
  1979.                 elif ('-map ' in Vars.p3_command) and ('-vf ' in Vars.p3_command):
  1980.                         self.buttest.set_sensitive(False)
  1981.                         self.buttestspl.set_sensitive(False)
  1982.                 elif '-map ' in Vars.p3_command:
  1983.                         self.buttest.set_sensitive(True)
  1984.                         self.buttestspl.set_sensitive(False)
  1985.                 elif '-vf ' in Vars.p3_command:
  1986.                         self.buttest.set_sensitive(True)
  1987.                         self.buttestspl.set_sensitive(True)
  1988.        
  1989.         def calcola_scale(self):
  1990.                 Vars.scalestr = ""
  1991.                 numero = self.size_spinner['adj'].get_value()
  1992.                 numero = int(numero/4)*4
  1993.                 dar = self.box2['cbox'].get_active()
  1994.                 lato = self.box1['cbox'].get_active()          
  1995.                 if (dar == 1): # 4/3
  1996.                         if (lato == 1):
  1997.                                 n2 = numero/4*3
  1998.                         else:
  1999.                                 n2 = numero/3*4
  2000.                         setdar = ",setdar=4/3,setsar=1/1"
  2001.                 elif (dar == 0): # 16/9
  2002.                         if (lato == 1):
  2003.                                 n2 = numero/16*9
  2004.                         else:
  2005.                                 n2 = numero/9*16
  2006.                         setdar = ",setdar=16/9,setsar=1/1"      
  2007.                 elif (dar == 2): #1/1 same as input  
  2008.                         w = int(Vars.video_width)
  2009.                         h = int(Vars.video_height)
  2010.                         if self.checkcr.get_active():
  2011.                                 crT = int(self.spinct['adj'].get_value())
  2012.                                 crB = int(self.spincb['adj'].get_value())
  2013.                                 crL = int(self.spincl['adj'].get_value())
  2014.                                 crR = int(self.spincr['adj'].get_value())
  2015.                                 h = h - crT - crB
  2016.                                 w = w - crL - crR
  2017.                         if (lato == 1): # h                
  2018.                                 n2 = (float(h)/(w/float(numero)))  
  2019.                         else:
  2020.                                 n2 = (float(w)/(h/float(numero)))
  2021.                         setdar = ""
  2022.                 n2 = int(n2/4)*4                    
  2023.                 Vars.scalestr = "scale="
  2024.                 if (lato == 1):
  2025.                         Vars.scalestr = Vars.scalestr + str(numero) + ":" + str(n2)
  2026.                 else:
  2027.                         Vars.scalestr = Vars.scalestr + str(n2) + ":" + str(numero)                  
  2028.                 lanc = self.check.get_active()  
  2029.                 if lanc:
  2030.                         Vars.scalestr = Vars.scalestr + ":flags=lanczos" + setdar  
  2031.                 else:
  2032.                         Vars.scalestr = Vars.scalestr + setdar
  2033.                 self.outfilter()
  2034.                      
  2035.         def outfilter(self):
  2036.                 final_str = ""
  2037.                 if (Vars.deint_str != ""):
  2038.                         final_str = final_str + Vars.deint_str
  2039.                 if (Vars.crop_str != ""):
  2040.                         if (final_str == ""):
  2041.                                 final_str = final_str + Vars.crop_str
  2042.                         else:
  2043.                                         final_str = final_str + "," + Vars.crop_str
  2044.                 if (Vars.scalestr != ""):
  2045.                         if (final_str == ""):
  2046.                                 final_str = final_str + Vars.scalestr
  2047.                         else:
  2048.                                 final_str = final_str + "," + Vars.scalestr
  2049.                 if (Vars.dn_str != ""):
  2050.                         if (final_str == ""):
  2051.                                 final_str = final_str + Vars.dn_str
  2052.                         else:
  2053.                                 final_str = final_str + "," + Vars.dn_str
  2054.                 if (Vars.uns_str != ""):
  2055.                         if (final_str == ""):
  2056.                                 final_str = final_str + Vars.uns_str
  2057.                         else:
  2058.                                 final_str = final_str + "," + Vars.uns_str
  2059.                 Vars.filter_test_str = final_str
  2060.                
  2061.                 if (final_str != ""):
  2062.                         Vars.final_vf_str = "-vf " + final_str
  2063.                 else:
  2064.                         Vars.final_vf_str = ""
  2065.                
  2066.                 label_str = ""          
  2067.                 if (Vars.maps_str != ""):
  2068.                         label_str = label_str + Vars.maps_str + " "  
  2069.                 if (Vars.time_final_str != ""):
  2070.                         label_str = label_str + Vars.time_final_str + " "                      
  2071.                 if (Vars.fr_str != ""):
  2072.                         label_str = label_str + Vars.fr_str + " "                      
  2073.                 if (Vars.final_vf_str != ""):
  2074.                         label_str = label_str + Vars.final_vf_str
  2075.                    
  2076.                 self.labelout.set_text(label_str)      
  2077.                 self.make_p3_out_command()
  2078.                 if (Vars.info_str != ""):
  2079.                         self.preview_logic()    
  2080.        
  2081.         def make_p3_out_command(self):
  2082.                 Vars.p3_command = ""
  2083.                 Vars.p3_comm_first_p = ""
  2084.                 self.f3_out_label.set_label("")
  2085.                 if (Vars.maps_str != ""):
  2086.                         Vars.p3_command = Vars.maps_str + " "  
  2087.                 if (Vars.time_final_str != ""):
  2088.                         Vars.p3_command = Vars.p3_command + Vars.time_final_str + " "
  2089.                 if ( (Vars.fr_str != "") and (Vars.v_copy == 0) ): # check -vcopy
  2090.                                 Vars.p3_command = Vars.p3_command + Vars.fr_str + " "                  
  2091.                 if ( (Vars.final_vf_str != "") and (Vars.v_copy == 0) ): # check -vcopy
  2092.                         Vars.p3_command = Vars.p3_command + Vars.final_vf_str + " "
  2093.                 # first pass    
  2094.                 if Vars.first_pass:
  2095.                         Vars.p3_comm_first_p = Vars.p3_command + Vars.video_codec_str + " "
  2096.                         if self.check_cpu.get_active():
  2097.                                 Vars.p3_comm_first_p = Vars.p3_comm_first_p + "-threads 3 "
  2098.                         if "libx264" in Vars.p3_comm_first_p:
  2099.                                 Vars.p3_comm_first_p = Vars.p3_comm_first_p + "-pass 1 -fastfirstpass 1 "
  2100.                         else:
  2101.                                 Vars.p3_comm_first_p = Vars.p3_comm_first_p + "-pass 1 "
  2102.                         Vars.p3_comm_first_p = Vars.p3_comm_first_p + "-an -f null /dev/null"
  2103.                 else:
  2104.                         Vars.p3_comm_first_p = ""
  2105.                        
  2106.                 if ( (Vars.final_af_str != "") and (Vars.a_copy == 0) and (Vars.audionone == 0) ):  # check -acopy -an
  2107.                         Vars.p3_command = Vars.p3_command + Vars.final_af_str + " "
  2108.                 if (Vars.final_codec_str != ""):
  2109.                         Vars.p3_command = Vars.p3_command + Vars.final_codec_str + " "
  2110.                
  2111.                 #second pass
  2112.                 if Vars.n_pass == 2:
  2113.                         Vars.p3_command = Vars.p3_command + "-pass 2" + " "
  2114.                        
  2115.                 if (Vars.container_str != ""):
  2116.                         Vars.p3_command = Vars.p3_command + Vars.container_str + " "
  2117.                 if (Vars.final_other_str != ""):
  2118.                         Vars.p3_command = Vars.p3_command + Vars.final_other_str + " "
  2119.                 self.f3_out_label.set_label(Vars.p3_command)
  2120.                
  2121.         def main(self):
  2122.                 Gtk.main()
  2123.  
  2124.                
  2125.        
  2126.  
  2127. if __name__ == "__main__":
  2128.         GObject.threads_init()
  2129.         ProgrammaGTK()
  2130.         Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement