QProgressBar

438 views
Skip to first unread message

Ricardo Viana

unread,
Mar 6, 2013, 11:22:19 AM3/6/13
to python_in...@googlegroups.com
Hi Fellas!

i'm trying to build my own video converter gui on a Linux. Using Pyqt4.
I'm using avconv command line converter.

I have setup all parameters and it is working fine.

The thing is i would like to have some kind of progress feedback.
Does anyone know how to retrieve the command line feedback so
i can hook it to some kind of expression to drive the QProgressBar?

thank you very much

--
////////////////////////////////////
Ricardo Viana
VFX Generalist

gui.png

Justin Israel

unread,
Mar 6, 2013, 1:07:14 PM3/6/13
to python_in...@googlegroups.com

Does the avconv output give you textual progress? If so, you would need to be reading from the subprocess or QProcess as it runs, parsing the progress value and setting that value on your progress meter.
QProcess will emit signals as new output is ready from the command, whereas a subprocess would require that you keep checking it, either with a qtimer until its done,  or a for loop where you make sure to be periodically calling QApplication.processEvents() to keep the event loop flowing.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To post to this group, send email to python_inside_maya@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Ricardo Viana

unread,
Mar 15, 2013, 2:03:51 PM3/15/13
to python_in...@googlegroups.com
Hi Justin

Been playing around with QProcess but only got it to send start/finished signals.
trying subprocess now.
here is what i have. But i can't append the stdout from the process.
It exports everything ok. just no feedback.
----code-----------

    def export(self):
       
        self.proc=subprocess.Popen("avconv -i %s -b %s -s %s %s" %(self.inputFile,self.bitRatesList[self.bitRate.currentIndex()],
                                                  self.sizeList[self.sizeSel.currentIndex()],self.outputFile),shell=True,stdout=subprocess.PIPE)
        for i in range (2000):
            output=self.proc.stdout.readline()
            self.log.append(output)

---code------

cheers all
Ricardo Viana






On 03/06/2013 06:07 PM, Justin Israel wrote:

Does the avconv output give you textual progress? If so, you would need to be reading from the subprocess or QProcess as it runs, parsing the progress value and setting that value on your progress meter.
QProcess will emit signals as new output is ready from the command, whereas a subprocess would require that you keep checking it, either with a qtimer until its done,  or a for loop where you make sure to be periodically calling QApplication.processEvents() to keep the event loop flowing.

On Mar 7, 2013 5:22 AM, "Ricardo Viana" <cgolh...@gmail.com> wrote:
Hi Fellas!

i'm trying to build my own video converter gui on a Linux. Using Pyqt4.
I'm using avconv command line converter.

I have setup all parameters and it is working fine.

The thing is i would like to have some kind of progress feedback.
Does anyone know how to retrieve the command line feedback so
i can hook it to some kind of expression to drive the QProgressBar?

thank you very much

--
////////////////////////////////////
Ricardo Viana
VFX Generalist

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Justin Israel

unread,
Mar 15, 2013, 11:29:01 PM3/15/13
to python_in...@googlegroups.com
QProcess has signals that indicate when new data is ready to read from your process, allowing to to get line updates. Also, in your example, you are going to be blocking the event loop with your for loop.

I put together an example of each of those for you here:
https://gist.github.com/justinfx/5174795

The first example shows how to monitor the QProcess for new output

The second shows how to use subprocess in a way that you can loop over the output, but still pump the event loop. If your lines are being emitted quickly you probably don't need to make the call for every loop, but rather you could call on on every 10 lines...or 100 lines.. or whatever is appropriate to give your main gui thread a chance to process the pending events:

i.e.

i = 0
while True:
line = process.stdout.readline()
if not line:
break

# process line

# process event loop every 10 lines
if i % 10 == 0:
QtGui.qApp.processEvents()
i +=1


-- justin

Ricardo Viana

unread,
Mar 18, 2013, 6:00:01 AM3/18/13
to python_in...@googlegroups.com
Thank you very much for the help Justin.

i followed your code on gist.

here is what i have now:

-----code----


def timer(self):
QtCore.QTimer.singleShot(100,self.export)



def export(self):
self.log.append("started")

self.proc=subprocess.Popen("avconv -i %s -b %s -s %s %s"
%(self.inputFile,self.bitRatesList[self.bitRate.currentIndex()],
self.sizeList[self.sizeSel.currentIndex()],self.outputFile),shell=True,stdout=subprocess.PIPE)
while True:
output=self.proc.stdout.readline()
if not output:
break
QtGui.qApp.processEvents()
self.log.append(output.strip())




-----code----


what is happening is the following. The self.log.append("started") only
shows when the subprocess terminates and it should be the
other way around. Also i can't append the output.strip to the
QTextBrowser(self.log) i have. it shows nicely on the terminal but not
in the GUI.

Another thing i can't figure is why i have to say to subprocess ->
Shell=True. In your code you don't have it. but if i run it that way
i get an error :

File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Justin Israel

unread,
Mar 18, 2013, 2:53:52 PM3/18/13
to python_in...@googlegroups.com

