码迷,mamicode.com
首页 > 其他好文 > 详细

JTimidity

时间:2018-09-09 18:02:46      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:etl   otp   textarea   grid   action   main   man   tab   public   

这是一个用java swing作为图形界面,timidity和ffmpeg作为后端的mid转mp3的转换工具,不过转出来的mp3音质并不是太好,只有128kbps的码率。
不知怎么办才能提高码率?如果有哪位知道如何提高码率的话,欢迎在评论区告知。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

class Main {

public static void main(final String[] args) {
// TODO 自动生成的方法存根
final GridLayout GL = new GridLayout();
final BorderLayout BL = new BorderLayout();
final JFrame JF = new JFrame("JTimidity");
final JButton JB = new JButton("开始");
final JButton JB2 = new JButton("浏览");
final JButton JB3 = new JButton("浏览");
final JScrollPane JSP = new JScrollPane();
final JTextArea JTA = new JTextArea();
JTA.setEditable(false);
JTA.setLineWrap(true);
JSP.setViewportView(JTA);
final JLabel JL = new JLabel("待转换的midi文件:");
final JTextField JTF = new JTextField();
JTF.setEditable(false);
final JLabel JL2 = new JLabel("转换后的mp3保存路径:");
final JTextField JTF2 = new JTextField();
JTF2.setEditable(false);
final JPanel JP = new JPanel();
final JPanel JP2 = new JPanel();
final JPanel JP3 = new JPanel();
JF.setUndecorated(true);
JF.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
JF.setSize(1050, 1050);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setLocationRelativeTo(null);
JF.setLayout(BL);
JP.setLayout(GL);
JP2.setLayout(GL);
JP3.setLayout(GL);
JF.add(JP, BorderLayout.NORTH);
JF.add(JP2, BorderLayout.CENTER);
JF.add(JP3, BorderLayout.SOUTH);
JP.add(JL);
JP.add(JTF);
JP.add(JB2);
JP2.add(JSP);
JP2.add(JB);
JP3.add(JL2);
JP3.add(JTF2);
JP3.add(JB3);
JF.setVisible(true);
JB2.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(final ActionEvent arg0) {
    // TODO 自动生成的方法存根
    final JFileChooser JFC = new JFileChooser();
    JFC.setAcceptAllFileFilterUsed(false);
    JFC.setMultiSelectionEnabled(false);
    final JFrame JF2 = new JFrame(JF.getTitle() + " - " + "选择midi文件");
    JF2.setUndecorated(true);
    JF2.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
    JF2.setSize(575, 575);
    JF2.setLocationRelativeTo(null);
    JF2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    JF2.add(JFC);
    JF2.setVisible(true);
    JFC.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent arg0) {
        // TODO 自动生成的方法存根
        if (arg0.getActionCommand() == JFileChooser.CANCEL_SELECTION) {
            JF2.dispose();
        }
        if (arg0.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
            JF2.dispose();
            final File F = JFC.getSelectedFile();
            JTF.setText(F.getAbsolutePath());
        }
        }
    });
    }

});
JB3.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(final ActionEvent arg0) {
    // TODO 自动生成的方法存根
    final JFileChooser JFC2 = new JFileChooser();
    JFC2.setAcceptAllFileFilterUsed(false);
    JFC2.setMultiSelectionEnabled(false);
    JFC2.setDialogType(JFileChooser.SAVE_DIALOG);
    final JFrame JF3 = new JFrame(JF.getTitle() + " - " + "选择mp3文件的保存位置");
    JF3.setUndecorated(true);
    JF3.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
    JF3.setSize(575, 575);
    JF3.setLocationRelativeTo(null);
    JF3.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    JF3.add(JFC2);
    JF3.setVisible(true);
    JFC2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent arg0) {
        // TODO 自动生成的方法存根
        if (arg0.getActionCommand() == JFileChooser.CANCEL_SELECTION) {
            JF3.dispose();
        }
        if (arg0.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
            JF3.dispose();
            final File F2 = JFC2.getSelectedFile();
            JTF2.setText(F2.getAbsolutePath());
        }
        }
    });
    }
});
JB.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(final ActionEvent arg0) {
    // TODO 自动生成的方法存根
    new Thread(new Runnable() {

        @Override
        public void run() {
        // TODO 自动生成的方法存根
        try {
            JB.setEnabled(false);
            final Process P = Runtime.getRuntime()
                .exec("timidity -o /root/output.wav -Ow " + JTF.getText());
            final InputStreamReader ISR = new InputStreamReader(P.getInputStream());
            final BufferedReader BR = new BufferedReader(ISR);
            String s;
            while ((s = BR.readLine()) != null) {
            JTA.append(s += ‘\n‘);
            JTA.setCaretPosition(JTA.getDocument().getLength());
            }
            final Process P2 = Runtime.getRuntime().exec(
                "ffmpeg -stats -vol 256 -y -i /root/output.wav -f mp3 -codec libshine -ac 2 -ar 48000 "
                    + JTF2.getText());
            final InputStreamReader ISR2 = new InputStreamReader(P2.getInputStream());
            final BufferedReader BR2 = new BufferedReader(ISR2);
            String s2;
            while ((s2 = BR2.readLine()) != null) {
            JTA.append(s2 += ‘\n‘);
            JTA.setCaretPosition(JTA.getDocument().getLength());
            }
            Runtime.getRuntime().exec("rm -f /root/output.wav");
            JB.setEnabled(true);
        } catch (final IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        }

    }).start();
    }

});
}

}

JTimidity

标签:etl   otp   textarea   grid   action   main   man   tab   public   

原文地址:https://www.cnblogs.com/debianroot/p/9613986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!