-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (106 loc) · 3.49 KB
/
Copy pathmain.go
File metadata and controls
125 lines (106 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"encoding/json"
"io"
"strings"
"os"
"os/exec"
"log"
"fmt"
)
// This program is not secure!
// It is only meant to be used in local networks where configuration is known to not be malicious
// Maybe use a different way to ssh connect at some point
// Find a way to do like a file stream at some point to not wait for copying the file each way
type Config struct {
Hostname string `json:"hostname"`
Host string `json:"host"`
SshPort string `json:"sshPort"`
}
func main() {
logFile, err := os.Open("log.txt")
if err != nil {
log.Fatal(err)
}
log.SetOutput(logFile)
// Read Config file
var config *Config
configFile, openErr := os.Open("config.json")
if os.IsNotExist(openErr) {
} else if openErr != nil {
log.Fatalf("While trying to open config.json, got %s", openErr)
}
configData, readErr := io.ReadAll(configFile)
if readErr != nil {
log.Fatal(readErr)
}
jsonErr := json.Unmarshal(configData, &config)
if jsonErr != nil {
log.Fatal(jsonErr)
}
log.Printf("Config: %v", config)
// Get the FFmpegArgs to pass through
var ffmpegArgs []string
ffmpegArgs = os.Args
var ffmpegOutputArgs []string
var inputFile string
var outputFile string
log.Println(os.Args)
// Get the files that are used in simple FFmpeg commands
for i := 1; i < len(ffmpegArgs) - 1; i++ {
if ffmpegArgs[i] == "-i" {
inputFile = ffmpegArgs[i + 1]
i++ // Skip the input file as well
continue
}
ffmpegOutputArgs = append(ffmpegOutputArgs, ffmpegArgs[i])
}
outputFile = ffmpegArgs[len(ffmpegArgs) - 1]
log.Println(ffmpegOutputArgs)
if strings.HasPrefix(outputFile, "file:") {
outputFile = outputFile[5:]
log.Fatal(outputFile)
}
log.Printf("FFmpeg args %v", ffmpegArgs)
log.Printf("Input file: %s", inputFile)
log.Printf("Output file: %s", outputFile)
// Get a temp folder from host
fmt.Println("Getting Temp Dir...")
mkTempArgs := []string{fmt.Sprintf("%s@%s", config.Hostname, config.Host), "-p", config.SshPort, "mktemp", "-d"}
mkTempCmd := exec.Command("ssh", mkTempArgs...)
cmdOutput, mkTempRunErr := mkTempCmd.Output()
if mkTempRunErr != nil {
log.Fatal(mkTempRunErr)
}
var tmpFolder string
// Remove the newline character at the end
tmpFolder = string(cmdOutput[:len(cmdOutput) - 1])
log.Printf("Temp Folder: %s", tmpFolder)
// cat test.mp4 | ssh robot@robot ffmpeg -i - -map_metadata -1 -c:v libsvtav1 -crf 30 -preset 1 -b:v 0 -g 60 -movflags +faststart -c:a copy -f matroska - > c.mkv
// Steam to remote, transcode, and stream back to local
everythingArgs := []string{fmt.Sprintf("%s@%s", config.Hostname, config.Host), "ffmpeg", "-i", "-"}
everythingArgs = append(everythingArgs, ffmpegOutputArgs...)
everythingArgs = append(everythingArgs, []string{"-f", "matroska", "-"}...)
fmt.Println("Doing Everything...")
fmt.Println(everythingArgs)
log.Println(everythingArgs)
everythingCmd := exec.Command("ssh", everythingArgs...)
c1 := exec.Command("cat", inputFile)
everythingCmd.Stdin, _ = c1.StdoutPipe()
c2 := exec.Command("tee", outputFile)
c2.Stdin, _ = everythingCmd.StdoutPipe()
_ = everythingCmd.Start()
_ = c2.Start()
_ = c1.Start()
_ = everythingCmd.Wait()
_ = c1.Wait()
_ = c2.Wait()
// Remove the temp folder when done with it
fmt.Println("Removing Temp Dir...")
deleteTempArgs := []string{fmt.Sprintf("%s@%s", config.Hostname, config.Host), "-p", config.SshPort, "rm", "-r", tmpFolder}
deleteTempCmd := exec.Command("ssh", deleteTempArgs...)
cmdOutput, runErr := deleteTempCmd.Output()
if runErr != nil {
log.Fatal(runErr)
}
}