在java中将视频转换为mp3时出错

时间:2015-01-15 19:42:46

标签: java converter

我正在申请将任何视频文件转换为mp3,我选择要转换的文件,当我开始转换时,会显示此错误

Failed to instantiate [ch.qos.logback.classic.LoggerContext]

但是,我添加了外部jar文件:

的logback经典-0.9.jar 的logback核-0.9.jar slf4j.jar xuggle-xuggler.jar-3.0.660.jar

这是全班文件

package javaProject;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.xuggle.mediatool.IMediaReader; 
import com.xuggle.mediatool.IMediaWriter; 
import com.xuggle.mediatool.ToolFactory; 
import com.xuggle.xuggler.ICodec; 

public class Converter {

static Object a=null;

public static void main(String[] aa) 
{

    final JFrame mainframe=new JFrame("Video To Mp3 Converter");
    mainframe.setResizable(true);

    JPanel panel=new JPanel();
    panel.setLayout(null);
    panel.setBackground(Color.white);
    mainframe.add(panel);

    //debug.put("You chose to open this file: " + chooser.getSelectedFile().getAbsolutePath());


    /* ****************************** MENU BAR STARTS HERE ************************** */

    JMenuBar menu=new JMenuBar();
    mainframe.setJMenuBar(menu);

    // file menu starts

    JMenu file=new JMenu("File");
    file.addSeparator();
    menu.add(file);

    JMenuItem addfiles= new JMenuItem("Open");
    file.add(addfiles);

    JMenuItem save=new JMenuItem("Save");
    file.add(save);

    JMenuItem play=new JMenuItem("Play");
    file.add(play);

    JMenuItem pause=new JMenuItem("Pause");
    file.add(pause);

    JMenuItem exit= new JMenuItem("Exit");
    file.add(exit);

    // edit menu

    JMenu edit= new JMenu("Edit");
    edit.addSeparator();
    menu.add(edit);

    JMenuItem paste=new JMenuItem("Paste");
    edit.add(paste);


    JMenuItem remove=new JMenuItem("Remove");
    edit.add(remove);

    JMenuItem removeall=new JMenuItem("Remove All");
    edit.add(removeall);

    // convert menu

     JMenu convert=new JMenu ("Convert");
     menu.add(convert);

    // help menu

     JMenu help=new JMenu ("Help");
     help.addSeparator();
     menu.add(help);

     JMenuItem supportedformats=new JMenuItem("Supported Formats");
     help.add(supportedformats);

     JMenuItem version=new JMenuItem("Version");
     help.add(version);

    JMenuItem aboutus=new JMenuItem("About Us");
    help.add(aboutus);

    JMenuItem updates=new JMenuItem("Check For Updates");
    help.add(updates);


    /* ************************** MENU BAR ENDS HERE ***************** */


     /* ********************** MAIN BODY  ************************* */

    JLabel browsefiles=new JLabel("Browse Files"); 
    browsefiles.setBounds(50, 150, 150, 25);
    panel.add(browsefiles);

    final JTextField field1=new JTextField (300);
    field1.setBounds(150, 150, 300, 25);
    panel.add(field1);

    JButton browse=new JButton("Browse Files");
    browse.setBounds(470, 150, 150, 25);
    panel.add(browse);

    final JButton start=new JButton("Start Converting");
    start.setBounds(170, 200, 150, 25);
    panel.add(start);

    JButton pausebutton=new JButton("Pause");
    pausebutton.setBounds(340, 200, 100, 25);
    panel.add(pausebutton);

    browse.addActionListener(new ActionListener()                   
    {                   
        public void actionPerformed(ActionEvent arg0) 

        {   JFileChooser chooser=new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("VIDEO FILES", new 
            String[] {"mp4","FLV"});
            chooser.setFileFilter(filter);

             if (chooser.showOpenDialog(mainframe) == JFileChooser.APPROVE_OPTION) 
             {
                 field1.setText(chooser.getSelectedFile().getName());
             }
            // JOptionPane.showMessageDialog(null, "This Item Received from file chooser: "+ 
            chooser.getSelectedFile());

             start.addActionListener(new ActionListener()
             {
                 public void actionPerformed(ActionEvent arg0) 

                {

                     IMediaReader reader = ToolFactory.makeReader("chooser.getSelectedFile()"); 
                     IMediaWriter writer = ToolFactory.makeWriter("E:/output.mp3", reader); 
                     int sampleRate = 44100; 
                     int channels = 1; 
                     writer.addAudioStream(0, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate); 
                     while (reader.readPacket() == null); 

                }});
        }   
    });                                                             

