Skip to content

Commit

Permalink
Add TXT Download
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejMecka committed Aug 21, 2023
1 parent 642510d commit 770fdd3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import Languages from "./lib/pickers/Languages.svelte";
import SRT from "./lib/subtitle_conversions/SRT.svelte";
import { convert_to_srt } from "./lib/subtitle_conversions/SRT.svelte";
import { convert_to_txt } from "./lib/subtitle_conversions/TXT.svelte";
import {
convert_to_webvtt,
HREF,
} from "./lib/subtitle_conversions/WebVTT.svelte";
import WebVtt from "./lib/subtitle_conversions/WebVTT.svelte";
import TXT from "./lib/subtitle_conversions/TXT.svelte";
import Notifications from "./lib/components/Notifications.svelte";
import {getFinishedSeconds} from "./lib/get_seconds";
Expand Down Expand Up @@ -68,6 +70,7 @@
console.log(SUB_DATA);
convert_to_srt(SUB_DATA);
convert_to_webvtt(SUB_DATA);
convert_to_txt(SUB_DATA);
// Progress Stuff
PERCENTAGE_TRANSCRIBED_VIDEO = (getFinishedSeconds(SUB_DATA[SUB_DATA.length - 1]) / TOTAL_VIDEO_LENGTH) * 100;
Expand Down Expand Up @@ -159,6 +162,7 @@
</video>
<WebVtt />
<SRT />
<TXT />
{/if}
<Notifications bind:send_notification />
</main>
Expand Down
38 changes: 38 additions & 0 deletions src/lib/subtitle_conversions/TXT.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script lang="ts" context="module">
import { writable } from 'svelte/store';
export let HREF = writable("")
let FILENAME = writable("")
export const convert_to_txt = function(sub_array) {
/**
* Convert Whisper Output to WebVTT
* param sub_array: The Subtitles whisper returned
*/
let result = '';
sub_array.forEach((elem) => {
result += `${elem}\n`
})
download('download.txt', result)
return result
}
function download(filename, text) {
HREF.set(`data:text/plain;charset=utf-8,${encodeURIComponent(text)}`)
FILENAME.set(filename);
}
</script>

<script lang="ts">
let file_contents = ""
let filename = ""
$: $HREF, file_contents = $HREF
$: $FILENAME, filename = $FILENAME
</script>

<a href={file_contents} download={filename}><button class="btn btn-success">
Download TXT
</button></a>
8 changes: 8 additions & 0 deletions src/lib/subtitle_conversions/test_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ export const SRT_OUTPUT =`1
00:00:19,760 --> 00:00:21,200
So Long, and Thanks for All the Fish
`

export const TXT_OUTPUT = `[00:00:00.000 --> 00:00:02.560] Hello World!
[00:00:02.560 --> 00:00:07.440] This is how subtitles look like from whisper
[00:00:07.440 --> 00:00:13.160] 42 is the meaning to the life the universe and everything
[00:00:13.160 --> 00:00:17.360] And you should carry a towel everywhere
[00:00:17.360 --> 00:00:19.760] but now I have to go
[00:00:19.760 --> 00:00:21.200] So Long, and Thanks for All the Fish
`
10 changes: 10 additions & 0 deletions src/lib/subtitle_conversions/txt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {describe, test, expect, vi } from 'vitest';
import { convert_to_txt } from './TXT.svelte';
import {subs, TXT_OUTPUT} from './test_data';

describe("WebVTT Component", () => {
test("It should generate a TXT file", () => {
const result = convert_to_txt(subs)
expect(result).equal(TXT_OUTPUT);
})
});

0 comments on commit 770fdd3

Please sign in to comment.