• 一个修改电影字幕时间偏移的小程序


    今天晚上看电影的时候在射手网上下载了一个字幕文件,看了一会儿就发觉字幕有点跟不上电影,差了两三秒钟吧,我想这应该是大家都遇到过的问题。怎么办呢,既然咱是程序员,那就动动手,发挥一下,于是花了20分钟写了这么个小程序,很简陋,但是记录下来,一方面为了做个记录,另一方面也为了能够帮到别人,如果你也有同样的需求,希望能节省一点你的时间吧

    附上jar文件,使用方法 java -jar SrtTimelineChanger 字幕文件路径 调节的时间(以毫秒计算) 如: java -jar SrtTimelineChanger 1.srt -3000

    只适用于.srt文件,或是时间方式为 HH:MM:SS,nnn ---> HH:MM:SS,nnn的字幕文件,编码为gbk

    代码如下:

      1 /*
      2  * Copyright 2013
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.yueang.tools;
     18 
     19 import java.io.BufferedReader;
     20 import java.io.BufferedWriter;
     21 import java.io.File;
     22 import java.io.FileInputStream;
     23 import java.io.FileNotFoundException;
     24 import java.io.FileOutputStream;
     25 import java.io.IOException;
     26 import java.io.InputStreamReader;
     27 import java.io.OutputStreamWriter;
     28 import java.io.UnsupportedEncodingException;
     29 
     30 public class SrtTimelineChanger {
     31 
     32     public static void main(String[] args) {
     33 
     34         if (args.length != 2) {
     35             System.out.print("params wrong");
     36             return;
     37         }
     38 
     39         String filePath = args[0];
     40         int timeShift = Integer.parseInt(args[1]);
     41 
     42         SrtTimelineChanger changer = new SrtTimelineChanger();
     43         try {
     44             changer.change(filePath, timeShift);
     45         } catch (UnsupportedEncodingException e) {
     46             // TODO Auto-generated catch block
     47             e.printStackTrace();
     48         } catch (FileNotFoundException e) {
     49             // TODO Auto-generated catch block
     50             e.printStackTrace();
     51         } catch (IOException e) {
     52             // TODO Auto-generated catch block
     53             e.printStackTrace();
     54         }
     55 
     56         System.out.print("Exit");
     57     }
     58 
     59     private void change(String filePath, int timeShift) throws IOException {
     60         File file = new File(filePath);
     61 
     62         StringBuilder sb = new StringBuilder();
     63         BufferedReader reader = new BufferedReader(new InputStreamReader(
     64                 new FileInputStream(file), "gbk"));
     65         String line = reader.readLine();
     66         while (line != null) {
     67             sb.append(changeLine(line, timeShift));
     68             sb.append("\r\n");
     69             line = reader.readLine();
     70         }
     71 
     72         reader.close();
     73         File outFile = new File(filePath + ".out");
     74         if (outFile.exists()) {
     75             outFile.delete();
     76         }
     77         outFile.createNewFile();
     78         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
     79                 new FileOutputStream(outFile), "gbk"));
     80         writer.write(sb.toString());
     81         writer.flush();
     82         writer.close();
     83     }
     84 
     85     private Object changeLine(String line, int timeShift) {
     86         if (line.matches("^\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d.*\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d$")) {
     87             int start = calculate(line.substring(0, 12));
     88             int end = calculate(line.substring(17, 29));
     89 
     90             StringBuilder sb = new StringBuilder();
     91             sb.append(expand(start + timeShift));
     92             sb.append(" --> ");
     93             sb.append(expand(end + timeShift));
     94 
     95             System.out.println(line);
     96             System.out.println(sb.toString());
     97 
     98             return sb.toString();
     99         }
    100 
    101         return line;
    102     }
    103 
    104     private String expand(long l) {
    105         int hour = (int) (l / 3600000);
    106         int min = (int) ((l - hour * 3600000) / 60000);
    107         int sec = (int) ((l - hour * 3600000 - min * 60000) / 1000);
    108         int mili = (int) (l - hour * 3600000 - min * 60000 - sec * 1000);
    109 
    110         return String.format("%02d:%02d:%02d,%03d", hour, min, sec, mili);
    111     }
    112 
    113     private int calculate(String time) {
    114         int hour = Integer.parseInt(time.substring(0, 2));
    115         int min = Integer.parseInt(time.substring(3, 5));
    116         int sec = Integer.parseInt(time.substring(6, 8));
    117         int mili = Integer.parseInt(time.substring(9, 12));
    118 
    119         return mili + sec * 1000 + min * 60000 + hour * 3600000;
    120     }
    121 }
  • 相关阅读:
    102/107. Binary Tree Level Order Traversal/II
    110. Balanced Binary Tree
    SQL SERVER select,update,delete使用表别名
    C# 有哪些集合
    C# 流总结
    How ASP.NET MVC Works ? (Artech)
    c#扩展方法
    使用缓存的九大误区
    9中浏览器端缓存
    理解AppDomain和AppPool
  • 原文地址:https://www.cnblogs.com/yueang/p/3113175.html
Copyright © 2020-2023  润新知