Code Monkey home page Code Monkey logo

webvtt's Introduction

Java WebVTT Library

🚧 Yet another Java WebVTT implementation. This is an open-source library designed for structured creation and parsing of WebVTT (Web Video Text Tracks) files using Java.

Features

  • Structured creation of WebVTT files.
  • Parse WebVTT files. (🚧 under construction)
  • Validate WebVTT syntax.

Getting Started

Prerequisites

To use this library, you will need: Java 17 or higher (Because I used two features: Record and Text Blocks)

Installing

You can add this library to your project as a dependency via Maven:

<dependency>
    <groupId>io.github.since1986</groupId>
    <artifactId>webvtt</artifactId>
    <version>1.0.0</version>
</dependency>

Usage

simple example

Here is a simple example of how to create a WebVTT:

var vtt = new VTT(
        new VTTHeader(null),
        new VTTBody(
                new VTTCue(
                        "1",
                        new CueTiming(new VTTRawTime(0L, 1L, 1L, 10L), new VTTRawTime(0L, 1L, 1L, 11L)),
                        List.of(
                                new NumberLine(1),
                                new PercentageLine(1),
                                new Position(1),
                                new Vertical(Vertical._Vertical.LR),
                                new Size(1)
                        ),
                        "test payload 1"
                ),
                new VTTComment("这是一个换行注释 > < & &gt; &lt; &amp;", true),
                new VTTCue(
                        "2",
                        new CueTiming(new VTTRawTime(0L, 1L, 1L, 11L), new VTTRawTime(9L, 1L, 1L, 20L)),
                        List.of(
                                new NumberLine(1),
                                new PercentageLine(1),
                                new Position(1),
                                new Vertical(Vertical._Vertical.LR),
                                new Size(1),
                                new Align(Align._Align.CENTER)
                        ),
                        "test payload 2"
                ),
                new VTTComment("这是一个单行的注释", false),
                new VTTComment("""
                        这是一个多
                        行注释
                        """, true)
        )
);

// System.out.println(vtt.to());

example of how to use VTTTime

Here is another example of how to use VTTTime. In this scenario, we aim to generate subtitles that print latitude and longitude information (which is read from a JSON file) every 200ms:

ClassLoader classLoader = this.getClass().getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("vtt-time-test.json")) {
    var objectMapper = new ObjectMapper();
    var inputList = objectMapper.readValue(inputStream, new TypeReference<List<Map<String, String>>>() {
    });
    // Each cue spans 200ms
    var cues = new ArrayList<VTTCue>();
    var start = VTTTime.zero();
    for (int i = 0; i < inputList.size(); i++) {
        var current = inputList.get(i);
        var end = start.plusMilliseconds(200L);
        var payload = "%s - %s,%s".formatted(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME), current.get("latitude"), current.get("longitude"));
        var cue = new VTTCue(
                String.valueOf(i),
                new CueTiming(start.to(), end.to()),
                List.of(
                        new PercentageLine(60),
                        new Size(20)
                ),
                payload
        );
        cues.add(cue);
        start = end;
    }
    var vtt = new VTT(
            new VTTHeader("测试每 200ms 打印一次字幕"),
            new VTTBody(cues.toArray(new VTTBodyItem[]{}))
    );
    // Files.writeString(Path.of("input.vtt"), vtt.to(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);

If you want to test the subtitles in VLC, you can follow these steps:

Create a 10-minute test video: Use the command to generate a test video with a duration of 10 minutes.

ffmpeg -f lavfi -i testsrc=duration=600:size=1280x720:rate=30 -f lavfi -i sine=frequency=1000:duration=600 -c:v libx264 -c:a aac -strict experimental input.mp4

Combine the WebVTT file generated from the example above with the video file to create an MKV file with soft subtitles: Execute the command.

ffmpeg -i input.mp4 -i input.vtt -c copy -c:s webvtt output.mkv

Play the resulting MKV file in VLC to check the effects.

vlc play

NOTE: The subtitles generated by my library in media players like VLC cannot correctly render the VTT subtitles cue settings, and I'm not sure if the problem is on my end or with VLC.


example of cue settings (must use WebM)

Create a 10-minute test video: Use the command:

ffmpeg -f lavfi -i testsrc=duration=600:size=1280x720:rate=30 -f lavfi -i sine=frequency=1000:duration=600 -c:v libvpx-vp9 -b:v 2M -c:a libopus -b:a 128k output.webm

Combine the WebVTT file generated from the example above with the video file

ffmpeg -i input.webm -i input.vtt -c copy -c:s webvtt output.webm

Contributing

Welcome contributions to this project. Please fork the repository and submit a pull request.

License

This project is licensed under the Apache License Version 2.0 License see the LICENSE file for details.

Acknowledgments

  • Thanks to all the contributors who have helped to improve this library.

Resources

Web Video Text Tracks Format (WebVTT)

Live WebVTT Validator

Can I use WebVTT - Web Video Text Tracks

WebVTT: The Web Video Text Tracks Format

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.