Skip to content

Commit

Permalink
1.2.0: add subtitle delay option (--subtitle-delay <seconds>)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiayang committed Aug 18, 2020
1 parent 8b82fb4 commit d2f26a1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build
*.mkv
*.jpg
*.png
.vscode
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NUMFILES := $$(($(words $(CXXSRC))))
DEFINES := -D__USE_MINGW_ANSI_STDIO=1
SANITISE :=

CXXFLAGS += -std=c++17 -fvisibility=hidden -O3 -c -Wall -fno-omit-frame-pointer $(SANITISE) $(DEFINES)
CXXFLAGS += -std=c++17 -fvisibility=hidden -O0 -g -c -Wall -fno-omit-frame-pointer $(SANITISE) $(DEFINES)
LDFLAGS += $(SANITISE) -fvisibility=hidden

PRECOMP_HDRS := source/include/precompile.h
Expand Down
30 changes: 30 additions & 0 deletions source/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define ARG_STOP_ON_ERROR "--stop-on-error"
#define ARG_SUBTITLE_LANGS "--subtitle-langs"
#define ARG_SKIP_NCOP_NCED "--skip-ncop-nced"
#define ARG_SUBTITLE_DELAY "--subtitle-delay"
#define ARG_PREFER_SDH_SUBS "--prefer-sdh-subs"
#define ARG_PREFER_TEXT_SUBS "--prefer-text-subs"
#define ARG_PREFER_ENGLISH_TITLE "--prefer-eng-title"
Expand Down Expand Up @@ -58,6 +59,10 @@ static void setupMap()
"enable metadata tagging"
});

helpList.push_back({ ARG_SUBTITLE_DELAY,
"number of seconds (eg. +0.1, -1.7) to delay the subtitles by (applies to both embedded subtitles and the extra-subs input)"
});

helpList.push_back({ ARG_MANUAL_SERIES_TITLE,
"override the series title with the given string"
});
Expand Down Expand Up @@ -568,6 +573,31 @@ namespace args
exit(-1);
}
}
else if(!strcmp(argv[i], ARG_SUBTITLE_DELAY))
{
if(i != argc - 1)
{
i++;
char* end = nullptr;
auto delay = strtod(argv[i], &end);

if(end != argv[i] + strlen(argv[i]))
{
util::error("%serror:%s invalid number '%s' for subtitle delay", COLOUR_RED_BOLD, COLOUR_RESET,
argv[i]);
exit(-1);
}

config::setSubtitleDelay(delay);
continue;
}
else
{
util::error("%serror:%s expected decimal number after '%s' option", COLOUR_RED_BOLD, COLOUR_RESET,
argv[i]);
exit(-1);
}
}
else if(!strcmp(argv[i], ARG_OUTPUT_FOLDER))
{
if(i != argc - 1)
Expand Down
4 changes: 4 additions & 0 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ namespace config
static int manualSeasonNumber = -1;
static int manualEpisodeNumber = -1;

static double subtitleDelay = 0;


void setAudioLangs(const std::vector<std::string>& xs) { audioLangs = xs; }
void setSubtitleLangs(const std::vector<std::string>& xs) { subtitleLangs = xs; }
Expand Down Expand Up @@ -309,6 +311,7 @@ namespace config
bool shouldSkipNCOPNCED() { return skipNCOPNCED; }
int getSeasonNumber() { return manualSeasonNumber; }
int getEpisodeNumber() { return manualEpisodeNumber; }
double getSubtitleDelay() { return subtitleDelay; }

void setManualMovieId(const std::string& x) { movieId = x; }
void setManualSeriesId(const std::string& x) { seriesId = x; }
Expand Down Expand Up @@ -340,6 +343,7 @@ namespace config
void setSkipNCOPNCED(bool x) { skipNCOPNCED = x; }
void setSeasonNumber(int x) { manualSeasonNumber = x; }
void setEpisodeNumber(int x) { manualEpisodeNumber = x; }
void setSubtitleDelay(double x) { subtitleDelay = x; }

void setConfigPath(const std::string& x)
{
Expand Down
4 changes: 4 additions & 0 deletions source/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,15 @@ namespace config
int getSeasonNumber();
int getEpisodeNumber();

double getSubtitleDelay();

bool isMuxing();
bool isTagging();



void setSubtitleDelay(double seconds);

void setAudioLangs(const std::vector<std::string>& xs);
void setSubtitleLangs(const std::vector<std::string>& xs);

Expand Down
20 changes: 16 additions & 4 deletions source/muxing/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace mux

// refer: https://github.com/FFmpeg/FFmpeg/blob/10bcc41bb40ba479bfc5ad29b1650a6b335437a8/doc/examples/remuxing.c
static bool writeOutput(const std::fs::path& outfile, AVFormatContext* inctx, AVFormatContext* ssctx,
const std::vector<AVStream*>& finalStreams, std::unordered_map<AVStream*, size_t>& finalStreamMap)
const std::vector<AVStream*>& finalStreams, std::unordered_map<AVStream*, size_t>& finalStreamMap, double subtitleDelay)
{
AVFormatContext* outctx = 0;
if(avformat_alloc_output_context2(&outctx, nullptr, "matroska", outfile.string().c_str()) < 0)
Expand Down Expand Up @@ -105,7 +105,7 @@ namespace mux
int64_t maxPts = 0;
size_t frameCount = 0;

auto copy_frames = [&maxPts, &frameCount, &finalStreamMap](AVFormatContext* inctx, AVFormatContext* ssctx,
auto copy_frames = [&maxPts, &frameCount, &finalStreamMap, subtitleDelay](AVFormatContext* inctx, AVFormatContext* ssctx,
AVFormatContext* outctx)
{
// is this even advisable??? subtitle files should be small, right??
Expand Down Expand Up @@ -148,7 +148,7 @@ namespace mux
return a->dts < b->dts;
});

auto copy_packet = [&frameCount, &maxPts, &finalStreamMap](AVFormatContext* outctx, AVStream* istrm, AVPacket* pkt) {
auto copy_packet = [&frameCount, &maxPts, &finalStreamMap, subtitleDelay](AVFormatContext* outctx, AVStream* istrm, AVPacket* pkt) {

// looks like we're re-using the same packet.
pkt->stream_index = finalStreamMap[istrm];
Expand All @@ -166,6 +166,15 @@ namespace mux
pkt->duration = av_rescale_q(pkt->duration, istrm->time_base, ostrm->time_base);
pkt->pos = -1;

// check for subtitles
if(istrm->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
{
// this is the subtitle delay.
auto delay = subtitleDelay;
auto pts_delay = (delay * ostrm->time_base.den) / ostrm->time_base.num;
pkt->pts += pts_delay;
}

if(pkt->dts == AV_NOPTS_VALUE)
{
util::error("frame %d (s:%d) has no dts", frameCount, pkt->stream_index);
Expand Down Expand Up @@ -867,10 +876,13 @@ namespace mux



if(auto d = config::getSubtitleDelay(); d != 0)
util::log("subtitle delay: %.3f s", d);

// make the output file:
if(!config::isDryRun())
{
if(!writeOutput(outfile, ctx, ssctx, finalStreams, finalStreamMap))
if(!writeOutput(outfile, ctx, ssctx, finalStreams, finalStreamMap, config::getSubtitleDelay()))
return false;
}

Expand Down

0 comments on commit d2f26a1

Please sign in to comment.