Assign the path of a file to a variable and then use it in FFmpeg

1

I'm trying to make a program in Python and PyQt for video encoding and I have a problem.

I do not know how to get a variable with the path of a file and then send it to FFmpeg .

I have built a basic window with three buttons, one to choose the file, another to choose the path of the outgoing file and another to start the encoding. To this last one now I only ask you to print the variable that has the path of the file to be able to see and verify that this variable is fine.

If I execute the first button, b1_clicked() , it works fine and even prints the path of the file, but if I execute the third button also to print that variable, it gives error:

  

Traceback (most recent call last):

     

File "/home/salva/PycharmProjects/test1/encoder3.py", line 40, in b3_clicked

     

print (fileinput)

     

NameError: name 'fileinput' is not defined

I need that variable to be able to send FFmpeg the video that I want to encode (I'll see how).

On that button 3 FFmpeg will act later when you know how to do it.

This is what I'm doing for now before I start with FFmpeg : (I'm updating)

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import subprocess

fileinput = ''

def window():
   app = QApplication(sys.argv)
   win = QDialog()
   b1 = QPushButton(win)
   b1.setText("Select file")
   b1.move(50,20)
   b1.clicked.connect(b1_clicked)

   b2 = QPushButton(win)
   b2.setText("Output file")
   b2.move(50,50)
   b2.clicked.connect(b2_clicked)

   b3 = QPushButton(win)
   b3.setText("Encode!")
   b3.move(50,80)
   b3.clicked.connect(b3_clicked)

   win.setGeometry(100,100,200,200)
   win.setWindowTitle("Encoder")
   win.show()
   sys.exit(app.exec_())

def b1_clicked():
   fileinput = QFileDialog.getOpenFileName(None, "Selecciona archivo a convertir", "/home/salva", "video files (*.avi *.mkv *.mp4 *.mov *.mpg);; All files (*)")
   print (fileinput)


def b2_clicked():
   filesaved = QFileDialog.getSaveFileName(None, "Archivo de salida", "/home/salva", "video files (*.avi *.mkv *.mp4 *.mov *.mpg);; All files (*)")
   print (filesaved)


def b3_clicked():
   print (fileinput)


if __name__ == '__main__':
   window()
    
asked by salvaestudio 01.05.2017 в 19:01
source

1 answer

1

Because fileinput is only known by b1_clicked() , since it is local. That is, a local variable only exists in the scope of the function (actually the space or namespace ) that creates it. Therefore, said variable does not exist in b3_clicked() (the correct term is not in its scope ).

You have two options.

  • You pass this variable in some way to the function b3_clicked() or
  • Create the variable fileinput in the global space, so that it is available to all functions and methods:
  • Example:

    # tus imports 
    
    fileinput = ''   # Creas una variable global, vacía
    
    # la función window()
    
    def b1_clicked():
       global fileinput 
       fileinput = QFileDialog.getOpenFileName(None, "Selecciona archivo a convertir", "/home/salva", "video files (*.avi *.mkv *.mp4 *.mov *.mpg);; All files (*)")
       print (fileinput)  # Usas la variable global
    
    def b3_clicked():
       # Haces algo si 'fileinput' está vacía
       print (fileinput)
    
        
    answered by 01.05.2017 / 19:18
    source