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

Jmidilame

时间:2018-10-27 13:20:33      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:document   accept   constant   def   share   linux命令行   jframe   cat   out   

今天终于有时间写博客了,这个Jmidilame是我基于上次的JTimidity (https://www.cnblogs.com/debianroot/p/9613986) 升级而来的。这个版本抛弃了timidity后端(因为它貌似有近十年没更新了)我把它替换成了fluidsynth.
我还修改了fluidsynth和ffmpeg的参数让其能够完整的显示转换过程。值得一提的是,fluidsynth和ffmpeg的转换过程进度信息都是输出到标准错误流(stderr)而不是标准输入流(stdin,大部分linux命令行程序使用该流来输出执行进度信息),这就导致我最初用java process类的getInputStream()方法没有获得任何的执行进度信息。直到我看到 ffmpeg和fluidsynth的man手册后,才知道有这么一回事。后来改用getErrorStream()方法,该问题得以解决。

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("Jmidilame");
final JButton JB = new JButton("开始");
JB.setEnabled(false);
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());
            if (JTF.getText() != null && JTF2.getText() != null) {
            JB.setEnabled(true);
            }
        }
        }
    });
    }
});
JB.addActionListener(new ActionListener() {

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

    new Thread(new Runnable() {

        @Override
        public void run() {
        // TODO 自动生成的方法存根
        JB.setEnabled(false);
        JB2.setEnabled(false);
        JB3.setEnabled(false);
        JF.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        JTA.setText(null);
        try {
            final Process P = Runtime.getRuntime().exec(
                "fluidsynth -v -F /root/output.flac -T flac /usr/share/sounds/sf2/FluidR3_GM.sf2 "  // "-v"= "--verbose",显示完整的执行进度。
                + JTF.getText());
            final InputStreamReader ISR = new InputStreamReader(P.getErrorStream());
            final BufferedReader BR = new BufferedReader(ISR);
            String s;
            while ((s = BR.readLine()) != null) {
            JTA.append(s += ‘\n‘);
            JTA.setCaretPosition(JTA.getDocument().getLength());
            }
            BR.close();
            ISR.close();
            final Process P2 = Runtime.getRuntime().exec(
                "ffmpeg -vol 448 -stats -loglevel level+verbose -y -i /root/output.flac -f mp3 -ab 320k -ac 2 -ar 48000 "  // ”-loglevel level+verbose -stats“这两个参数可以显示完整的转换进度。
                    + JTF2.getText());
            final InputStreamReader ISR2 = new InputStreamReader(P2.getErrorStream());
            final BufferedReader BR2 = new BufferedReader(ISR2);
            String s2;
            while ((s2 = BR2.readLine()) != null) {
            JTA.append(s2 += ‘\n‘);
            JTA.setCaretPosition(JTA.getDocument().getLength());
            }
            BR2.close();
            ISR2.close();
            JB.setEnabled(true);
            JB2.setEnabled(true);
            JB3.setEnabled(true);
            JF.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            Runtime.getRuntime().exec("rm -f /root/output.flac");
        } catch (final IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        }

    }).start();
    }
});
}

}

Jmidilame

标签:document   accept   constant   def   share   linux命令行   jframe   cat   out   

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

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