# -*- coding: utf-8 -*-

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

class VideosToMp3(QtGui.QWidget):
  def __init__(self, parent = None):
    super(VideosToMp3, self).__init__(parent)
    ''' Labels and GUI ''' 
    extension = QtGui.QWidget()
    download_label = QtGui.QLabel("Enter YouTube video URL:")
    self.download_line_edit = QtGui.QLineEdit()
    file_label = QtGui.QLabel("Enter file name (optional):")
    self.file_line_edit = QtGui.QLineEdit()  
    mp3_filename_label = QtGui.QLabel("MP3 file name:")
    self.filename_line_edit = QtGui.QLineEdit()
    download_button = QtGui.QPushButton("Download in URL")
    download_file_button = QtGui.QPushButton("Download in file")  
    cancel_button = QtGui.QPushButton("Cancel")  
    spacer = QtGui.QSpacerItem(10, 20, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
    gridLayout = QtGui.QGridLayout()
    gridLayout.addWidget(download_label, 0, 0)
    gridLayout.addWidget(self.download_line_edit, 0, 1)
    gridLayout.addWidget(mp3_filename_label, 1, 0)
    gridLayout.addWidget(self.filename_line_edit, 1, 1)
    gridLayout.addWidget(file_label, 2, 0)
    gridLayout.addWidget(self.file_line_edit, 2, 1)  
    gridLayout.addWidget(download_button, 3, 0)  
    gridLayout.addWidget(cancel_button, 3, 1)  
    gridLayout.addWidget(download_file_button, 4, 0) 
    gridLayout.addItem(spacer)

    mainLayout = QtGui.QVBoxLayout()
    mainLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
    mainLayout.addLayout(gridLayout)
    self.setLayout(mainLayout)
    
    ''' Connect '''
    #self.connect(download_button, SIGNAL('clicked()'), self.download_video)
    self.connect(cancel_button, SIGNAL('clicked()'), self.close)  
    self.connect(download_file_button, SIGNAL('clicked()'), self.download_videos)

class Download_in_url():
  def __init__(self):
    def download_video_in_url(url):
      os.system('python youtube-dl' + url)  

class Download_in_file():
  def __init__(self):
    print "Creating folder 'music'"
    os.system('mkdir -p music')
    download_videos()

    def check_file_format(file_name): 
	if file_name.endswith('.flv'):
	    return str(file_name)
	else:
	    return str(file_name + '.flv')

    def download_videos(): 
	list_videos = []
	list_music = []
	try:
	  input_file = open('videos.txt')
	  for line in input_file:
	      (name, address) = line.split('||') 	
	      name = name.replace(' ', '_')  		
	      if name.startswith('[olemas]') or os.path.exists(str(os.getcwd()) + '/music/' + name.rstrip('.flv') + '.mp3'):
		  print name, "is exists! Ignoring!"
	      else:
		  check_name = check_file_format(name)
		  download = 'python youtube-dl -o ' + check_name + ' ' + address
		  convert = 'ffmpeg -i ' + check_name + ' -y -f mp3 -vn -acodec copy ' + check_name.rstrip('.flv') + '.mp3'
		  list_videos.append(download)
		  list_music.append(convert)
	except IOError:
	    pass

	print "Downloading all videos"
	for download_videos in list_videos:
	    os.system(download_videos)

	print "Creating all mp3's"
	for create_music in list_music:
	    os.system(create_music)

print "Moving music to directory 'music'"
os.system('mv *.mp3 ' + os.getcwd() + '/music')

print "Removing unused videos"
os.system('rm *.flv')     
    
def main():
  aplication = QtGui.QApplication(sys.argv)
  window = VideosToMp3()
  window.setWindowTitle("YouTube To MP3 Downloader")
     
  window.show()
  program = aplication.exec_()
  sys.exit(program)    

if __name__ == "__main__":
  main()

Moving music to directory 'music'
mv: stat `*.mp3' ei õnnestu: No such file or directory
Removing unused videos
rm: `*.flv' ei saa kustutada: No such file or directory
Traceback (most recent call last):
File "DownloadYouTubeVideos.py", line 105, in <module>
main()
File "DownloadYouTubeVideos.py", line 97, in main
window = VideosToMp3()
File "DownloadYouTubeVideos.py", line 43, in __init__
self.connect(download_file_button, SIGNAL('clicked()'), self.download_videos)
AttributeError: 'VideosToMp3' object has no attribute 'download_videos'


How to use the classes and Qt threads, because the user interface is not running properly at the time of when download videos and converts them to mp3.

Hi,

I think that before you go into threads you should maybe explain your idea in a few lines.
For now, the error you receive is due to the fact that the slot you've connected to the download_file_button is not a method of the VideosToMp3 class, but of Download_in_File class. That will not work. Also, this Download_in_File clas is never instantiated.

Please explain a bit more so we can help you dig out the solution, and give advice for the way you should implement a thread if it proves you need one.

# -*- coding: utf-8 -*-

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *  
import sys
import os

class Download_Videos_In_File(QtCore.QThread):
  def __init__(self):
    QtCore.QThread.__init__(self)
  
  def run(self):

  #  def download_video_in_url(self):
  #  os.system('python youtube-dl ' + str(self.download_url_button.text()))  

    print "Creating folder 'music'"
    os.system('mkdir -p music')
    self.download_videos()

  def download_videos(self): 
    list_videos = []
    list_music = []
    try:
      input_file = open('videos.txt')
      for line in input_file:
	(name, address) = line.split('||') 	
	name = name.replace(' ', '_')  		
	if name.startswith('[olemas]') or os.path.exists(str(os.getcwd()) + '/music/' + name.rstrip('.flv') + '.mp3'):
	  print name, "is exists! Ignoring!"
	else:
	  check_name = self.check_file_format(name)
	  download = 'python youtube-dl -o ' + check_name + ' ' + address
	  convert = 'ffmpeg -i ' + check_name + ' -y -f mp3 -vn -acodec copy ' + check_name.rstrip('.flv') + '.mp3'
	  list_videos.append(download)
	  list_music.append(convert)
    except IOError:
      pass
      
    print "Downloading all videos"
    for download_videos in list_videos:
      os.system(download_videos)

    print "Creating all mp3's"
    for create_music in list_music:
      os.system(create_music)

  def check_file_format(self, file_name): 
    if file_name.endswith('.flv'):
      return str(file_name)
    else:
      return str(file_name + '.flv')

class UI_Widget(QtGui.QWidget):
  def __init__(self, parent = None):
    super(UI_Widget, self).__init__(parent)
    ''' 
    Labels, line edits and GUI 
    ''' 
    extension = QtGui.QWidget()
    download_url_label = QtGui.QLabel("Enter YouTube video URL:")
    self.download_url_line_edit = QtGui.QLineEdit()

    download_file_label = QtGui.QLabel("Enter file name (optional):")
    self.download_file_line_edit = QtGui.QLineEdit()
  
    mp3_filename_label = QtGui.QLabel("MP3 file name:")
    self.mp3_line_edit = QtGui.QLineEdit()
    '''
    Buttons
    '''
    self.download_url_button = QtGui.QPushButton("Download in URL")
    self.download_file_button = QtGui.QPushButton("Download in file")  
    self.cancel_button = QtGui.QPushButton("Cancel")  

    spacer = QtGui.QSpacerItem(10, 20, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
    ''' 
    Layout
    '''
    gridLayout = QtGui.QGridLayout()
    gridLayout.addWidget(download_url_label, 0, 0)
    gridLayout.addWidget(self.download_url_line_edit, 0, 1)

    gridLayout.addWidget(mp3_filename_label, 1, 0)
    gridLayout.addWidget(self.mp3_line_edit, 1, 1)

    gridLayout.addWidget(download_file_label, 2, 0)
    gridLayout.addWidget(self.download_file_line_edit, 2, 1)
  
    gridLayout.addWidget(self.download_url_button, 3, 0)  
    gridLayout.addWidget(self.cancel_button, 3, 1)  
    gridLayout.addWidget(self.download_file_button, 4, 0)
 
    gridLayout.addItem(spacer)
    mainLayout = QtGui.QVBoxLayout()
    mainLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
    mainLayout.addLayout(gridLayout)
    self.setLayout(mainLayout)
    '''
    Threads
    '''
    self.download = Download_Videos_In_File()
    ''' 
    Connect 
    '''
    self.connect(self.cancel_button, SIGNAL('clicked()'), self.close)  
    self.connect(self.download_file_button, SIGNAL('clicked()'), self.download.start)
  
def main():
  aplication = QtGui.QApplication(sys.argv)
  window = UI_Widget()
  window.setWindowTitle("YouTube To MP3 Downloader")
  window.show()
  program = aplication.exec_()
  
  try:
    print "Moving music to directory 'music'"
    os.system('mv *.mp3 ' + os.getcwd() + '/music')
    print "Removing unused videos"
    os.system('rm *.flv')
  except:
    pass

  sys.exit(program)    

if __name__ == "__main__":
  main()

My goal is that the user enters either a URL or file (where is the video address and name of the file), then my program download videos and converts them mp3, however, as the interface to be "usable".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.