Skip to content

Commit

Permalink
Bring back chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Jan 14, 2024
1 parent a881a5b commit 218d3ee
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions markut.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func (chunk Chunk) Duration() Millis {
return chunk.End - chunk.Start
}

type Chapter struct {
Loc Loc
Timestamp Millis
Label string
}

const MinYouTubeChapterDuration Millis = 10*1000;

func typeCheckArgs(loc Loc, argsStack []Token, signature ...TokenKind) (args []Token, err error, nextStack []Token) {
if len(argsStack) < len(signature) {
err = &DiagErr{
Expand Down Expand Up @@ -87,6 +95,7 @@ type Cut struct {
type EvalContext struct {
inputPath string
chunks []Chunk
chapters []Chapter
cuts []Cut
modified_cuts []int

Expand Down Expand Up @@ -116,6 +125,11 @@ func (context EvalContext) PrintSummary() {
}
}
fmt.Println()
fmt.Println("Chapters:")
for _, chapter := range context.chapters {
fmt.Printf("- %s - %s\n", millisToTs(chapter.Timestamp), chapter.Label)
}
fmt.Println()
fmt.Printf("Chunks Count: %d\n", len(context.chunks))
fmt.Printf("Cuts Count: %d\n", len(context.chunks) - 1)
fmt.Println()
Expand Down Expand Up @@ -151,6 +165,8 @@ func evalMarkutFile(path string) (context EvalContext, ok bool) {
lexer := NewLexer(string(content), path)
token := Token{}
argsStack := []Token{}
chapStack := []Chapter{}
var chapOffset Millis = 0
for {
token, err = lexer.Next()
if err != nil {
Expand Down Expand Up @@ -280,6 +296,19 @@ func evalMarkutFile(path string) (context EvalContext, ok bool) {
}
n := len(argsStack)
argsStack = append(argsStack, argsStack[n-1])
case "chapter":
args, err, argsStack = typeCheckArgs(token.Loc, argsStack, TokenString, TokenTimestamp)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
ok = false
return
}
chapStack = append(chapStack, Chapter{
Loc: args[1].Loc,
Label: string(args[0].Text),
Timestamp: args[1].Timestamp,
})
case "puts":
args, err, argsStack = typeCheckArgs(token.Loc, argsStack, TokenString)
if err != nil {
Expand Down Expand Up @@ -437,6 +466,26 @@ func evalMarkutFile(path string) (context EvalContext, ok bool) {
}

context.chunks = append(context.chunks, chunk)

for _, chapter := range chapStack {
if chapter.Timestamp < chunk.Start || chunk.End < chapter.Timestamp {
fmt.Printf("%s: ERROR: the timestamp %s of chapter \"%s\" is outside of the the current chunk\n", chapter.Loc, millisToTs(chapter.Timestamp), chapter.Label)
fmt.Printf("%s: NOTE: which starts at %s\n", start.Loc, millisToTs(start.Timestamp))
fmt.Printf("%s: NOTE: and ends at %s\n", end.Loc, millisToTs(end.Timestamp))
ok = false
return
}

context.chapters = append(context.chapters, Chapter{
Loc: chapter.Loc,
Timestamp: chapter.Timestamp - chunk.Start + chapOffset,
Label: chapter.Label,
})
}

chapOffset += chunk.End - chunk.Start

chapStack = []Chapter{}
default:
fmt.Printf("%s: ERROR: Unknown command %s\n", token.Loc, command)
ok = false
Expand All @@ -449,13 +498,31 @@ func evalMarkutFile(path string) (context EvalContext, ok bool) {
}
}

for i := 0; i + 1 < len(context.chapters); i += 1 {
duration := context.chapters[i + 1].Timestamp - context.chapters[i].Timestamp;
// TODO: angled brackets are not allowed on YouTube. Let's make `chapters` check for that too.
if duration < MinYouTubeChapterDuration {
fmt.Printf("%s: ERROR: the chapter \"%s\" has duration %s which is shorter than the minimal allowed YouTube chapter duration which is %s (See https://support.google.com/youtube/answer/9884579)\n", context.chapters[i].Loc, context.chapters[i].Label, millisToTs(duration), millisToTs(MinYouTubeChapterDuration));
fmt.Printf("%s: NOTE: the chapter ends here\n", context.chapters[i + 1].Loc);
ok = false;
return;
}
}

if len(argsStack) > 0 {
ok = false;
for i := range argsStack {
fmt.Printf("%s: ERROR: unused argument\n", argsStack[i].Loc)
}
}

if len(chapStack) > 0 {
ok = false;
for i := range argsStack {
fmt.Printf("%s: ERROR: unused chapter\n", chapStack[i].Loc)
}
}

return
}

Expand Down

0 comments on commit 218d3ee

Please sign in to comment.