Open process

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Open process

Post by MaxV » Mon Sep 26, 2016 5:44 pm

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?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Open process

Post by capellan » Mon Sep 26, 2016 9:40 pm

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.

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: Open process

Post by wsamples » Tue Sep 27, 2016 7:11 am

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.

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Open process

Post by MaxV » Tue Sep 27, 2016 9:16 am

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.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Open process

Post by MaxV » Tue Sep 27, 2016 9:26 am

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.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Open process

Post by capellan » Sun Oct 02, 2016 9:02 pm

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

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Open process

Post by zaxos » Sat Oct 08, 2016 11:45 am

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.
Knowledge is meant to be shared.

Post Reply