Home >Backend Development >Golang >How Can I Retrieve and Process Real-time Output from Long-Running Shell Commands in Go?
When executing long-running shell commands with os/exec in Go, it's often beneficial to retrieve and process the output in real time. Take, for instance, the ffmpeg command, which outputs progress information in the form of lines like this:
frame= 101 fps=0.0 q=28.0 size= 91kB time=00:00:04.13 bitrate= 181.2kbits/
Our goal is to capture and process these lines, extracting the progress information to display a live progress bar.
Unlike stdout, ffmpeg sends diagnostic messages (console output) to stderr. By modifying our code to read from stderr instead, we can capture the progress information:
package main import ( "bufio" "fmt" "os/exec" "strings" ) func main() { args := "-i test.mp4 -acodec copy -vcodec copy -f flv rtmp://aaa/bbb" cmd := exec.Command("ffmpeg", strings.Split(args, " ")...) stderr, _ := cmd.StderrPipe() cmd.Start() scanner := bufio.NewScanner(stderr) scanner.Split(bufio.ScanWords) for scanner.Scan() { m := scanner.Text() fmt.Println(m) } cmd.Wait() }
With this modification, we can continuously print the progress information as the command executes.
To extract the progress information, we need to parse the lines from stderr and isolate the relevant values. In the case of ffmpeg, we're interested in the "time" field:
time=00:00:04.13
By parsing this value and converting it to a ratio (e.g., "4.13 seconds / total duration"), we can display the progress as a percentage. This allows us to track the command's progress in real-time and provide a user-friendly experience.
The above is the detailed content of How Can I Retrieve and Process Real-time Output from Long-Running Shell Commands in Go?. For more information, please follow other related articles on the PHP Chinese website!