Code Monkey home page Code Monkey logo

jsrt's Introduction

jsrt

A small SRT parser and editor in Java. SRT is a subtitle file format.

Download

https://github.com/fredyw/jsrt/releases

How to Build

jsrt uses Gradle build system and requires Java 8 to build.

Building jsrt JAR

gradle

The JAR will be created in dist directory.

Running jsrt-gui

java -jar dist/jsrt-gui-<version>.jar

Screenshot

JSRT screenshot

Examples

in.srt

1
00:00:20,000 --> 00:00:24,400
Hello World. This is a long text.
Bye World. This is a short text.

2
00:00:24,600 --> 00:00:27,800
Foo Bar
Bar Foo

out1.srt

1
16:17:55,370 --> 16:17:55,370
Hello
World

2
16:17:55,370 --> 16:17:55,370
Bye
World

out2.srt

1
00:00:05,000 --> 00:00:07,000
Test

2
00:00:20,100 --> 00:00:24,500
Hello
World.
This is a
long text.
Bye World.
This is a
short
text.

3
00:00:24,600 --> 00:00:27,800
Foo Bar
Bar Foo

4
00:01:05,000 --> 00:01:07,000
Test

Main.java

package examples;

import java.io.File;
import java.util.Arrays;
import java.util.Date;

import org.fredy.jsrt.api.SRT;
import org.fredy.jsrt.api.SRTInfo;
import org.fredy.jsrt.api.SRTReader;
import org.fredy.jsrt.api.SRTTimeFormat;
import org.fredy.jsrt.api.SRTWriter;
import org.fredy.jsrt.editor.SRTEditor;

public class Main {
    private static void print(SRTInfo info) {
        for (SRT s : info) {
            System.out.println("Number: " + s.number);
            System.out.println("Start time: " + SRTTimeFormat.format(s.startTime));
            System.out.println("End time: " + SRTTimeFormat.format(s.endTime));
            System.out.println("Texts:");
            for (String line : s.text) {
                System.out.println("    " + line);
            }
            System.out.println();
        }
    }
    
    private static void testRead() {
        SRTInfo info = SRTReader.read(new File("in.srt"));
        print(info);
    }
    
    private static void testWrite() {
        SRTInfo info = new SRTInfo();
        Date d = new Date();
        info.add(new SRT(1, d, d, "Hello", "World"));
        info.add(new SRT(2, d, d, "Bye", "World"));
        
        File f = new File("out1.srt");
        f.deleteOnExit();
        SRTWriter.write(f, info);
    }
    
    private static void testEdit() {
        SRTInfo info = SRTReader.read(new File("in.srt"));
        SRTEditor.updateText(info, 1, 10);
        SRTEditor.updateTime(info, 1, SRTTimeFormat.Type.MILLISECOND, 100);
        SRTEditor.prependSubtitle(info, "00:00:05,000", "00:00:07,000",
            Arrays.asList("Test"));
        SRTEditor.appendSubtitle(info, "00:01:05,000", "00:01:07,000",
            Arrays.asList("Test"));
        
        print(info);
        
        // Write it back
        File f = new File("out2.srt");
        f.deleteOnExit();
        SRTWriter.write(f, info);
    }
    
    public static void main(String[] args) {
        testRead();
        testWrite();
        testEdit();
    }
}

jsrt's People

Contributors

fredyw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsrt's Issues

Support SRT with Byte Order Mark (BOM)

A lot of Vietnamese subs use UTF-8, some of them use BOM.

When read SRT file, SRTReader throws NumberFormatException. The reason is that the first line contains the BOM character. So i suggest that we replace the BOM before call Integer.parseInt

https://github.com/fredyw/jsrt/blob/master/jsrt-api/src/main/java/org/fredy/jsrt/api/SRTReader.java#L82

could be changed to

subtitleNumber = Integer.parseInt(nString.replace("\uFEFF", ""));

Or we could replace the BOM at the first line.

Thanks and best regards,

Multiple blank lines between two SRTs

In case we have multiple blank lines between two SRTs, SRTReader throws Invalid Subtitle Number.
So i suggest that BufferReader should read until nString is not empty.

https://github.com/fredyw/jsrt/blob/master/jsrt-api/src/main/java/org/fredy/jsrt/api/SRTReader.java#L75

while (nString != null && nString.isEmpty()) {
    nString = br.readLine();
}

Input

464
00:33:10,300 --> 00:33:12,270
Em phải đi London vào buổi chiều.


465
00:33:12,560 --> 00:33:13,560
Tốt.

466
00:33:16,700 --> 00:33:18,880
Tòa nhà bên cạnh đã sập vào đêm qua.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.