Can you post a full example on pastsbin or gist? It sounds like something is happening before the event loop.
Also for subprocess, you should pass the command as a list so you don't need shell=True

command=["avconv", "-i", self.inputFile, "-b", self.bitRatesList[self.bitRate.currentIndex()], "-s", self.sizeList[self.sizeSel.cur rentIndex()], self.outputFile]

Generally though, I think the QProcess approach is more robust as it plays nicely with the event loop.

With the qtimer then reason I was using it plus a 100 ms timeout was to have the callback placed into the event loop and run after the app had fully started and signals would work. In a real case, the process would probably get started by some action in your running app. Like a button press or a response to some signal.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To post to this group, send email to python_inside_maya@googlegroups.com.

Ricardo Viana

unread,
Mar 18, 2013, 3:06:44 PM3/18/13
to python_in...@googlegroups.com
Thanks for the help man. Learning quite a lot of python just by joining this group.

here is the link:

https://gist.github.com/RicardoViana/5189872


cheers
Ricardo Viana
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Justin Israel

unread,
Mar 18, 2013, 3:57:37 PM3/18/13
to python_in...@googlegroups.com
This works fine as a test, when I change these lines:

        command = 'for i in {1..5}; do echo $i; sleep 1; done'
        self.proc=subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

Also, you can just connect the button right to the slot:

        saveFile = QtGui.QPushButton('Export')
        saveFile.clicked.connect(self.export)

Now here is a down side of the subprocess approach. What if the application takes a while to print a line? That means your application will still hang on that line, then process events, then wait on another line. I still think you should explore the QProcess approach.

Ricardo Viana

unread,
Mar 18, 2013, 4:36:33 PM3/18/13
to python_in...@googlegroups.com
Thanks. i m trying Qprocess now.
i can get the started and finished feedback. but nothing in between... :(

i even changed from avconv to ffmpeg to see if its something w the command.
maybe its something to do with the stdout from the command line app!?!?

here is  the latest.

https://gist.github.com/RicardoViana/5190548
video_converter.png

Justin Israel

unread,
Mar 18, 2013, 5:30:10 PM3/18/13
to python_in...@googlegroups.com
Whoops, you are starting your QProcess twice:
101: self.proc.start(command)
106: QtCore.QTimer.singleShot(100, partial(self.proc.start, command))

And also you aren't making use of that partial callback...

     def dataReady(self, proc):
        # make sure you are first converting the QByteArray
        # to a string, and then stripping the string. 
        out = str(proc.readAllStandardOutput())
        self.log.append(out.strip())
     ...
     def export(self):
        ... 
        # probably dont need to save it as a member
        # but pass it a parent
        proc = QtCore.QProcess(self)
        # connect signals first
        proc.started.connect(self.started)
        proc.readyReadStandardOutput.connect(partial(self.dataReady, proc))
        proc.finished.connect(self.finished)
        # start
        proc.start(command)
    

Ricardo Viana

unread,
Mar 18, 2013, 6:53:41 PM3/18/13
to python_in...@googlegroups.com
SOLVED!! aha!! :)

https://gist.github.com/RicardoViana/5190548



It seems that both ffmpeg and avconv use the StandardError instead
of standardOutput. So i used:

out = str(proc.readAllStandardError())


and it works like a charm. Next step is to figure a way to translate this to a progress bar..


thank you very much for the help.

Justin Israel

unread,
Mar 18, 2013, 7:40:26 PM3/18/13
to python_in...@googlegroups.com

Oh cool. Good find.
For the progress bar, you would need to be getting lines from the output that actually indicate a level of progress. You would then parse that value and set the step value on the progress bar.
Progress values don't have to be 0-100. If you were running a process on 1000 frames and you get output for each frame, you can set the progress max to 1000 and just increment the steps.

Ricardo Viana

unread,
Mar 20, 2013, 1:01:30 PM3/20/13
to python_in...@googlegroups.com
Hi there!

Got the progressBar working ! see attach.

latest code:

https://gist.github.com/RicardoViana/5190548



the problem was getting the total amount of frames in the video.
i found this skimage module which seems to read that.
wonder if there is any class in qt to do that instead.

Now next step would be to have a preview of the video being encoded.
Started looking at phonon but if i run :

from PyQt4.phonon import Phonon

i get:

ImportError: No module named phonon


searched the web for a troubleshoot. unsucess!!
i have got phonon and libphonon-dev from the repositories.
but still doesn't work. i thought it would come with pyqt4 install.
I'm on xubuntu. but i also have kubuntu desktop and still nothing.

any thoughts?


Cheers
Ric
video_converter_progress.png

olheiros

unread,
Mar 20, 2013, 2:14:36 PM3/20/13
to python_in...@googlegroups.com
Got everything working fine.

Just for the record:

Phonon on linux was solved by going into synaptic
and installing:

python-qt4-phonon
Python bindings for Phonon

python-qt4-phonon-dbg
Python bindings for Phonon (debug extensions)


the later probably not necessary but just in case...

cheers all
and a big thank you for the help.

latest version:

https://gist.github.com/RicardoViana/5190548

any suggestions are welcome.

Reply all
Reply to author
Forward
0 new messages