Package gui

Source Code of gui.FFMpegGui

/*
*  Multiconvert
*  Multiconvert is a lightweight and plattform independent Video conversion tool
*  based on ffmpeg and written in Java.
*
*  Copyright (C) 2010 Sebastian Weidenbach
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*  ----------------------------------------------------------------------------------
*  Historie:
*  2010−02−17 Erste Fassung              <sebastianwe@users.sourceforge.net>
*/

package gui;

import java.awt.Font;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import language.Deutsch;
import language.English;
import language.Language;
import language.Spanish;
import util.ConvertUtils;
import util.SwingUtils;
import util.Constants;
import ffmpeg.FFMpeg;
import ffmpeg.FFMpegProgressReceiver;

/**
* FFMpegGui represents the main window of the application
*
* @author SebastianWe
*/
public class FFMpegGui extends JFrame implements FFMpegProgressReceiver
{
  /*
   * FFMpegGui has access to ffmpeg and saves Language, a progressbar, the input/output file button, the convert button
   * When the application is converting <converting> is set to true
   * The convert process is initiated by runner
   * When the application waits for a download it sets waitfordl to true
   */
  private static final long serialVersionUID = 8043159648233921240L;
  private FFMpeg ffmpeg;
  private JProgressBar progress;
  private final JButton selInputFile;
  private final JButton selOutputFile;
  private JButton convert;
  private boolean converting = false;
  private Thread runner;
  private boolean waitfordl = false;
  private Language lang;
  private final JTextField outputFile;
 
