From 3f8160a63a1bf62f3928e2dcba1d55621ef66456 Mon Sep 17 00:00:00 2001 From: "Aly, Walid" Date: Thu, 9 Jan 2025 17:37:49 +0000 Subject: [PATCH] [1/2] main - SBC pod launcher JSON based configuration parser main - SBC pod launcher JSON based configuration parser where the ffmpeg command is generated. Signed-off-by: Aly, Walid --- launcher/config_parser/jparser.go | 102 ++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 launcher/config_parser/jparser.go diff --git a/launcher/config_parser/jparser.go b/launcher/config_parser/jparser.go new file mode 100644 index 0000000..20d2602 --- /dev/null +++ b/launcher/config_parser/jparser.go @@ -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 ", 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.") + } +}