Page 1 of 1

Open process

Posted: Mon Sep 26, 2016 5:44 pm
by MaxV
Hi,
I just discovered the open process is like shell() and doesn’t block livecode. Nice! :D
I figured to use it to read the output of a command, for example ffmpeg. I use ffmpeg to convert videos to the new h.265 (incedible compression!), a ffmpeg output is like:

Code: Select all

frame=   44 fps= 11 q=-0.0 size=      91kB time=00:00:01.87 bitrate= 398.3kbits/
frame=   48 fps=9.9 q=-0.0 size=     112kB time=00:00:02.04 bitrate= 449.9kbits/
frame=   51 fps=9.2 q=-0.0 size=     117kB time=00:00:02.17 bitrate= 439.7kbits/
frame=   55 fps=9.1 q=-0.0 size=     132kB time=00:00:02.30 bitrate= 469.9kbits/
but using open process this way:
########CODE#######
on mouseUp
close process "/usr/bin/ffmpeg -i SampleVideo_1280x720_5mb.mp4 -c:v libx265 -preset medium -x265-params crf=28 -c:a aac -strict experimental -b:a 128k filesupercompresso.MP4"
set the defaultFolder to "~/Scaricati"
put empty into field 1
delete file "filesupercompresso.MP4"
open process "/usr/bin/ffmpeg -i SampleVideo_1280x720_5mb.mp4 -c:v libx265 -preset medium -x265-params crf=28 -c:a aac -strict experimental -b:a 128k filesupercompresso.MP4" for text read
controlla
end mouseUp

on controlla
read from process "/usr/bin/ffmpeg -i SampleVideo_1280x720_5mb.mp4 -c:v libx265 -preset medium -x265-params crf=28 -c:a aac -strict experimental -b:a 128k filesupercompresso.MP4" until linefeed
put it after field 1
--if the result is not "eof" then
--put the result & return after field 1
send controlla to me in 1 sec
--end if
end controlla
#####END OF CODE#####

I get much more and conversion seems to me much slower:

Code: Select all

frame=   44 fps=9.8 q=-0.0 size=      91kB time=00:00:01.87 bitrate= 398.3kbits/s speed=0.418x    
[h264 @ 0x4b67ea0] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4b83800] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4a624a0] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4a6b0c0] nal_unit_type: 1, nal_ref_idc: 2
frame=   48 fps=9.1 q=-0.0 size=     112kB time=00:00:02.04 bitrate= 449.9kbits/s speed=0.388x    
[h264 @ 0x4a5ab40] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4b67ea0] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4b83800] nal_unit_type: 1, nal_ref_idc: 2
frame=   51 fps=8.5 q=-0.0 size=     117kB time=00:00:02.17 bitrate= 439.7kbits/s speed=0.362x    
[h264 @ 0x4a624a0] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4a6b0c0] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4a5ab40] nal_unit_type: 1, nal_ref_idc: 2
[h264 @ 0x4b67ea0] nal_unit_type: 1, nal_ref_idc: 2
frame=   55 fps=8.0 q=-0.0 size=     132kB time=00:00:02.32 bitrate= 465.6kbits/s speed=0.338x   
What am I doing wrong?
And how can I know the process ended?

Re: Open process

Posted: Mon Sep 26, 2016 9:40 pm
by capellan
Probably, this topic belongs to the Linux section of this forum,
but at first sight, I do not understand how could you delete
this mp4 file and later use it in the same handler.

Re: Open process

Posted: Tue Sep 27, 2016 7:11 am
by wsamples
Would it work to use the '-progress url' option and monitor the resultant file? From the ffmpeg manpages:

Send program-friendly progress information to url.
Progress information is written approximately every second and at the end of the
encoding process. It is made of "key=value" lines. key consists of only alphanumeric
characters. The last key of a sequence of progress information is always "progress".

When the conversion is finished the last line of this file will read "progress=end". This might make it unnecessary to read from the process itself. Example, works here for me:

Code: Select all

open process "ffmpeg -progress progress.txt -i input.m4v -c:v libx264 -preset ultrafast output.mp4"
An alternative approach would be to trigger an independent standalone which runs the conversion as a blocking shell call and signals the calling app when the shell call finishes.

I don't have any good advice for a general solution regarding 'open process', but this may help you in this case.

Re: Open process

Posted: Tue Sep 27, 2016 9:16 am
by MaxV
capellan wrote:Probably, this topic belongs to the Linux section of this forum,
but at first sight, I do not understand how could you delete
this mp4 file and later use it in the same handler.
The delete file is just to test again and again code.
I'd like to see some example of good coding about open process and read from process.

Re: Open process

Posted: Tue Sep 27, 2016 9:26 am
by MaxV
wsamples wrote:Example, works here for me:

Code: Select all

open process "ffmpeg -progress progress.txt -i input.m4v -c:v libx264 -preset ultrafast output.mp4"
This is a nice trick, thank you.

Re: Open process

Posted: Sun Oct 02, 2016 9:02 pm
by capellan
Hi MaxV,

Today, I remember that this stack (published by Scott Raney of Metacard)
shows some examples of launching and calling an external process.

Use the Project browser to visit the card with these handlers or
just click the right arrow until this card appears.

Almost 20 years later, this stack and most of these handlers
works fine in Livecode running on Linux. :D

https://dl.dropboxusercontent.com/u/383 ... s.livecode

Re: Open process

Posted: Sat Oct 08, 2016 11:45 am
by zaxos
try:

Code: Select all

local tProcess
on startProcess
put "/usr/bin/ffmpeg -i SampleVideo_1280x720_5mb.mp4 -c:v libx265 -preset medium -x265-params crf=28 -c:a aac -strict experimental -b:a 128k filesupercompresso.MP4" into tProcess
open process tProcess for read
readProcess
end startProcess

on readProcess
read from process tProcess for 10 lines in 100 millisec
put it after fld 1
if tProcess is among the lines of the openProcesses then
send readProcess to me in 1 tick
else
put "Process closed." into fld 1
end if
end readProcess
I have used that with robocopy operations and so far the speeds are good.