加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Jmidilame

发布时间:2020-12-14 04:17:12 所属栏目:大数据 来源:网络整理
导读:今天终于有时间写博客了,这个Jmidilame是我基于上次的JTimidity (https://www.cnblogs.com/debianroot/p/9613986) 升级而来的。这个版本抛弃了timidity后端(因为它貌似有近十年没更新了)我把它替换成了fluidsynth. 我还修改了fluidsynth和ffmpeg的参数让

今天终于有时间写博客了,这个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();
    }
});
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读