Skip to content

Commit

Permalink
Move FFmpeg Argument funcs to the global funcs array
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 6, 2024
1 parent 568ebbd commit 1904adb
Showing 1 changed file with 105 additions and 56 deletions.
161 changes: 105 additions & 56 deletions markut.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,19 @@ type EvalContext struct {
ExtraInFlags []string
}

const (
DefaultVideoCodec = "libx264"
DefaultVideoBitrate = "4000k"
DefaultAudioCodec = "aac"
DefaultAudioBitrate = "300k"
)

func defaultContext() (context EvalContext) {
// Default chunk transcoding parameters
context.VideoCodec = "libx264"
context.VideoBitrate = "4000k"
context.AudioCodec = "aac"
context.AudioBitrate = "300k"
context.VideoCodec = DefaultVideoCodec
context.VideoBitrate = DefaultVideoBitrate
context.AudioCodec = DefaultAudioCodec
context.AudioBitrate = DefaultAudioBitrate
context.outputPath = "output.mp4"
return
}
Expand Down Expand Up @@ -222,7 +229,7 @@ type Func struct {

var funcs = map[string]Func{
"chat": {
Description: "Load a chat log generated by https://www.twitchchatdownloader.com/$SPOILER$ which is going to be used by the subsequent `chunk` func calls to include certain messages into the subtitles generated by the `markut chat` subcommand. There could be only one loaded chat log at a time. Repeated calls to the `chat` func replace the currently loaded chat log with another one. The already defined chunks keep the copy of the logs that were loaded at the time of their definition.",
Description: "Load a chat log file generated by https://www.twitchchatdownloader.com/$SPOILER$ which is going to be used by the subsequent `chunk` func calls to include certain messages into the subtitles generated by the `markut chat` subcommand. There could be only one loaded chat log at a time. Repeated calls to the `chat` func replace the currently loaded chat log with another one. The already defined chunks keep the copy of the logs that were loaded at the time of their definition.",
Signature: "<path:String> %name",
Category: "Chat",
Run: func(context *EvalContext, command string, token Token) bool {
Expand Down Expand Up @@ -404,6 +411,98 @@ var funcs = map[string]Func{
return true
},
},
"video_codec": {
Description: "Set the value of the output video codec flag (-c:v). Default is \""+DefaultVideoCodec+"\".",
Signature: "<codec:String> %name",
Category: "FFmpeg Arguments",
Run: func(context *EvalContext, command string, token Token) bool {
args, err := context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.VideoCodec = string(args[0].Text)
return true;
},
},
"video_bitrate": {
Description: "Set the value of the output video bitrate flag (-vb). Default is \""+DefaultVideoBitrate+"\".",
Signature: "<bitrate:String> %name",
Category: "FFmpeg Arguments",
Run: func(context *EvalContext, command string, token Token) bool {
args, err := context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.VideoBitrate = string(args[0].Text)
return true;
},
},
"audio_codec": {
Description: "Set the value of the output audio codec flag (-c:a). Default is \""+DefaultAudioCodec+"\".",
Signature: "<codec:String> %name",
Category: "FFmpeg Arguments",
Run: func(context *EvalContext, command string, token Token) bool {
args, err := context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.AudioCodec = string(args[0].Text)
return true;
},
},
"audio_bitrate": {
Description: "Set the value of the output audio bitrate flag (-ab). Default is \""+DefaultAudioBitrate+"\".",
Signature: "<bitrate:String> %name",
Category: "FFmpeg Arguments",
Run: func(context *EvalContext, command string, token Token) bool {
args, err := context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.AudioBitrate = string(args[0].Text)
return true;
},
},
"outf": {
Description: "Append extra output flag",
Signature: "<flag:String> %name",
Category: "FFmpeg Arguments",
Run: func(context *EvalContext, command string, token Token) bool {
args, err := context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
outFlag := args[0]
context.ExtraOutFlags = append(context.ExtraOutFlags, string(outFlag.Text))
return true;
},
},
"inf": {
Description: "Append extra input flag",
Signature: "<flag:String> %name",
Category: "FFmpeg Arguments",
Run: func(context *EvalContext, command string, token Token) bool {
args, err := context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
inFlag := args[0]
context.ExtraInFlags = append(context.ExtraInFlags, string(inFlag.Text))
return true;
},
},
}

// This function is compatible with the format https://www.twitchchatdownloader.com/ generates.
Expand Down Expand Up @@ -506,56 +605,6 @@ func (context *EvalContext) evalMarkutFile(path string) bool {
case TokenSymbol:
command := string(token.Text)
switch command {
case "video_codec":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.VideoCodec = string(args[0].Text)
case "video_bitrate":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.VideoBitrate = string(args[0].Text)
case "audio_codec":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.AudioCodec = string(args[0].Text)
case "audio_bitrate":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
context.AudioBitrate = string(args[0].Text)
case "outf":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
outFlag := args[0]
context.ExtraOutFlags = append(context.ExtraOutFlags, string(outFlag.Text))
case "inf":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}
inFlag := args[0]
context.ExtraInFlags = append(context.ExtraInFlags, string(inFlag.Text))
case "include":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
Expand Down Expand Up @@ -1354,7 +1403,7 @@ var Subcommands = map[string]Subcommand{
sort.Slice(names, func(i, j int) bool {
return names[i] < names[j]
})
sort.Slice(names, func(i, j int) bool {
sort.SliceStable(names, func(i, j int) bool { // Rare moment in my boring dev life when I actually need a stable sort
return funcs[names[i]].Category < funcs[names[j]].Category
})
if len(names) > 0 {
Expand Down

0 comments on commit 1904adb

Please sign in to comment.