Skip to content

Commit

Permalink
Fix issues cutting where sponsor overlaps exists #16
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloo committed Jan 23, 2021
1 parent 39dd56c commit c9ba3f9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/sponskrub/methods/chapter.d
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ClipChapterTime[] merge_sponsor_times_with_chapters(ClipTime[] sponsor_times, Ch
next_terminal = chapter_times[chapter_index].end;
chapter_index++;
}
clip_chapters ~= ClipChapterTime(clip_terminal, next_terminal, "", chapter_title);
clip_chapters ~= ClipChapterTime(clip_terminal, next_terminal, Categories.Content, chapter_title);
clip_terminal = next_terminal;
}
}
Expand Down
31 changes: 8 additions & 23 deletions src/sponskrub/methods/cut.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,15 @@ import std.array;
import std.typecons;
import sponsorblock;
import ffwrap;
import chapter;

ClipTime[] timestamps_to_keep(ClipTime[] sponsor_times, string video_length) {
ClipTime[] clip_times;
sponsor_times.sort!((a, b) => a.start.to!float < b.start.to!float);

//If the sponsorship is directly at the beginning don't add both content and the sponsor
if (sponsor_times[0].start != "0.000000") {
clip_times ~= ClipTime("0", sponsor_times[0].start, "content");
}


for (auto i = 0; i < sponsor_times.length; i++) {
auto clip_start = "";
auto clip_end = "";
clip_start = sponsor_times[i].end;
if (i+1 < sponsor_times.length) {
clip_end = sponsor_times[i+1].start;
} else {
clip_end = video_length;
}
clip_times ~= ClipTime(clip_start, clip_end, "content");
}

return clip_times;
ClipTime[] timestamps_to_keep(ClipChapterTime[] chapters) {
//we can redo this so it just filters a bunch of chapter times and includes only content
return chapters
.sort!((a, b) => a.start.to!float < b.start.to!float)
.filter!(chapter => chapter.category == Categories.Content)
.map!(chapter => ClipTime(chapter.start, chapter.end, "")) //we need better types, this shouldn't need a category
.array;
}

string cut_and_cat_clips_filter(ClipTime[] timestamps, FileCategory category) {
Expand Down
26 changes: 14 additions & 12 deletions src/sponskrub/sponskrub.d
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,30 @@ Options:
if (sponsor_times.length > 0) {
bool ffmpeg_status;

ChapterTime[] chapter_times;
ClipChapterTime[] new_chapter_times;

chapter_times = get_chapter_times(input_filename);

if (chapter_times.length == 0) {
chapter_times = [ChapterTime("0", video_length, "sponskrub-content")];
}

new_chapter_times = merge_sponsor_times_with_chapters(sponsor_times, chapter_times);

if ("chapter" in parsed_arguments.flag_arguments) {
writeln("Marking the shilling...");

ChapterTime[] chapter_times;
ClipChapterTime[] new_chapter_times;

chapter_times = get_chapter_times(input_filename);

if (chapter_times.length == 0) {
chapter_times = [ChapterTime("0", video_length, "sponskrub-content")];
}

new_chapter_times = merge_sponsor_times_with_chapters(sponsor_times, chapter_times);

ffmpeg_status = add_ffmpeg_metadata(
input_filename,
output_filename,
generate_chapters_metadata(new_chapter_times)
);
} else {
auto content_times = timestamps_to_keep(sponsor_times, video_length);
//using the chapter data also means that in future we could also adjust
//preexisting chapter metadata to remain accurate after the cut
writeln("Surgically removing the shilling...");
auto content_times = timestamps_to_keep(new_chapter_times);

ffmpeg_status = run_ffmpeg_filter(
input_filename,
Expand Down
4 changes: 3 additions & 1 deletion src/sponskrub/sponsorblock.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ enum Categories: string {
Outro = "outro",
Interaction = "interaction",
SelfPromo = "selfpromo",
NonMusic = "music_offtopic"
NonMusic = "music_offtopic",
//
Content = "content"
}

alias ClipTime = Tuple!(string, "start", string, "end", string, "category");
Expand Down

0 comments on commit c9ba3f9

Please sign in to comment.