    JButton playbutton=new JButton("PLAY");
    playbutton.setBounds(170, 230, 270, 25);
    panel.add(playbutton);

    /* ********************* MAIN BODY ENDS HERE ************* */

    /* **************************** MENU BAR ACTIONS HERE ******************* */


    addfiles.addActionListener(new ActionListener()     
    {                   
        public void actionPerformed(ActionEvent arg0) 

        {   JFileChooser chooser=new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("VIDEO FILES", new 
        String[] {"mp4"});
            chooser.setFileFilter(filter);

             if (chooser.showOpenDialog(mainframe) == JFileChooser.APPROVE_OPTION) {
                 field1.setText(chooser.getSelectedFile().getName());
             }
        }       
    });             

    exit.addActionListener(new ActionListener()         
    {                   
        public void actionPerformed(ActionEvent arg0) 

        {   
            System.exit(0);
        }       
    });                                                         

    remove.addActionListener(new ActionListener()               
    {                   
        public void actionPerformed(ActionEvent arg0) 

        {   
            if (field1==a)
            {
                JOptionPane.showMessageDialog(null, "Add Files First");
            }

            else 
            {
                field1.setText(null);   
            }


        }   
    });                                                             

    removeall.addActionListener(new ActionListener()                
    {                   
        public void actionPerformed(ActionEvent arg0) 

        {   
            if (field1==a)
            {
                JOptionPane.showMessageDialog(null, "Add Files First");
            }

            else 
            {
                field1.setText(null);   
            }
        }   
    });                                                             

    aboutus.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent arg0) 

        {
            AboutUs aboutusobj=new AboutUs();
            aboutusobj.aboutus();
        }
    });                                                                 

    updates.addActionListener(new ActionListener()                      
    {                   
        public void actionPerformed(ActionEvent arg0) 

        {
            Updates updatesobj=new Updates();
            updatesobj.checkforupdates();               
        }   
    });

    /* *************************** MENU BAR ACTIONS END HERE ********************** */

    mainframe.setSize(800, 500);
    mainframe.setLocationRelativeTo(null);
    mainframe.setVisible(true);

}

 }

这是我添加了actionlistener以将视频转换为mp3的应用程序部分

    browse.addActionListener(new ActionListener()               
    {                   
        public void actionPerformed(ActionEvent arg0) 

        {   JFileChooser chooser=new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("VIDEO FILES", new 
             String[] {"mp4","FLV"});
            chooser.setFileFilter(filter);

             if (chooser.showOpenDialog(mainframe) == JFileChooser.APPROVE_OPTION) 
             {
                 field1.setText(chooser.getSelectedFile().getName());
             }


             start.addActionListener(new ActionListener()
             {
                 public void actionPerformed(ActionEvent arg0) 

                {

                     IMediaReader reader = ToolFactory.makeReader("chooser.getSelectedFile()"); 
                     IMediaWriter writer = ToolFactory.makeWriter("E:/output.mp3", reader); 
                     int sampleRate = 44100; 
                     int channels = 1; 
                     writer.addAudioStream(0, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate); 
                     while (reader.readPacket() == null); 

                }});
        }   
    });     

1 个答案:

答案 0 :(得分:1)

Mp3是一个音频容器,而不是一个视频容器。您是否只是尝试从视频中提取音频并将其放入Mp3?