Skip to content

Instantly share code, notes, and snippets.

@RicardoViana
Last active December 13, 2017 11:30
Show Gist options
  • Save RicardoViana/5190548 to your computer and use it in GitHub Desktop.
Save RicardoViana/5190548 to your computer and use it in GitHub Desktop.
Video converter for Linux. PYQT4, phonon, avconv
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.phonon import Phonon
from functools import partial
import skimage.io as io
class Export(QtGui.QWidget):
def __init__(self):
super(Export, self).__init__()
self.initUI()
def initUI(self):
self.setStyleSheet("background-color:#333333;color:#FFFFFF")
layout=QtGui.QVBoxLayout()
self.blackStyle="background-color:#000000"
self.greyStyle="background-color:#666666;height:35px;border-width: 0px; border-style: solid"
self.whiteStyle="background-color:#FFFFFF"
self.redStyle="background-color:red ; color:white; height:35px;border-style:solid;border-width:0px"
self.inputFilePath = QtGui.QLabel()
self.setGeometry(300, 300, 390, 600)
self.setWindowTitle('Video Converter')
openFile = QtGui.QPushButton('load video')
openFile.clicked.connect(self.showDialog)
openFile.setStyleSheet(self.redStyle)
self.bitRateText=QtGui.QLabel('Set Bitrate')
self.bitRatesList=["1500k","3500k","6000k","8000k","12000k","25000k","50000k"]
self.bitRate=QtGui.QComboBox()
self.bitRate.setStyleSheet(self.greyStyle)
self.bitRate.addItems(self.bitRatesList)
self.setSizeText=QtGui.QLabel('Set Size')
self.sizeSel= QtGui.QComboBox()
self.sizeSel.setStyleSheet(self.greyStyle)
self.sizeList=["640x480","800x600","1024x768","720x576","768x576","960x540",
"1280x720","1920x1080","2048x1152"]
self.sizeSel.addItems(self.sizeList)
self.formatsLabel=QtGui.QLabel("select extension:")
self.formatList=[".mp4",".wmv",".mpg",".avi"]
self.formatOption=QtGui.QComboBox()
self.formatOption.setStyleSheet(self.greyStyle)
self.formatOption.addItems(self.formatList)
saveFile = QtGui.QPushButton('Export')
saveFile.setStyleSheet(self.redStyle)
saveFile.clicked.connect(self.export)
self.log=QtGui.QTextBrowser()
self.log.setStyleSheet(self.blackStyle)
self.progress=QtGui.QProgressBar()
layout.addWidget(self.inputFilePath)
layout.addWidget(openFile)
layout.addWidget(self.bitRateText)
layout.addWidget(self.bitRate)
layout.addWidget(self.setSizeText)
layout.addWidget(self.sizeSel)
layout.addWidget(self.formatsLabel)
layout.addWidget(self.formatOption)
layout.addWidget(saveFile)
layout.addWidget(self.progress)
layout.addWidget(self.log)
self.setLayout(layout)
#video widget
self.playerWidget=QtGui.QWidget()
self.playerWidget.setWindowTitle("Video preview")
self.playerWidget.setGeometry(600,400,640,480)
self.player=Phonon.VideoPlayer(parent=self.playerWidget)
self.videoWidget=self.player.videoWidget()
self.outputFile=""
self.show()
self.raise_()
def started(self):
self.log.append("started")
self.v=io.Video(self.inputFile)
self.progress.setRange(0,int(self.v.frame_count()))
self.playerWidget.show()
self.player.seek(2)
self.player.pause()
def dataReady(self,proc):
out = str(proc.readAllStandardError())
self.frame=out[:12]
if "frame" in self.frame:
self.log.append(self.frame)
frameNumber=self.frame.split("=")[1]
self.progress.setValue(int(frameNumber))
print frameNumber
self.player.seek(int(frameNumber)*40)
def finished(self):
self.progress.setValue(int(self.v.frame_count()))
self.log.append("finished")
self.progress.reset()
self.playerWidget.hide()
def export(self):
if self.outputFile == "" :
self.log.clear()
self.log.append("Please load a video file!")
else:
command="avconv -i %s -y -b %s -s %s %s" %(self.inputFile,self.bitRatesList[self.bitRate.currentIndex()],
self.sizeList[self.sizeSel.currentIndex()],str(self.outputFile+self.formatList[self.formatOption.currentIndex()]))
self.proc = QtCore.QProcess(self)
self.proc.started.connect(self.started)
self.proc.readyReadStandardError.connect(partial(self.dataReady, self.proc))
self.proc.finished.connect(self.finished)
self.proc.start(command)
def showDialog(self):
self.inputFile= QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'/home')
self.outputFile=self.inputFile.split(".")[0]
self.inputFilePath.setText(self.inputFile.split("/")[-1])
media_source= Phonon.MediaSource(self.inputFile)
self.player.load(media_source)
def main():
app = QtGui.QApplication(sys.argv)
app.setStyle("Plastique")
ex = Export()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
@mrgloom
Copy link

mrgloom commented Apr 6, 2016

Seems when I run it under debugger, it not entered in started function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment