Skip to content

Commit

Permalink
[1/2] main - SBC pod launcher JSON based configuration parser
Browse files Browse the repository at this point in the history
main - SBC pod launcher JSON based configuration parser
where the ffmpeg command is generated.

Signed-off-by: Aly, Walid <[email protected]>
  • Loading branch information
walidbarakat committed Jan 10, 2025
1 parent aae6836 commit 3f8160a
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions launcher/config_parser/jparser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)

// processAppParams processes the appParams section and uses handlers to print the key-value pairs and collect formatted strings
func processAppParams(data map[string]interface{}) ([]string, error) {
var result []string
for key, value := range data {
if handler, ok := handlers[key]; ok {
formattedString, err := handler(key, value)
if err != nil {
return nil, fmt.Errorf("error processing key %s: %w", key, err)
}
result = append(result, formattedString)
} else {
formattedString, err := handleKeyValue(key, value)
if err != nil {
return nil, fmt.Errorf("error processing key %s: %w", key, err)
}
result = append(result, formattedString)
}
}
return result, nil
}

// callFFmpegCMD constructs a single command string from the formatted strings and calls ffmpeg
func callFFmpegCMD(formattedStrings []string) error {
commandString := strings.Join(formattedStrings, " ")
fmt.Println("Command String:", commandString)

// Split the command string into arguments
args := strings.Fields(commandString)

// Create the ffmpeg command
cmd := exec.Command("ffmpeg", args...)

// Set the command's standard output and error to the current process's standard output and error
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// Run the command
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to execute ffmpeg command: %w", err)
}

return nil
}

func main() {
// Parse command-line arguments
flag.Parse()
if flag.NArg() != 1 {
log.Fatalf("Usage: %s <config-file>", os.Args[0])
}
configFile := flag.Arg(0)

// Open the JSON configuration file
file, err := os.Open(configFile)
if err != nil {
log.Fatalf("Could not open the configuration file: %v", err)
}
defer file.Close()

// Read the JSON configuration file
byteValue, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalf("Could not read the configuration file: %v", err)
}

// Parse the JSON configuration file
var config map[string]interface{}
if err := json.Unmarshal(byteValue, &config); err != nil {
log.Fatalf("JSON parse error: %v", err)
}

// Extract and process the appParams section
if ffmpegPipelineDefinition, ok := config["ffmpegPipelineDefinition"].(map[string]interface{}); ok {
if appParams, ok := ffmpegPipelineDefinition["appParams"].(map[string]interface{}); ok {
formattedStrings, err := processAppParams(appParams)
if err != nil {
log.Fatalf("Error processing appParams: %v", err)
}
// Construct and execute the command string
if err := callFFmpegCMD(formattedStrings); err != nil {
log.Fatalf("Error executing ffmpeg command: %v", err)
}
} else {
log.Fatalf("appParams section not found in the configuration file.")
}
} else {
log.Fatalf("ffmpegPipelineDefinition section not found in the configuration file.")
}
}

0 comments on commit 3f8160a

Please sign in to comment.