srt 字幕 转换为smi字幕的程序,JAVA写的。

用JAVA写的源代码,好象在CSDN上发过一次。。在自己的空间里发下。。

功能就是SRT转换为SMI格式。编译应该可以用。。

需要程序的请联系我,

我直接把程序发给你好了。不多说了。快关灯了。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
private static JFrame frame = new JFrame("字幕格式转换器");
private JLabel originFormat = new JLabel("源格式");
private JLabel destFormat = new JLabel("目标格式");
private JLabel originFile = new JLabel("源文件");
private JLabel destFile = new JLabel("目标文件");
private JButton open = new JButton("打开");
private JButton tofile = new JButton("目标");
private JButton start = new JButton("Start");
private JButton exit = new JButton("Exit");
private JTextField originPath = new JTextField(30);
private JTextField destPath = new JTextField(30);
private JComboBox originList = null;
private JComboBox destList = null;
private static JLabel message = new JLabel();
private ACL al = new ACL();
private static String[] of = new String[] { "sub", "src", "smi" };

public Main() {
   open.addActionListener(al);
   tofile.addActionListener(al);
   start.addActionListener(al);
   exit.addActionListener(al);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new BorderLayout());
   frame.setSize(new Dimension(350, 220));
   frame.setLocation(300, 150);
   frame.add(BorderLayout.NORTH, new Top());
   frame.add(BorderLayout.CENTER, new Body());
   frame.add(BorderLayout.WEST, new JLabel());
   frame.add(BorderLayout.EAST, new JLabel());
   frame.add(BorderLayout.SOUTH, new Foot());
   frame.setVisible(true);
   frame.setResizable(false);
   // frame.pack();

}

class Top extends JPanel {
   public Top() {
    setBorder(BorderFactory.createTitledBorder("选择类型"));

    this.setLayout(new GridLayout(1, 5));
    originList = new JComboBox(of);
    destList = new JComboBox(of);
    this.add(originFormat);
    this.add(originList);
    this.add(new JLabel("     —->>>>"));
    this.add(destFormat);
    this.add(destList);
   }
}

class Body extends JPanel {
   public Body() {
    setBorder(BorderFactory.createTitledBorder("选择文件"));

    this.setLayout(new GridLayout(2, 3));
    this.add(originFile);
    this.add(originPath);
    open.setMargin(new Insets(5, 5, 5, 5));

    this.add(open);
    this.add(destFile);
    this.add(destPath);
    tofile.setMargin(new Insets(5, 5, 5, 5));
    this.add(tofile);
   }
}

class Foot extends JPanel {
   public Foot() {
    setBorder(BorderFactory.createTitledBorder("选择动作"));
    this.setLayout(new FlowLayout());
    start.setMargin(new Insets(3, 10, 3, 10));
    this.add(start);
    exit.setMargin(new Insets(3, 10, 3, 10));
    this.add(exit);
    this.add(BorderLayout.SOUTH, message);
   }
}

class ACL implements ActionListener {
   private File from;
   private File to;

   @Override
   public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(exit)) {
     if (JOptionPane
       .showConfirmDialog(frame, "您确定要退出吗?", "退出",
         JOptionPane.YES_NO_OPTION,
         JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
      System.exit(0);
    } else if (e.getSource().equals(open)) {
     JFileChooser filec = new JFileChooser();

     FileNameExtensionFilter filter = new FileNameExtensionFilter(
       "选择字幕文件", "txt", "smi", "srt");
     filec.setFileFilter(filter);
     int state = filec.showOpenDialog(frame);
     if (state == JFileChooser.APPROVE_OPTION) {
      from = filec.getSelectedFile();
      originPath.setText(from.getPath() + from.getName());
     }
    } else if (e.getSource().equals(tofile)) {
     JFileChooser filec = new JFileChooser();
     FileNameExtensionFilter filter = new FileNameExtensionFilter(
       "创建字幕文件", "txt", "smi", "srt");
     filec.setFileFilter(filter);
     int state = filec.showSaveDialog(frame);
     if (state == JFileChooser.APPROVE_OPTION) {
      to = filec.getSelectedFile();
      String filename=to.getName();
      if(filename.split(".").length<=0){
       to=new File(to.getPath()+".smi");
      }
     
      destPath.setText(to.getPath());
     }
    } else if (e.getSource().equals(start)) {
     convert(from, to);
    }
   }

}

private static void convert(File from, File to) {
   System.out.println(from.getName() + " : " + to.getName());
   RandomAccessFile raf = null;
   try {
    raf = new RandomAccessFile(to, "rw");
   } catch (FileNotFoundException e) {
    message.setText(e.getMessage());
   }
   try {
    String head = "<SAMI> <HEAD><STYLE TYPE="text/css"></STYLE></HEAD> <BODY> ";
    String boot = "</BODY>
</SAMI> ";
    String start = "<SYNC Start=";
    String endStart = ">";
    String body = "<P Class=ZHCC>";
    raf.writeBytes(head);
    Scanner sc = new Scanner(from);
    while (sc.hasNext()) {
     String str = sc.nextLine();
     str = replaceLi(str);
     // System.out.println(str);

     if (str.length() < 5)
      continue;
     // else if(str.split("-").length>0){
     char c[] = str.toCharArray();
     if (c.length > 0 && c[0] == ’0′) {
      String st[] = str.split("-");
      String stt[] = st[0].split(",");
      String re = calculateTime(stt[0]);
      if (re.equals("0"))
       re = "";
      raf.writeBytes(start + re + stt[1] + endStart + body);
      // System.out.println(start+re+stt[1]+endStart+body);
      continue;
     }
     raf.writeBytes(str + "
");
     // System.out.println(str);
    }
    raf.writeBytes(boot);
   } catch (Exception e) {
    message.setText(e.getMessage());

   }
}

/**
*
* @param src
*            源字幕文件的内容,
* @return 把没用的符号去掉
*/
public static String replaceLi(String src) {
   src = src.replace("<i>", "");
   src = src.replace("</i>", "");
   src = src.replace("–>", "-");
   src = src.replace("</font>", "");
   src = src.replace("<font color="#ffff00">", "");

   return src;
}

/**
* 计算时间
*
* @param str
*            过滤后的内容
* @return 返回每一句话的时间点.
*/
public static String calculateTime(String str) {
   try {
    Integer sum = new Integer(0);
    String[] time = str.split(":");
    sum = (Integer.valueOf(time[0]) * 3600 + Integer.valueOf(time[1])
      * 60 + Integer.valueOf(time[2]));
    return String.valueOf(sum);
   } catch (Exception e) {
    return str;
   }
}

public static void main(String[] args) {
   new Main();

}

}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>