  /**
   * Creates a new Muliconvert main window
   */
  public FFMpegGui()
  {
    super("Multiconvert");
   
    final FFMpegGui self = this;
   
    String path = "";
   
    final Vector<String> presets = new Vector<String>();
   
    // Read settings
    try
    {
      Scanner scan = new Scanner(new File(System.getProperty("user.home") + "/.multiconvert/" + "settings.ini"));
      path = scan.nextLine().substring(5).trim();
      int l = Integer.parseInt(scan.nextLine().substring(5).trim());
      if(l == 0)
        lang = new Deutsch();
      else if(l == 1)
        lang = new English();
      else if(l == 2)
        lang = new Spanish();
      while(scan.hasNextLine())
        presets.add(scan.nextLine().split("=")[1]);
       
      scan.close();
    }
    catch(Exception exc){} 
   
    if(lang == null)
      lang = new English();
   
    try
    {
      if(!path.isEmpty())
        ffmpeg = new FFMpeg(path, lang, this);
      else if(System.getProperty("os.name").contains("Linux") ||
          System.getProperty("os.name").contains("Solaris") ||
          System.getProperty("os.name").contains("Mac") ||
          System.getProperty("os.name").contains("Unix"))
        ffmpeg = new FFMpeg("/usr/bin/ffmpeg", lang, this);
      else
        ffmpeg = new FFMpeg("ffmpeg/ffmpeg.exe", lang, this);
    }
    catch (IOException e1)
    {
      JOptionPane.showMessageDialog(self, lang.exenotfound, lang.error, JOptionPane.ERROR_MESSAGE);
    }
   
   
    // Set Swing components
    selInputFile = new JButton(lang.search);
    selOutputFile = new JButton(lang.search);
   
    JMenu datei = new JMenu(lang.data);
    JMenu hilfe = new JMenu(lang.help);
   
    JMenuItem ueber = new JMenuItem(lang.about, new ImageIcon("images/info.gif"));
    ueber.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        new Info(self, lang);
      }
    });
   
    JMenuItem help = new JMenuItem(lang.help, new ImageIcon("images/help.gif"));
    help.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        SwingUtils.openUrl(self, Constants.tracURL, lang);
      }
    });
   
    JMenuItem update = new JMenuItem(lang.update, new ImageIcon("images/update.gif"));
    update.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        String newVersion = SwingUtils.webDownload(self, "http://mypowerhost.de/multiconvert/version", lang);
       
        if(!Constants.version.equals(newVersion) && !newVersion.isEmpty())
        {
          int result = JOptionPane.showConfirmDialog(self, lang.updateavailable, lang.newupdate, JOptionPane.INFORMATION_MESSAGE);
          if(result == JOptionPane.YES_OPTION)
          {
            if(System.getProperty("os.name").contains("Linux")   ||
               System.getProperty("os.name").contains("Solaris")||
               System.getProperty("os.name").contains("Mac")   ||
               System.getProperty("os.name").contains("Unix"))
              SwingUtils.openUrl(self, "http://mypowerhost.de/multiconvert/multiconvert_win_" + newVersion + ".msi", lang);
            else
              SwingUtils.openUrl(self, "http://mypowerhost.de/multiconvert/multiconvert_" + newVersion + ".tar.gz", lang);
          }
        }
        else if(Constants.version.equals(newVersion) && !newVersion.isEmpty())
        {
          JOptionPane.showMessageDialog(self, lang.updatenotavailable, lang.noupdate, JOptionPane.INFORMATION_MESSAGE);
        }
      }
    });
   
    JMenuItem openInet = new JMenuItem(lang.inet);
   
    JMenuItem openVideo = new JMenuItem(lang.ovideo);
   
    JMenuItem einstellungen = new JMenuItem(lang.settings, new ImageIcon("images/settings.gif"));
    einstellungen.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        new Settings(self, lang);
      }
    });
   
    JMenuItem beenden = new JMenuItem(lang.quit, new ImageIcon("images/exit.gif"));
    beenden.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        System.exit(0);
      }
    });
   
    datei.add(openVideo);
    datei.add(openInet);
    datei.addSeparator();
    datei.add(einstellungen);
    datei.addSeparator();
    datei.add(beenden);
   
    hilfe.add(update);
    hilfe.add(help);
    hilfe.addSeparator();
    hilfe.add(ueber);
   
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(datei);
    menuBar.add(hilfe);
   
    setJMenuBar(menuBar);
   
    final JButton savePreset = new JButton(lang.savepreset);
    savePreset.setLocation(10, 425);
    savePreset.setSize(180, 20);
    savePreset.setVisible(false);
   
    final JLabel preset = new JLabel(lang.name);
    preset.setLocation(10, 400);
    preset.setSize(70, 20);
    preset.setFont(preset.getFont().deriveFont(Font.BOLD));
    preset.setVisible(false);
   
    final JTextField prname = new JTextField();
    prname.setLocation(75, 400);
    prname.setSize(115, 20);
    prname.setVisible(false);
   
    final JPanel input = new JPanel();
    input.setBorder(new TitledBorder(lang.input));
    input.setSize(200, 455);
    input.setLocation(5, 0);
    input.setLayout(null);
   
    final JPanel output = new JPanel();
    output.setBorder(new TitledBorder(lang.output));
    output.setSize(200, 455);
    output.setLocation(210, 0);
    output.setLayout(null);
   
    progress = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
    progress.setSize(405, 15);
    progress.setLocation(5, 500);
    progress.setValue(0);
   
    JLabel file = new JLabel(lang.data);
    file.setLocation(10,25);
    file.setSize(40, 20);
   
    final JTextField inputFile = new JTextField();
    inputFile.setLocation(50, 25);
    inputFile.setSize(140,20);
    inputFile.setEditable(true);
   
    final JPopupMenu context = new JPopupMenu();
    JMenuItem paste = new JMenuItem(lang.insert);
    context.add(paste);
    paste.addMouseListener(new MouseListener()
    {
      @Override
      public void mouseClicked(MouseEvent e)
      {
      }
   
      @Override
      public void mousePressed(MouseEvent e)
      {
      }
   
      @Override
      public void mouseExited(MouseEvent e)
      {
      }
   
      @Override
      public void mouseEntered(MouseEvent e)
      {
      }
   
      @Override
      public void mouseReleased(MouseEvent e)
      {
        Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable transferData = systemClipboard.getContents(null);
        DataFlavor[] dataFlavor = transferData.getTransferDataFlavors();
        String s = null;
        for(int i = 0; i < dataFlavor.length; i++)
        {
          try
          {
            s = (String) transferData.getTransferData(dataFlavor[i]);
            if(s != null)
              break;
          }
          catch (Exception exc)
          {
            if(Constants.debug)
              exc.printStackTrace();
          }
        }
       
        if(s != null)
        {
          inputFile.setText(s);
          KeyEvent k = new KeyEvent(self, KeyEvent.KEY_RELEASED, 0, 0, 0, 'x');
          Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(k);
        }
      }
    });
   
    inputFile.addMouseListener(new MouseListener()
    {
      @Override
      public void mouseReleased(MouseEvent e)
      {
        if (e.isPopupTrigger())
        {
          context.show(inputFile, e.getX(), e.getY());
        }
      }
   
      @Override
      public void mousePressed(MouseEvent e)
      {
        if (e.isPopupTrigger())
        {
          context.show(inputFile, e.getX(), e.getY());
        }
      }
   
      @Override
      public void mouseExited(MouseEvent e)
      {
      }
   
      @Override
      public void mouseEntered(MouseEvent e)
      {
      }
   
      @Override
      public void mouseClicked(MouseEvent e)
      {
      }
    });
   
    final JFileChooser outputSelect = new JFileChooser();
    outputSelect.setFileFilter(new VideoFilter());
   
    JLabel oFile = new JLabel(lang.data);
    oFile.setLocation(10,25);
    oFile.setSize(40, 20);
   
    outputFile = new JTextField();
    outputFile.setLocation(50, 25);
    outputFile.setSize(140,20);
    outputFile.setEditable(false);
   
    final JFileChooser inputSelect = new JFileChooser();
   
    final JLabel inputDuration = new JLabel("-");
    inputDuration.setLocation(90, 15);
    inputDuration.setSize(80, 20);
    inputDuration.setFont(inputDuration.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputMRate = new JLabel("-");
    inputMRate.setLocation(90, 40);
    inputMRate.setSize(80, 20);
    inputMRate.setFont(inputMRate.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputAFmt = new JLabel("-");
    inputAFmt.setLocation(90, 15);
    inputAFmt.setSize(80, 20);
    inputAFmt.setFont(inputAFmt.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputAFmtSpec = new JLabel("-");
    inputAFmtSpec.setLocation(10, 40);
    inputAFmtSpec.setSize(160, 20);
    inputAFmtSpec.setFont(inputAFmtSpec.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputFreq = new JLabel("-");
    inputFreq.setLocation(90, 65);
    inputFreq.setSize(80, 20);
    inputFreq.setFont(inputFreq.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputChannel = new JLabel("-");
    inputChannel.setLocation(90, 90);
    inputChannel.setSize(80, 20);
    inputChannel.setFont(inputChannel.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputARate = new JLabel("-");
    inputARate.setLocation(90, 115);
    inputARate.setSize(80, 20);
    inputARate.setFont(inputDuration.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputFmt = new JLabel("-");
    inputFmt.setLocation(90, 15);
    inputFmt.setSize(80, 20);
    inputFmt.setFont(inputFmt.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputFmtSpec = new JLabel("-");
    inputFmtSpec.setLocation(10, 40);
    inputFmtSpec.setSize(160, 20);
    inputFmtSpec.setFont(inputFmtSpec.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputColorSpace = new JLabel("-");
    inputColorSpace.setLocation(90, 65);
    inputColorSpace.setSize(80, 20);
    inputColorSpace.setFont(inputColorSpace.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputScala = new JLabel("-");
    inputScala.setLocation(90, 90);
    inputScala.setSize(80, 20);
    inputScala.setFont(inputScala.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputRatio = new JLabel("-");
    inputRatio.setLocation(90, 115);
    inputRatio.setSize(80, 20);
    inputRatio.setFont(inputRatio.getFont().deriveFont(Font.PLAIN));
   
    final JLabel inputRate = new JLabel("-");
    inputRate.setLocation(90, 140);
    inputRate.setSize(80, 20);
    inputRate.setFont(inputRate.getFont().deriveFont(Font.PLAIN));
   
    JLabel orate = new JLabel(lang.rate);
    orate.setLocation(10, 140);
    orate.setSize(70, 20);
    orate.setFont(orate.getFont().deriveFont(Font.BOLD));
   
    final JLabel ofps = new JLabel(lang.bps);
    ofps.setLocation(10, 165);
    ofps.setSize(70, 20);
    ofps.setFont(ofps.getFont().deriveFont(Font.BOLD));
   
    final JCheckBox doFPS = new JCheckBox();
    doFPS.setLocation(155, 165);
    doFPS.setSize(20, 20);
    doFPS.setSelected(true);
   
    final JTextField outputscala1 = new JTextField();
    outputscala1.setLocation(90, 90);
    outputscala1.setSize(30, 20);
   
    JLabel cross = new JLabel("x");
    cross.setFont(cross.getFont().deriveFont(Font.PLAIN));
    cross.setSize(15, 20);
    cross.setLocation(125, 90);
   
    final JTextField outputscala2 = new JTextField();
    outputscala2.setLocation(140, 90);
    outputscala2.setSize(30, 20);
   
    outputscala1.setText("0");
    outputscala2.setText("0");
   
    if(ffmpeg != null && ffmpeg.getInputMedium() != null)
    {
      outputscala1.setText(String.valueOf(ConvertUtils.getWidthFromScala(ffmpeg.getInputMedium().getScala())));
      outputscala2.setText(String.valueOf(ConvertUtils.getHeightFromScala(ffmpeg.getInputMedium().getScala())));
    }
   
    outputscala1.setEditable(false);
    outputscala2.setEditable(false);
   
    final JComboBox oratiosw = new JComboBox(new String[] {lang.automatic, lang.userdefined, "cif 176x144", "4cif 704x576", "vga 640x480", "svga 800x600", "xga 1024x768", "sxga 1280x1024", "uxga 1600x1200", "qxga 2048x1536", "qsxga 2560x2048", "hd480 852x480", "hd720 1280x720", "hd1080 1920x1080", "dvd 720x576"});
    oratiosw.setLocation(90, 65);
    oratiosw.setSize(80, 20);
    oratiosw.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent arg0)
      {
        if(oratiosw.getSelectedIndex() == 0)
        {
          outputscala1.setEditable(false);
          outputscala2.setEditable(false);
          if(ffmpeg.getInputMedium() != null)
          {
            outputscala1.setText(String.valueOf(ConvertUtils.getWidthFromScala(ffmpeg.getInputMedium().getScala())))
            outputscala2.setText(String.valueOf(ConvertUtils.getHeightFromScala(ffmpeg.getInputMedium().getScala())));
          }
          else
          {
            outputscala1.setText("0");
            outputscala2.setText("0");
          }
        }
        else if(oratiosw.getSelectedIndex() == 1)
        {
          outputscala1.setEditable(true);
          outputscala2.setEditable(true);
        }
        else
        {
          outputscala1.setEditable(false);
          outputscala2.setEditable(false);
          outputscala1.setText(String.valueOf(ConvertUtils.getWidthFromScala((String) oratiosw.getSelectedItem())));
          outputscala2.setText(String.valueOf(ConvertUtils.getHeightFromScala((String) oratiosw.getSelectedItem())));
        }
      }
    });
   
   
    inputFile.addKeyListener(new KeyListener()
    {
      @Override
      public void keyTyped(KeyEvent e)
      {
      }
   
      @Override
      public void keyPressed(KeyEvent e)
      {
      }
   
      @Override
      public void keyReleased(KeyEvent e)
      {
        if(ffmpeg == null)
        {
          JOptionPane.showMessageDialog(self, lang.exenotfound, lang.error, JOptionPane.ERROR_MESSAGE);
          return;
        }
       
        inputFmt.setText("-");
        inputFmtSpec.setText("-");
        inputFmtSpec.setToolTipText("");
        inputColorSpace.setText("-");
        inputRatio.setText("-");
        inputScala.setText("-");
        inputRate.setText("-");
        inputDuration.setText("-");
        inputMRate.setText("-");
        inputAFmt.setText("-");
        inputAFmtSpec.setText("-");
        inputAFmtSpec.setToolTipText("");
        inputChannel.setText("-");
        inputFreq.setText("-");
        inputARate.setText("-");
       
        File f = new File(inputFile.getText());
        if(f.exists() && f.isFile() || inputFile.getText().contains("youtube"))
        {
          inputFile.setText(inputFile.getText());
         
          try
          {
            JOptionPane.showMessageDialog(self, lang.downloadinfo, lang.download, JOptionPane.INFORMATION_MESSAGE);
                       
            ffmpeg.setInput(inputFile.getText(), self, self);
           
            waitfordl = true;
          }
          catch(IOException exc)
          {
            if(Constants.debug)
              exc.printStackTrace();
          }
        }
      }
    });
   
    selInputFile.setLocation(10, 50);
    selInputFile.setSize(180, 20);
    selInputFile.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        if(ffmpeg == null)
        {
          JOptionPane.showMessageDialog(self, lang.exenotfound, lang.error, JOptionPane.ERROR_MESSAGE);
          return;
        }
       
        inputFmt.setText("-");
        inputFmtSpec.setText("-");
        inputFmtSpec.setToolTipText("");
        inputColorSpace.setText("-");
        inputRatio.setText("-");
        inputScala.setText("-");
        inputRate.setText("-");
        inputDuration.setText("-");
        inputMRate.setText("-");
        inputAFmt.setText("-");
        inputAFmtSpec.setText("-");
        inputAFmtSpec.setToolTipText("");
        inputChannel.setText("-");
        inputFreq.setText("-");
        inputARate.setText("-");
       
        inputSelect.addChoosableFileFilter(new VideoFilter());

       
        int ret;
        if(!waitfordl)
          ret = inputSelect.showOpenDialog(self);
        else
        {
          ret = JFileChooser.APPROVE_OPTION;
          inputSelect.setSelectedFile(new File(System.getProperty("user.home") + "/.multiconvert/video.flv"));
          waitfordl = false;
        }
        if(ret == JFileChooser.APPROVE_OPTION)
        {
          inputFile.setText(inputSelect.getSelectedFile().getAbsolutePath());
         
          try
          {
            ffmpeg.setInput(inputFile.getText(), self, null);
            if(ffmpeg.getInputMedium().getVideoCodec() != null)
            {
              inputFmt.setText(ffmpeg.getInputMedium().getVideoCodec().getShortDescription());
              inputFmtSpec.setText(ffmpeg.getInputMedium().getVideoCodec().getLongDescription());
              inputFmtSpec.setToolTipText(ffmpeg.getInputMedium().getVideoCodec().getLongDescription());
              inputColorSpace.setText(ffmpeg.getInputMedium().getColorSpace());
              inputRatio.setText(ffmpeg.getInputMedium().getAspectRatio());
              inputScala.setText(ffmpeg.getInputMedium().getScala());
              inputRate.setText(ffmpeg.getInputMedium().getRate());
             
              if(oratiosw.getSelectedIndex() == 0)
              {
                if(ffmpeg.getInputMedium() != null)
                {
                  outputscala1.setText(String.valueOf(ConvertUtils.getWidthFromScala(ffmpeg.getInputMedium().getScala())))
                  outputscala2.setText(String.valueOf(ConvertUtils.getHeightFromScala(ffmpeg.getInputMedium().getScala())));
                }
              }
            }
           
            // Zeit berechnen
            double time = ffmpeg.getInputMedium().getDuration();
            int h = (int) (time / 60 / 60);
            time -= h * 60 * 60;
            int m = (int) (time / 60);
            time -= m * 60;
            int s = (int) time;
           
            inputDuration.setText(h + ":" + m + ":" + s);
           
            if(ffmpeg.getInputMedium().getAudioCodec() != null)
            {
              inputMRate.setText(ffmpeg.getInputMedium().getMRate());
              inputAFmt.setText(ffmpeg.getInputMedium().getAudioCodec().getShortDescription());
              inputAFmtSpec.setText(ffmpeg.getInputMedium().getAudioCodec().getLongDescription());
              inputAFmtSpec.setToolTipText(ffmpeg.getInputMedium().getAudioCodec().getLongDescription());
              inputARate.setText(ffmpeg.getInputMedium().getARate());
              inputFreq.setText(ffmpeg.getInputMedium().getAFreq());
              inputChannel.setText(ffmpeg.getInputMedium().getAChannel());
            }
          }
          catch(IOException exc)
          {
            if(Constants.debug)
              exc.printStackTrace();
          }
        }
      }
    });
   
    openVideo.addActionListener(new ActionListener()
    { 
      @Override
      public void actionPerformed(ActionEvent e)
      {
        selInputFile.doClick();
      }
    });
   
    selOutputFile.setLocation(10, 50);
    selOutputFile.setSize(180, 20);
   
    JPanel inputMain = new JPanel();
    inputMain.setLayout(null);
    inputMain.setBorder(new TitledBorder(lang.main));
    inputMain.setSize(180, 65);
    inputMain.setLocation(10, 75);
   
    JPanel inputVideo = new JPanel();
    inputVideo.setLayout(null);
    inputVideo.setBorder(new TitledBorder(lang.video));
    inputVideo.setSize(180, 165);
    inputVideo.setLocation(10, 140);
   
    final JPanel outputVideo = new JPanel();
    outputVideo.setLayout(null);
    outputVideo.setBorder(new TitledBorder(lang.video));
    outputVideo.setSize(180, 190);
    outputVideo.setLocation(10, 95);
    outputVideo.setVisible(false);
   
    JPanel inputAudio = new JPanel();
    inputAudio.setLayout(null);
    inputAudio.setBorder(new TitledBorder(lang.audio));
    inputAudio.setSize(180,140);
    inputAudio.setLocation(10, 305);
   
    final JPanel outputAudio = new JPanel();
    outputAudio.setLayout(null);
    outputAudio.setBorder(new TitledBorder(lang.audio));
    outputAudio.setSize(180,115);
    outputAudio.setLocation(10, 285);
    outputAudio.setVisible(false);
   
    JLabel out = new JLabel(lang.type);
    out.setLocation(10, 75);
    out.setSize(40, 20);
    out.setFont(out.getFont().deriveFont(Font.BOLD));
   
    String[] types = new String[4 + presets.size()];
    types[0] = "VCD";
    types[1] = "SVCD";
    types[2] = "DVD";
    types[3] = lang.userdefined;
   
    for(int i = 4; i < types.length; i++)
    {
      String p = presets.get(i - 4);
      String[] line = p.split("\\|");
      types[i] = line[0];
    }
   
    final JComboBox outType = new JComboBox(types);
    outType.setLocation(50, 75);
    outType.setSize(140,20);
   
    selOutputFile.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        if(ffmpeg == null)
        {
          JOptionPane.showMessageDialog(self, lang.exenotfound, lang.error, JOptionPane.ERROR_MESSAGE);
          return;
        }
       
        int ret = outputSelect.showSaveDialog(self);
        if(ret == JFileChooser.APPROVE_OPTION)
        {
          String f = outputSelect.getSelectedFile().getAbsolutePath().toLowerCase();
          if(f.endsWith(".wmv") || f.endsWith(".avi") || f.endsWith(".mpeg") || f.endsWith(".mpg") || f.endsWith(".flv") || f.endsWith(".mp4"))
            outputFile.setText(outputSelect.getSelectedFile().getAbsolutePath());
          else
            if(outType.getSelectedIndex() < 3)
              outputFile.setText(outputSelect.getSelectedFile().getAbsolutePath() + ".mpeg");
            else
              outputFile.setText(outputSelect.getSelectedFile().getAbsolutePath() + ".avi");
        }
      }
    });
   
    JLabel ofmt = new JLabel(lang.format);
    ofmt.setLocation(10, 15);
    ofmt.setSize(70,20);
    ofmt.setFont(ofmt.getFont().deriveFont(Font.BOLD));
   
    final JLabel outputFmtSpec = new JLabel();
    outputFmtSpec.setLocation(10, 40);
    outputFmtSpec.setSize(160, 20);
    outputFmtSpec.setFont(outputFmtSpec.getFont().deriveFont(Font.PLAIN));
   
   
    final JComboBox outputFmt;
   
    if(ffmpeg != null)
      outputFmt = new JComboBox(ffmpeg.getSupportedCodecsList(FFMpeg.VIDEO));
    else
      outputFmt = new JComboBox();
   
    outputFmt.setLocation(90, 15);
    outputFmt.setSize(80, 20);
    outputFmt.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        outputFmtSpec.setText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputFmt.getSelectedItem()).getLongDescription());
        outputFmtSpec.setToolTipText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputFmt.getSelectedItem()).getLongDescription());
      }
    });
   
    if(ffmpeg != null)
    {
      outputFmtSpec.setText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputFmt.getSelectedItem()).getLongDescription());
      outputFmtSpec.setToolTipText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputFmt.getSelectedItem()).getLongDescription());
    }
   
    JLabel oscala = new JLabel(lang.size);
    oscala.setLocation(10, 65);
    oscala.setSize(70, 20);
    oscala.setFont(oscala.getFont().deriveFont(Font.BOLD));
   
    JLabel oratio = new JLabel(lang.ratio);
    oratio.setLocation(10, 115);
    oratio.setSize(70, 20);
    oratio.setFont(oratio.getFont().deriveFont(Font.BOLD));
   
    final JComboBox outputratio = new JComboBox(new String[] { lang.automatic, "16:9", "4:3" });
    outputratio.setLocation(90, 115);
    outputratio.setSize(80, 20);
   
    final JTextArea outputbit = new JTextArea();
    outputbit.setLocation(90, 140);
    outputbit.setSize(40, 20);
   
    final JTextArea outputhz = new JTextArea("25");
    outputhz.setLocation(90, 165);
    outputhz.setSize(40,20);
   
    JLabel outputbits = new JLabel("kb/s");
    outputbits.setLocation(135, 140);
    outputbits.setSize(40, 20);
    outputbits.setFont(outputbits.getFont().deriveFont(Font.PLAIN));
   
    JLabel outputhzs = new JLabel("fps");
    outputhzs.setLocation(135, 165);
    outputhzs.setSize(40, 20);
    outputhzs.setFont(outputhzs.getFont().deriveFont(Font.PLAIN));
   
    JLabel oaFmt = new JLabel(lang.format);
    oaFmt.setLocation(10, 15);
    oaFmt.setSize(70, 20);
    oaFmt.setFont(oaFmt.getFont().deriveFont(Font.BOLD))

    final JLabel oaRate = new JLabel(lang.rate);
    oaRate.setLocation(10, 90);
    oaRate.setSize(70, 20);
    oaRate.setFont(oaRate.getFont().deriveFont(Font.BOLD));
   
    final JLabel oaChannels = new JLabel(lang.channels);
    oaChannels.setLocation(10, 65);
    oaChannels.setSize(70, 20);
    oaChannels.setFont(oaChannels.getFont().deriveFont(Font.BOLD));
   
    final JLabel outputAFmtSpec = new JLabel();
    outputAFmtSpec.setLocation(10, 40);
    outputAFmtSpec.setSize(160, 20);
    outputAFmtSpec.setFont(outputAFmtSpec.getFont().deriveFont(Font.PLAIN));
   
    final JTextArea outputARate = new JTextArea();
    outputARate.setLocation(90, 90);
    outputARate.setSize(40, 20);
    outputARate.setEnabled(false);
    oaRate.setEnabled(false);
    oaChannels.setEnabled(false);
   
   
    final JComboBox outputAChannels = new JComboBox(new String[] { lang.automatic, "Mono", "Stereo", "2.1", "5.1" });
    outputAChannels.setLocation(90, 65);
    outputAChannels.setSize(80, 20);
    outputAChannels.setEnabled(false);
   
    final JComboBox outputAFmt;
   
    if(ffmpeg != null)
      outputAFmt = new JComboBox(ffmpeg.getSupportedCodecsList(FFMpeg.AUDIO));
    else
      outputAFmt = new JComboBox();
   
    outputAFmt.setLocation(90, 15);
    outputAFmt.setSize(80, 20);

    outputAFmt.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        if(outputAFmt.getSelectedIndex() == 0)
        {
          outputARate.setEnabled(false);
          oaRate.setEnabled(false);
          oaChannels.setEnabled(false);
          outputAChannels.setEnabled(false);
        }
        else
        {
          outputARate.setEnabled(true);
          oaRate.setEnabled(true);
          oaChannels.setEnabled(true);
          outputAChannels.setEnabled(true);
        }
        outputAFmtSpec.setText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputAFmt.getSelectedItem()).getLongDescription());
        outputAFmtSpec.setToolTipText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputAFmt.getSelectedItem()).getLongDescription());
      }
    });
   
    if(ffmpeg != null)
    {
      outputAFmtSpec.setText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputAFmt.getSelectedItem()).getLongDescription());
      outputAFmtSpec.setToolTipText(ffmpeg.getSupportedCodecs().getCodecByName((String) outputAFmt.getSelectedItem()).getLongDescription());
    }
   
    JLabel outputabits = new JLabel("kb/s");
    outputabits.setLocation(135, 90);
    outputabits.setSize(40, 20);
    outputabits.setFont(outputabits.getFont().deriveFont(Font.PLAIN));
   
    JLabel duration = new JLabel(lang.duration);
    duration.setLocation(10, 15);
    duration.setSize(70,20);
    duration.setFont(duration.getFont().deriveFont(Font.BOLD));
   
    JLabel mrate = new JLabel(lang.rate);
    mrate.setLocation(10,40);
    mrate.setSize(70,20);
    mrate.setFont(mrate.getFont().deriveFont(Font.BOLD));
   
    JLabel fmt = new JLabel(lang.format);
    fmt.setLocation(10, 15);
    fmt.setSize(70, 20);
    fmt.setFont(fmt.getFont().deriveFont(Font.BOLD));
   
    JLabel colorSpace = new JLabel(lang.colorspace);
    colorSpace.setLocation(10, 65);
    colorSpace.setSize(70, 20);
    colorSpace.setFont(colorSpace.getFont().deriveFont(Font.BOLD));
   
    JLabel scala = new JLabel(lang.size);
    scala.setLocation(10, 90);
    scala.setSize(70, 20);
    scala.setFont(scala.getFont().deriveFont(Font.BOLD));
   
    JLabel ratio = new JLabel(lang.ratio);
    ratio.setLocation(10, 115);
    ratio.setSize(70, 20);
    ratio.setFont(ratio.getFont().deriveFont(Font.BOLD));
   
    JLabel rate = new JLabel(lang.rate);
    rate.setLocation(10, 140);
    rate.setSize(70, 20);
    rate.setFont(rate.getFont().deriveFont(Font.BOLD));
   
    JLabel aFmt = new JLabel(lang.format);
    aFmt.setLocation(10, 15);
    aFmt.setSize(70, 20);
    aFmt.setFont(aFmt.getFont().deriveFont(Font.BOLD));
   
    JLabel freq = new JLabel(lang.frequence);
    freq.setLocation(10, 65);
    freq.setSize(70, 20);
    freq.setFont(freq.getFont().deriveFont(Font.BOLD));
   
    JLabel channel = new JLabel(lang.channels);
    channel.setLocation(10, 90);
    channel.setSize(70, 20);
    channel.setFont(channel.getFont().deriveFont(Font.BOLD));
   
    JLabel aRate = new JLabel(lang.rate);
    aRate.setLocation(10, 115);
    aRate.setSize(70, 20);
    aRate.setFont(aRate.getFont().deriveFont(Font.BOLD));
   
    convert = new JButton(lang.convert);
    convert.setSize(405, 40);
    convert.setLocation(5, 455);
    convert.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        if(converting)
        {
          runner.interrupt();
          return;
        }
       
        if(inputFile.getText().isEmpty())
          JOptionPane.showMessageDialog(self, lang.noinput, lang.convertion, JOptionPane.WARNING_MESSAGE);
        else if (outputFile.getText().isEmpty())
          JOptionPane.showMessageDialog(self, lang.noutput, lang.convertion, JOptionPane.WARNING_MESSAGE);
        else if (outputhz.getText().isEmpty() && outType.getSelectedIndex() == 3)
          JOptionPane.showMessageDialog(self, lang.nofps, lang.convertion, JOptionPane.WARNING_MESSAGE);
        else if (outputbit.getText().isEmpty() && outType.getSelectedIndex() == 3)
          JOptionPane.showMessageDialog(self, lang.novbits, lang.convertion, JOptionPane.WARNING_MESSAGE);
        else if (outputARate.getText().isEmpty() && outputAFmt.getSelectedIndex() != 0 && outType.getSelectedIndex() == 3)
          JOptionPane.showMessageDialog(self, lang.noabits, lang.convertion, JOptionPane.WARNING_MESSAGE);
        else if (ffmpeg == null)
          JOptionPane.showMessageDialog(self, lang.exenotfound, lang.error, JOptionPane.ERROR_MESSAGE);
        else
        {
          ffmpeg.setOutput(outputFile.getText(), (String) outputAFmt.getSelectedItem(), (String) outputFmt.getSelectedItem(), (String) outputratio.getSelectedItem(), outputscala1.getText() + "x" + outputscala2.getText(), outputbit.getText(), outputARate.getText(), outType.getSelectedIndex(), outputhz.getText(), String.valueOf(outputAChannels.getSelectedIndex()));
          selInputFile.setEnabled(false);
          selOutputFile.setEnabled(false);
          convert.setText(lang.stopconvertion);
          converting = true;
         
          try
          {
            runner = new Thread(ffmpeg);
            runner.start();
          }
          catch(Exception exc)
          {
            conversionStopped();
            JOptionPane.showMessageDialog(self, lang.convertionstopped, lang.convertion, JOptionPane.ERROR_MESSAGE);
            if(Constants.debug)
              exc.printStackTrace();
          }
        }
      }
    });
   
    openInet.addActionListener(new ActionListener()
    { 
      @Override
      public void actionPerformed(ActionEvent e)
      {
        InternetVideo inet = new InternetVideo(self, lang);
        String uri = inet.getURI();
       
        if(uri != null)
        {
          inputFile.setText(uri);
          KeyEvent k = new KeyEvent(self, KeyEvent.KEY_RELEASED, 0, 0, 0, 'y');
          Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(k);
          try
          {
            Robot r = new Robot();
            inputFile.requestFocus();
            r.keyPress(KeyEvent.VK_ENTER);
            r.keyRelease(KeyEvent.VK_ENTER);
          }
          catch(Exception exc)
          {
            if(Constants.debug)
              System.out.println(exc.getMessage());
          }
        }
      }
    });
   
    savePreset.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        if(prname.getText().isEmpty())
          JOptionPane.showMessageDialog(self, lang.nopresetname, lang.error, JOptionPane.ERROR_MESSAGE);
        else
        {
          String pre = prname.getText() + "|" + outputAFmt.getSelectedIndex() + "|" + outputFmt.getSelectedIndex() + "|" + outputratio.getSelectedIndex() + "|" + outputscala1.getText() + "|" + outputscala2.getText() + "|" + outputbit.getText() + "|" + outputARate.getText() + "|" + outType.getSelectedIndex() + "|" + outputhz.getText() + "|" + outputAChannels.getSelectedIndex();
          new File(System.getProperty("user.home") + "/.multiconvert/").mkdir();
          BufferedWriter writer;
         
          int found = -1;
          for(int i = 0; i < presets.size(); i++)
          {
            if(presets.get(i).split("\\|")[0].equals(prname.getText()))
            {
              found = i;
              presets.set(i, pre);
            }
          }
       
          try
          {
            if(found == -1)
              presets.add(pre);
           
            new File(System.getProperty("user.home") + "/.multiconvert/").mkdir();
             
            String langstr = (getLanguage() instanceof English) ? "1" : ((getLanguage() instanceof Spanish) ? "2" : "0");
             
            writer = new BufferedWriter(new FileWriter(new File(System.getProperty("user.home") + "/.multiconvert/" +"settings.ini"), false));
            writer.write("path=" + getPath() + "\n");
            writer.write("lang=" + langstr + "\n");
            for(int i = 0; i < presets.size(); i++)
            {
              writer.write("preset=" + presets.get(i) + "\n");
            }
            writer.close();   
           
            String[] types = new String[4 + presets.size()];
            types[0] = "VCD";
            types[1] = "SVCD";
            types[2] = "DVD";
            types[3] = lang.userdefined;
           
            for(int i = 4; i < types.length; i++)
            {
              String p = presets.get(i - 4);
              String[] line = p.split("\\|");
              types[i] = line[0];
            }
           
            outType.setModel(new DefaultComboBoxModel(types));
           
            if(found == -1)
              outType.setSelectedIndex(4 + presets.size() - 1);
            else
              outType.setSelectedIndex(4 + found);
          }
          catch(Exception exc)
          {
            JOptionPane.showMessageDialog(self, lang.confignotsaved, lang.error, JOptionPane.ERROR_MESSAGE);
         
            if(Constants.debug)
              exc.printStackTrace();
         
            return;
          }
           
        JOptionPane.showMessageDialog(self, lang.presetsaved, lang.configsuccess, JOptionPane.INFORMATION_MESSAGE);
        }
      }
    });
   
    outType.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent arg0)
      {
        if(outType.getSelectedIndex() >= 3)
        {
          prname.setText("");
          prname.setEnabled(true);
          if(outType.getSelectedIndex() > 3)
          {
            String preset = presets.get(outType.getSelectedIndex() - 4);
            String[] vars = preset.split("\\|");
            outputAFmt.setSelectedIndex(Integer.parseInt(vars[1]));
            outputFmt.setSelectedIndex(Integer.parseInt(vars[2]));
            outputratio.setSelectedIndex(Integer.parseInt(vars[3]));
            outputscala1.setText(String.valueOf(util.ConvertUtils.getWidthFromScala(inputScala.getText())));
            outputscala2.setText(String.valueOf(util.ConvertUtils.getHeightFromScala(inputScala.getText())));
            oratiosw.setSelectedIndex(0);
            outputbit.setText(vars[6]);
            outputARate.setText(vars[7]);
            //outType.setSelectedIndex(Integer.parseInt(vars[8]));
            outputhz.setText(vars[9]);
            prname.setText(outType.getSelectedItem().toString());
            prname.setEnabled(false);
            if(vars.length > 10)
              outputAChannels.setSelectedIndex(Integer.parseInt(vars[10]));
            else
              outputAChannels.setSelectedIndex(0);
          }
         
          outputVideo.setVisible(true);
          outputAudio.setVisible(true);
          preset.setVisible(true);
          prname.setVisible(true);
          savePreset.setVisible(true);
        }
        else
        {
          outputVideo.setVisible(false);
          outputAudio.setVisible(false);
          preset.setVisible(false);
          savePreset.setVisible(false);
          prname.setVisible(false);
        }
      }
    });
   
    doFPS.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        if(doFPS.isSelected())
        {
          ofps.setEnabled(true);
          outputhz.setEnabled(true);
        }
        else
        {
          ofps.setEnabled(false);
          outputhz.setEnabled(false);
          outputhz.setText("0");
        }
      }
    });
   
    inputMain.add(duration);
    inputMain.add(mrate);
    inputMain.add(inputDuration);
    inputMain.add(inputMRate);
   
    inputVideo.add(fmt);
    inputVideo.add(inputFmt);
    inputVideo.add(inputFmtSpec);
    inputVideo.add(colorSpace);
    inputVideo.add(scala);
    inputVideo.add(ratio);
    inputVideo.add(rate);
    inputVideo.add(inputColorSpace);
    inputVideo.add(inputScala);
    inputVideo.add(inputRatio);
    inputVideo.add(inputRate);
   
    inputAudio.add(aFmt);
    inputAudio.add(freq);
    inputAudio.add(channel);
    inputAudio.add(aRate);
    inputAudio.add(inputAFmt);
    inputAudio.add(inputFreq);
    inputAudio.add(inputChannel);
    inputAudio.add(inputARate);
    inputAudio.add(inputAFmtSpec);
   
    outputVideo.add(ofmt);
    outputVideo.add(outputFmt);
    outputVideo.add(outputFmtSpec);
    outputVideo.add(oratio);
    outputVideo.add(oscala);
    outputVideo.add(orate);
    outputVideo.add(outputscala1);
    outputVideo.add(cross);
    outputVideo.add(outputscala2);
    outputVideo.add(outputratio);
    outputVideo.add(outputbit);
    outputVideo.add(outputbits);
    outputVideo.add(oratiosw);
    outputVideo.add(outputhz);
    outputVideo.add(outputhzs);
    outputVideo.add(ofps);
    outputVideo.add(doFPS);
   
   
    outputAudio.add(oaFmt);
    outputAudio.add(oaRate);
    outputAudio.add(oaChannels);
    outputAudio.add(outputAFmt);
    outputAudio.add(outputAFmtSpec);
    outputAudio.add(outputARate);
    outputAudio.add(outputAChannels);
    outputAudio.add(outputabits);
   
    input.add(file);
    input.add(inputFile);
    input.add(selInputFile);
    input.add(inputMain);
    input.add(inputVideo);
    input.add(inputAudio);
   
    output.add(oFile);
    output.add(outputFile);
    output.add(selOutputFile);
    output.add(out);
    output.add(outType);
    output.add(outputVideo);
    output.add(outputAudio);
    output.add(savePreset);
    output.add(prname);
    output.add(preset);
   
    setIconImage(new ImageIcon("images/icon.png").getImage());
    setLayout(null);
    setSize(420,570);
    setResizable(false);
    setLocation(SwingUtils.getCenterPositon(getSize()));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
   
    add(input);
    add(output);
    add(convert);
    add(progress);
     
    setVisible(true);
  }

  @Override
  public void setProgress(int p)
  {
    if(p == 105)
    {
      conversionStopped();
      JOptionPane.showMessageDialog(this, lang.convertionfailed, lang.convertion, JOptionPane.ERROR_MESSAGE);
    }
    else if(p == 104)
    {
      progress.setValue(0);
      setTitle("Multiconvert");
    }
    else if(p == 103)
    {
      conversionStopped();
     
      JOptionPane.showMessageDialog(this, lang.convertionfailed, lang.convertion, JOptionPane.ERROR_MESSAGE);
    }
    else if(p == 102)
    {
      conversionStopped();
     
      JOptionPane.showMessageDialog(this, lang.convertionstopped, lang.convertion, JOptionPane.INFORMATION_MESSAGE);
    }
    else if(p == 101)
    {
      conversionStopped();
     
      if(JOptionPane.showConfirmDialog(this, lang.convertionsuccess, lang.convertion, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.OK_OPTION)
        SwingUtils.openFile(this, outputFile.getText(), lang);
       
    }
    else
    {
      progress.setValue(p);
      setTitle("Multiconvert " + p + "%");
    }
  }
 
  /**
   * set some parameters when conversion stopped
   */
  private void conversionStopped()
  {
    selInputFile.setEnabled(true);
    selOutputFile.setEnabled(true);
    progress.setValue(0);
    setTitle("Multiconvert");
    convert.setText(lang.convert);
    converting = false;
  }
 
  /**
   * set a new ffmpeg path
   * @param path
   *         Path to ffmpeg
   * @throws IOException
   *         when ffmpeg was not found
   */
  public void setPath(String path) throws IOException
  {
    if(ffmpeg != null)
      ffmpeg.setPath(path);
    else
      ffmpeg = new FFMpeg(path, lang, this);
  }
 
  /**
   * Get the ffmpeg path
   * @return ffmpeg path
   */
  public String getPath()
  {
    if(ffmpeg != null)
      return ffmpeg.getPath();
    else
      return "";
  }
 
  /**
   * Get the language
   * @return lang
   */
  public Language getLanguage()
  {
    return lang;
  }
 
  /**
   * Get JButton selInputFile
   * @return selInputFile
   */
  public JButton getSelInputFile()
  {
    return selInputFile;
  }
}
TOP

Related Classes of gui.FFMpegGui

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.