Skip to content

Commit

Permalink
refactor: ♻️fix subtitles
Browse files Browse the repository at this point in the history
  • Loading branch information
Banou26 committed Feb 7, 2025
1 parent d8e503b commit 09b0dd2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 59 deletions.
49 changes: 21 additions & 28 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef struct Attachment {
typedef struct SubtitleFragment {
int streamIndex;
bool isHeader;
emscripten::val data;
std::string data;
std::string language;
std::string title;
long start;
Expand Down Expand Up @@ -384,7 +384,7 @@ class Remuxer {
// We handle subtitles separately
if (in_codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
// It's a subtitle header
SubtitleFragment subtitle_fragment;
SubtitleFragment subtitle_fragment = SubtitleFragment();
subtitle_fragment.streamIndex = i;
subtitle_fragment.isHeader = true;
subtitle_fragment.start = 0;
Expand All @@ -395,16 +395,10 @@ class Remuxer {
AVDictionaryEntry* title = av_dict_get(in_stream->metadata, "title", NULL, 0);
if (title) subtitle_fragment.title = title->value;
// The extradata is the "header"

std::string subtitle_data;
subtitle_data.assign((char*)in_codecpar->extradata, in_codecpar->extradata_size);
subtitle_fragment.data = subtitle_data;

subtitle_fragment.data = emscripten::val(
emscripten::typed_memory_view(
subtitle_data.size(),
subtitle_data.data()
)
);
subtitles.push_back(subtitle_fragment);
// Mark not to be remuxed in the output container (mp4)
streams_list[i] = -1;
Expand Down Expand Up @@ -560,19 +554,7 @@ class Remuxer {
break;
}

if (packet->stream_index >= number_of_streams
|| streams_list[packet->stream_index] < 0) {
// not an included stream, drop
av_packet_free(&packet);
continue;
}

AVStream* in_stream = input_format_context->streams[packet->stream_index];
if (packet->stream_index >= number_of_streams
|| streams_list[packet->stream_index] < 0) {
// not an included stream, drop
continue;
}

// If it's a subtitle packet
if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
Expand All @@ -582,13 +564,24 @@ class Remuxer {
subtitle_fragment.start = packet->pts;
subtitle_fragment.end = subtitle_fragment.start + packet->duration;
// The actual subtitle data
subtitle_fragment.data = emscripten::val(
emscripten::typed_memory_view(
packet->size,
packet->data
)
);
subtitles.push_back(std::move(subtitle_fragment));
std::string subtitle_data;
subtitle_data.assign((char*)packet->data, packet->size);
subtitle_fragment.data = subtitle_data;

subtitles.push_back(subtitle_fragment);
continue;
}

if (packet->stream_index >= number_of_streams
|| streams_list[packet->stream_index] < 0) {
// not an included stream, drop
av_packet_free(&packet);
continue;
}

if (packet->stream_index >= number_of_streams
|| streams_list[packet->stream_index] < 0) {
// not an included stream, drop
continue;
}

Expand Down
28 changes: 0 additions & 28 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,6 @@ export function debounceImmediateAndLatest<T extends (...args: any[]) => any>(
return debouncedFunction as T;
}

// https://github.com/sindresorhus/throttleit/blob/main/index.js
export const throttle = (wait: number, fn: Function) => {
if (typeof fn !== 'function') {
throw new TypeError(`Expected the first argument to be a \`function\`, got \`${typeof fn}\`.`)
}

let timeoutId: number
let lastCallTime = 0

return function throttled(...args: any[]) {
clearTimeout(timeoutId)

const now = Date.now()
const timeSinceLastCall = now - lastCallTime
const delayForNextCall = wait - timeSinceLastCall

if (delayForNextCall <= 0) {
lastCallTime = now
fn(args)
} else {
timeoutId = setTimeout(() => {
lastCallTime = Date.now()
fn(args)
}, delayForNextCall)
}
}
}

export const queuedThrottleWithLastCall = <T2 extends any[], T extends (...args: T2) => any>(time: number, func: T) => {
let runningFunction: Promise<ReturnType<T>> | undefined
let lastCall: Promise<ReturnType<T>> | undefined
Expand Down
6 changes: 3 additions & 3 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const resolvers = {
return {
...subtitle,
type: 'header',
content: new TextDecoder().decode(data)
content: data
}
}),
info: {
Expand Down Expand Up @@ -240,7 +240,7 @@ const resolvers = {
return {
...subtitle,
type: 'dialogue',
content: new TextDecoder().decode(data)
content: data
}
}),
offset: result.offset,
Expand All @@ -262,7 +262,7 @@ const resolvers = {
return {
...subtitle,
type: 'dialogue',
content: new TextDecoder().decode(data)
content: data
}
}),
offset: result.offset,
Expand Down

0 comments on commit 09b0dd2

Please sign in to comment.