初学者学的,,高手别见笑..
期待V2.0~~~~
用SWING来做咯.package com.subtitle;import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
/**
 * 写本代码动机:本人买了个iStation u43 能看avi格式的,带字幕的AVI放不了,只能支持smi格式的字幕文件
 * 前两天要看Prison Break,没字幕,所以就想要写这个程序来Convert一下了.
 * 功能说明 :从srt字幕文件转换到 smi字幕格式的文件
 * @author jongsuny
 * @version 1.0
 */
public class Convert {
public static void main(String[] args) throws Exception {
FileInputStream is = new FileInputStream("c:\\sub.txt");//srt字幕路径
System.setIn(is);//direct stdin to file PrintStream os=new PrintStream(new File("c:\\subout.txt"));//smi字幕路径
Scanner sc=new Scanner(System.in);

System.setOut(os);//direct stdout to file

String head="<SAMI> <HEAD><STYLE TYPE=\"text/css\"></STYLE></HEAD>  <BODY>  ";
String boot="</BODY> \n </SAMI> ";
String start="<SYNC Start=";
String endStart=">";
String body="<P Class=ZHCC>";
System.out.println(head);
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="";
System.out.println(start+re+stt[1]+endStart+body);
continue;
}

System.out.println(str);
System.out.println();
}
System.out.println(boot); }
/**
 * 
 * @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;
}
}}

解决方案 »

  1.   

    今天写了一个晚上,,升级版本,,可以直接选择文件...
    接下来要通用的了...补充类型选择...这个要过一段时间了,现在太忙了.
    期待ING...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.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();
    int state = filec.showSaveDialog(frame);
    if (state == JFileChooser.APPROVE_OPTION) {
    to = filec.getSelectedFile();
    destPath.setText(to.getPath() + to.getName());
    }
    } 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> \n </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 + "\n");
    // 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(); }}