From 44d7a9bfdd33c3e0506357ddd0f41a5484f137a2 Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Thu, 12 May 2022 20:53:26 +0200 Subject: [PATCH] Add the library Signed-off-by: Sam Therapy --- .drone.yml | 15 +++++ .gitignore | 137 ++++++++++++++++++++++++++++++++++++++++++ .vscode/settings.json | 5 ++ LICENSE | 3 +- README.md | 8 ++- mod.ts | 94 +++++++++++++++++++++++++++++ scripts/npm-build.ts | 31 ++++++++++ test.ts | 100 ++++++++++++++++++++++++++++++ 8 files changed, 391 insertions(+), 2 deletions(-) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 mod.ts create mode 100644 scripts/npm-build.ts create mode 100644 test.ts diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..56976f4 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,15 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: lint + image: denoland/deno:alpine + commands: + - deno fmt --check + - deno lint + + - name: test + image: denoland/deno:alpine + commands: + - deno test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6fb83cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,137 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.pnp.* +.yarn/* +!.yarn/patches +!.yarn/plugins +!.yarn/releases +!.yarn/sdks +!.yarn/versions + +.dccache + +# npm output +npm/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e40716f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "deno.enable": true, + "deno.lint": true, + "deno.unstable": true +} diff --git a/LICENSE b/LICENSE index 2071b23..481853b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ MIT License -Copyright (c) +Copyright (c) 2022 Sam Thearpy +Copyright (c) 2014 Silvia Pfeiffer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 54f523e..ef45324 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ -# srttovtt +# SRT-to-VTT +## A basic library to convert from srt to vtt + +This code is taken from the excellent [webvtt.org](https://www.webvtt.org/), I +merely adapted it to work as a library. + +## Usage diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..ba9df88 --- /dev/null +++ b/mod.ts @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: MIT + +/** + * @param {string} data SRT text to be converted + * @returns {string} VTT text + */ +export default function srt2webvtt(data: string): string { + // remove dos newlines + let srt = data.replace(/\r+/g, ""); + // trim white space start and end + srt = srt.replace(/^\s+|\s+$/g, ""); + + // get cues + const cuelist = srt.split("\n\n"); + let result = ""; + + if (cuelist.length > 0) { + result += "WEBVTT\n\n"; + for (let i = 0; i < cuelist.length; i = i + 1) { + result += convertSrtCue(cuelist[i]); + } + } + + return result; +} + +/** + * + * @param {string} caption SRT section for parsing + * @returns {string} translated VTT section + */ +function convertSrtCue(caption: string): string { + // remove all html tags for security reasons + //srt = srt.replace(/<[a-zA-Z\/][^>]*>/g, ''); + + let cue = ""; + const s = caption.split(/\n/); + + // concatenate muilt-line string separated in array into one + while (s.length > 3) { + for (let i = 3; i < s.length; i++) { + s[2] += "\n" + s[i]; + } + s.splice(3, s.length - 3); + } + + let line = 0; + + // detect identifier + if (!s[0].match(/\d+:\d+:\d+/) && s[1].match(/\d+:\d+:\d+/)) { + cue += s[0].match(/\w+/) + "\n"; + line += 1; + } + + // get time strings + if (s[line].match(/\d+:\d+:\d+/)) { + // convert time string + const m = s[1].match( + /(\d+):(\d+):(\d+)(?:,(\d+))?\s*--?>\s*(\d+):(\d+):(\d+)(?:,(\d+))?/, + ); + if (m) { + cue += m[1] + + ":" + + m[2] + + ":" + + m[3] + + "." + + m[4] + + " --> " + + m[5] + + ":" + + m[6] + + ":" + + m[7] + + "." + + m[8] + + "\n"; + line += 1; + } else { + // Unrecognized timestring + return ""; + } + } else { + // file format error or comment lines + return ""; + } + + // get cue text + if (s[line]) { + cue += s[line] + "\n\n"; + } + + return cue; +} diff --git a/scripts/npm-build.ts b/scripts/npm-build.ts new file mode 100644 index 0000000..9d084ad --- /dev/null +++ b/scripts/npm-build.ts @@ -0,0 +1,31 @@ +// ex. scripts/build_npm.ts +import { build, emptyDir } from "https://deno.land/x/dnt/mod.ts"; + +await emptyDir("./npm"); + +await build({ + entryPoints: ["./mod.ts"], + outDir: "./npm", + shims: { + // see JS docs for overview and more options + deno: true, + }, + package: { + // package.json properties + name: "srt-to-vtt", + version: Deno.args[0], + description: "A library that converts SRT files to VTT equivalents so they can be broadcast on the Internet.", + license: "MIT", + repository: { + type: "git", + url: "git+https://git.froth.zone/sam/srt-to-vtt", + }, + bugs: { + url: "https://git.froth.zone/sam/srt-to-vtt/issues", + }, + }, +}); + +// post build steps +Deno.copyFileSync("LICENSE", "npm/LICENSE"); +Deno.copyFileSync("README.md", "npm/README.md"); \ No newline at end of file diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..89d735c --- /dev/null +++ b/test.ts @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: MIT +import convert from "./mod.ts"; +import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; + +Deno.test("convert, 1 sub", () => { + const srt = `1 +00:00:00,000 --> 00:00:00,000 +Hello, world! +`; + const expected = `WEBVTT + +1 +00:00:00.000 --> 00:00:00.000 +Hello, world! + +`; + assertEquals(convert(srt), expected); +}); + +Deno.test("convert, multiple subs", () => { + const srt = `1 +00:02:09,545 --> 00:02:12,757 +rare roasted partridge breast +in raspberry coulis +with a sorrel timbale. + +2 +00:02:13,007 --> 00:02:16,093 +...and grilled +free-range rabbit +with herbed french fries. + +3 +00:02:16,302 --> 00:02:20,014 +Our pasta tonight is +a squid ravioli +in a lemon grass broth. + +4 +00:02:24,602 --> 00:02:28,731 +God, I hate this place. +It's a chick's restaurant. +Why aren't we at Dorsia? + +5 +00:02:28,898 --> 00:02:30,816 +Because Bateman won't give +the maitre d' head. +`; + + const expected = `WEBVTT + +1 +00:02:09.545 --> 00:02:12.757 +rare roasted partridge breast +in raspberry coulis +with a sorrel timbale. + +2 +00:02:13.007 --> 00:02:16.093 +...and grilled +free-range rabbit +with herbed french fries. + +3 +00:02:16.302 --> 00:02:20.014 +Our pasta tonight is +a squid ravioli +in a lemon grass broth. + +4 +00:02:24.602 --> 00:02:28.731 +God, I hate this place. +It's a chick's restaurant. +Why aren't we at Dorsia? + +5 +00:02:28.898 --> 00:02:30.816 +Because Bateman won't give +the maitre d' head. + +`; + assertEquals(convert(srt), expected); +}); + +Deno.test("Invalid time string", () => { + const srt = `1 +00:00:00,000,0 --> 00:00:00,000 +Hello, world! +`; + assertEquals(convert(srt), "WEBVTT\n\n"); +}); + +Deno.test("Invalid file", () => { + const srt = `1 +// This is a comment +Hello, world! +`; + assertEquals(convert(srt), "WEBVTT\n\n"); +});