-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <[email protected]>
- Loading branch information
1 parent
aae6836
commit 3f8160a
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} |