Get the output value of system - ruby-on-rails

I am using the following method to get the time of the video with ffmpeg do not know what reason I can not put the output of the command
command =~ /Duration: ([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
variable for time and then insert in the
can someone give a help?
def get_time_video
command = system " ffmpeg -i video.flv 2>&1 "
command =~ /Duration: ([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
time = " #{$1}:#{$2}:#{$3} "
puts time # 00:00:30
update_attribute(:time, “#{time}”)
end

The Kernel.system function returns true or false as seen in the Documentation. If you want to parse the output of a command, you can use the backtick notation:
system = `ffmpeg -i video.flv 2>&1`

Related

io.popen how to get the go output in lua

How could I capture compiler errors go in lua?
I'm trying to get the output of the comp compiler errors in a tmux panel using lua
when executing the script the result is only shown in the current panel and not in the second panel
and the /tmp/output file is always empty
cmd=io.popen("go build -gcflags=-e scree.go")
f=io.open("/tmp/output")
f:write(cmd:read("*all"))
for line in f:lines() do
os.execute("tmux run-shell -t 2 'echo " .. line .. "' ")
end
f:close()
Is there any way to do this without using a temporary file?
I'm not totally clear on this. But maybe something like the following. i.e. pipe stderr to stdout and capture the result (not tested).
f = assert (io.popen ("go build -gcflags=-e scree.go 2>&1"))
for line in f:lines() do
os.execute("tmux run-shell -t 2 'echo " .. line .. "' ")
end
f:close()
I think the key is that popen won't capture stderr. See further details about that here

Lua: forward output from Minicom

I have a Lua script and in there I open a minicom session which executes a script (with the -S" parameter).
local myFile = assert(io.popen('minicom -S myScript.sh ' myDevice ' -C myLogFile.log'))
local myFileOutput = myFile:read('*all')
myFile:close()
This works really fine.
But I would like to get the same output as if I execute the minicom command itself:
minicom -S myScript.sh ' myDevice ' -C myLogFile.log
Right now I don't get any output at all (I know that that's somehow obvious).
I would that the output should also occur at (at least nearly) the same time as with the minicom command itself. Not one big chuck of data at the end.
Does anyone know how to achieve that?
If I understand you correctly, you need something like
local myFile = assert(io.popen('minicom ...'))
for line in myFile:lines('l') do
print(line)
end
myFile:close()

Capture output of shell command, line by line, into an array

I want to capture the total number of rubocop offenses to determine whether my codebase is getting better or worse. I have almost no experience with ruby scripting.
This is my script so far:
#!/usr/bin/env ruby
#script/code_coverage
var = `rubocop ../ -f fuubar -o ../coverage/rubocop_results.txt -f offenses`
puts var
So I ./rails-app/script/code_coverage and my terminal displays
...
--
6844 Total
When I var.inspect I get a long string. I want to either read the output of rubocop ../ -f ... line by line (preferred) or capture each line of output into an array.
I would like to be able to do something like this:
var.each |line| do
if line.contains('total')
...
total = ...
end
Is this possible? I guess this would be similar to writing the output to a file and and then reading the file in line by line.
If you want to keep it simple, you can simply split your var string.
var = `rubocop ../ -f fuubar -o ../coverage/rubocop_results.txt -f offenses`.split("\n")
Then you can iterate on var like you wanted to.
use open3 library of ruby. Open3 grants you access to stdin, stdout, stderr and a thread to wait the child process when running another program. You can specify various attributes, redirections, current directory, etc., of the program as Process.spawn.
http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html
require 'open3'
# Run lynx on this file.
cmd = "lynx -crawl -dump /data/feed/#{file_name}.html > /data/feed/#{file_name}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
cmdout = stdout.read
$logger.debug "stdout is:" + stdout.read
$logger.debug "stderr is:" + stderr.read
end

How to capture ffmpeg output in rails?

I'm running a ffmpeg command to try to get the duration of a video file, the command is as follows...
system('ffmpeg -i C:\Users\example\Desktop\video9.mp4 -f ffmetadata')
When I run that line it outputs a lot of info to the rails console, including duration. But how would I capture that info so I can split it and grab the data I need? (I'm doing this inside a rails controller)
When I run something like this...
metadata = system('ffmpeg -i C:\Users\example\Desktop\video9.mp4 -f ffmetadata')
puts metadata
All it returns is false.
Use:
output = `ffmpeg -i C:\\Users\\example\\Desktop\\video9.mp4 -f ffmetadata`
The problem is that system doesn't capture the output of the command being run. Instead, we use %x[...] or its equivalent using backticks, which captures the sub-shell's STDOUT.
If you need more control, look at Open3.capture3.
Found it...
inspect_command = "ffmpeg -i " + file_location + " 2>&1 "
metadata = `#{inspect_command}`
If all you need to get is the video duration use ffprobe instead of ffmpeg. It returns the video metadata directly.

Rails not responding to ffmpeg commands

I am trying to run this command here:
f = open("|ffmpeg -i /Users/joaoh82/Desktop/teste.MP4")
result = f.read()
But I am not getting any response...
But when I try this command in the terminal it works great:
ffmpeg -i /Users/joaoh82/Desktop/teste.MP4
But now on rails code. Funny thing is that when I try the same thing with some else like an echo $PATH it works great! Like:
f = open("|echo $PATH")
result = f.read()
Any ideas!?
ffmpeg -i prints to stderr, which won't be captured by your pipe. You could redirect stderr to stdout:
result = `ffmpeg -i /Users/joaoh82/Desktop/teste.MP4 2>&1`
You probably need to specify the full path to ffmpeg. It might be working in your shell because ffmpeg is in your PATH.
Basically in your shell, type which ffmpeg. Use that full path in your open() call.

Resources