def init(): "Initizing the environment" # Making sure temp folder is accessable. if not os.path.exists(config.temp_dir): try: os.makedirs(config.temp_dir) except (IOError, WindowsError): config.temp_dir = os.path.join( os.environ["Temp"], "iQuality" ) # if temp folder cannot be created, we should set it to default. # setting ext_bin directory if hasattr(sys, "frozen"): # the ext_bin folder will be one level upper splitted_dirs = config.ext_bin_path.split('\\') config.ext_bin_path = "\\".join(splitted_dirs[:-2] + [splitted_dirs[-1]]) log.debug("ext_bin_path: %s" % config.ext_bin_path) if not os.path.exists(config.ext_bin_path): os.makedirs(config.ext_bin_path) # Checking internet connection returncode = utils.launch_without_console('ping 8.8.8.8 -n 1').wait() if returncode > 0: log.error("Got NoInternetConnectionException()") raise NoInternetConnectionException() returncode = utils.launch_without_console( 'ping www.google.com -n 1').wait() if returncode > 0: log.error("Got NoDnsServerException()") raise NoDnsServerException()
def init(): "Initizing the environment" # Making sure temp folder is accessable. if not os.path.exists(config.temp_dir): try: os.makedirs(config.temp_dir) except (IOError, WindowsError): config.temp_dir = os.path.join(os.environ["Temp"], "iQuality") # if temp folder cannot be created, we should set it to default. # setting ext_bin directory if hasattr(sys, "frozen"): # the ext_bin folder will be one level upper splitted_dirs = config.ext_bin_path.split('\\') config.ext_bin_path = "\\".join(splitted_dirs[:-2]+[splitted_dirs[-1]]) if not os.path.exists(config.ext_bin_path): os.makedirs(config.ext_bin_path) # Checking internet connection returncode = utils.launch_without_console('ping 8.8.8.8 -n 1').wait() if returncode > 0: log.error("Got NoInternetConnectionException()") raise NoInternetConnectionException() returncode = utils.launch_without_console('ping www.google.com -n 1').wait() if returncode > 0: log.error("Got NoDnsServerException()") raise NoDnsServerException()
def FFMpeg(input, output, bitrate, bin_path=os.path.join(config.ext_bin_path, 'ffmpeg.exe'), fmt='mp3'): # Yields filesize_counter cmd = r'%s -y -i "%s" -vn -ac 2 -b:a %s -f %s "%s"' % ( bin_path, input, str(bitrate), fmt, output) log.debug("Running '%s'" % cmd) proc = utils.launch_without_console(cmd) yield 0 old_encoded_fs_counter = 0 while True: out = proc.stderr.read(54) if not out: break # size= 2930kB time=00:03:07.49 bitrate= 128.0kbits/s if 'size=' in out and 'time=' in out: encoded_fs_counter = out.split('size=')[1].split('kB')[0].strip() if encoded_fs_counter.isdigit(): encoded_fs_counter = int(encoded_fs_counter) if encoded_fs_counter > old_encoded_fs_counter: old_encoded_fs_counter = encoded_fs_counter yield encoded_fs_counter time.sleep(0.1) proc.wait()
def FFMpeg(input, output, bitrate, bin_path=os.path.join(config.ext_bin_path, 'ffmpeg.exe'), fmt='mp3'): # Yields filesize_counter cmd = r'%s -y -i "%s" -vn -ac 2 -b:a %s -f %s "%s"' % (bin_path, input, str(bitrate), fmt, output) log.debug("Running '%s'" % cmd) proc = utils.launch_without_console(cmd) yield 0 old_encoded_fs_counter = 0 while True: out = proc.stderr.read(54) if not out: break # size= 2930kB time=00:03:07.49 bitrate= 128.0kbits/s if 'size=' in out and 'time=' in out: encoded_fs_counter = out.split('size=')[1].split('kB')[0].strip() if encoded_fs_counter.isdigit(): encoded_fs_counter = int(encoded_fs_counter) if encoded_fs_counter > old_encoded_fs_counter: old_encoded_fs_counter = encoded_fs_counter yield encoded_fs_counter time.sleep(0.1) proc.wait()
def SoX(input, output, bin_path=os.path.join(config.ext_bin_path, 'sox.exe')): # Yields progress _real_input = input _real_output = output if not input.endswith('.mp3'): t_path = "%s.tmp.mp3" % input if os.path.exists(t_path): os.unlink(t_path) os.rename(input, t_path) input = t_path if not output.endswith('.mp3'): t_path = "%s.dtmp.mp3" % output if os.path.exists(t_path): os.unlink(t_path) output = t_path cmd = r'%s -S "%s" "%s" silence 1 0.1 1%% reverse silence 1 0.1 1%% reverse' % ( bin_path, input, output) log.debug("Running '%s'" % cmd) proc = utils.launch_without_console(cmd) yield 0 samples = 1 in_value = 0 out_value = 0 while True: # print out out = proc.stderr.readline() if not out: break # Duration : 00:04:24.06 = 11644870 samples = 19804.2 CDDA sectors # from PyQt4 import QtCore; import pdb; QtCore.pyqtRemoveInputHook(); pdb.set_trace() match = re.search(r"samples [=~] ([\d.]+) CDDA", out) if match: samples = float(match.group(1)) break while True: # print out out = proc.stderr.read(70) if not out: break # In:100% 00:04:23.96 [00:00:00.09] Out:11.6M [ | ] Hd:0.0 Clip:400 if 'In:' in out: t = out.split('In:')[1].split('.')[0].strip() if t.isdigit() and int(t) > in_value: in_value = int(t) # In:100% 00:04:23.96 [00:00:00.09] Out:11.6M [ | ] Hd:0.0 Clip:400 if 'Out:' in out: t = out.split('Out:')[1].split(' ')[0].strip() try: if 'k' in t: out_value = t.split('k')[0] out_value = float(out_value) * 1000 elif 'M' in t: out_value = t.split('M')[0] out_value = float(out_value) * 1000000 except: pass # print "@@@%f@@@" % out_value progress = in_value * 0.3 + (out_value / samples * 100) * 0.7 + 1 # print "samples: %s, progress: %s, out: %s" % (samples, progress, out.strip('\r')) if 0 >= progress >= 100: yield progress elif in_value > 1: yield in_value * 0.3 time.sleep(0.1) proc.wait() if _real_output != output: if os.path.exists(_real_output): os.unlink(_real_output) os.rename(output, _real_output)
def SoX(input, output, bin_path=os.path.join(config.ext_bin_path, 'sox.exe')): # Yields progress _real_input = input _real_output = output if not input.endswith('.mp3'): t_path = "%s.tmp.mp3" % input if os.path.exists(t_path): os.unlink(t_path) os.rename(input, t_path) input = t_path if not output.endswith('.mp3'): t_path = "%s.dtmp.mp3" % output if os.path.exists(t_path): os.unlink(t_path) output = t_path cmd = r'%s -S "%s" "%s" silence 1 0.1 1%% reverse silence 1 0.1 1%% reverse' % (bin_path, input, output) log.debug("Running '%s'" % cmd) proc = utils.launch_without_console(cmd) yield 0 samples = 1 in_value = 0 out_value = 0 while True: # print out out = proc.stderr.readline() if not out: break # Duration : 00:04:24.06 = 11644870 samples = 19804.2 CDDA sectors # from PyQt4 import QtCore; import pdb; QtCore.pyqtRemoveInputHook(); pdb.set_trace() match = re.search(r"samples [=~] ([\d.]+) CDDA", out) if match: samples = float(match.group(1)) break while True: # print out out = proc.stderr.read(70) if not out: break # In:100% 00:04:23.96 [00:00:00.09] Out:11.6M [ | ] Hd:0.0 Clip:400 if 'In:' in out: t = out.split('In:')[1].split('.')[0].strip() if t.isdigit() and int(t) > in_value: in_value = int(t) # In:100% 00:04:23.96 [00:00:00.09] Out:11.6M [ | ] Hd:0.0 Clip:400 if 'Out:' in out: t = out.split('Out:')[1].split(' ')[0].strip() try: if 'k' in t: out_value = t.split('k')[0] out_value = float(out_value)*1000 elif 'M' in t: out_value = t.split('M')[0] out_value = float(out_value)*1000000 except: pass # print "@@@%f@@@" % out_value progress = in_value*0.3+(out_value/samples*100)*0.7+1 # print "samples: %s, progress: %s, out: %s" % (samples, progress, out.strip('\r')) if 0 >= progress >= 100: yield progress elif in_value>1: yield in_value*0.3 time.sleep(0.1) proc.wait() if _real_output != output: if os.path.exists(_real_output): os.unlink(_real_output) os.rename(output, _real_output)
def run(self): # Called by Qt once the thread environment has been set up. url = self.songObj.url filesize = self.songObj.filesize audio_path = r"%s\%s" % (self.dl_dir, self.songObj.GetProperFilename('mp3')) video_path = r"%s\%s" % (self.dl_dir, self.songObj.GetProperFilename()) dest_audio_path = r"%s\%s" % (config.temp_dir, "%s.mp3" % utils.get_rand_string()) if not self.isMultimediaFile: dest_path = r"%s\%s" % (config.temp_dir, utils.get_rand_string()) elif self.songObj.ext == "mp3": dest_path = dest_audio_path else: # video dest_path = r"%s\%s" % (config.temp_dir, "%s.vid" % utils.get_rand_string()) dl_obj = Main.SmartDL(url, dest_path, logger=log) dl_obj.start() self.dl_obj = dl_obj while not dl_obj.isFinished(): if dl_obj.status == 'combining': self.status.emit(tr("Combining Parts...")) break self.downloadProgress.emit(int(dl_obj.get_progress()*100), dl_obj.get_speed(), dl_obj.get_eta(), dl_obj.get_downloaded_size(), filesize) time.sleep(0.1) while not dl_obj.isFinished(): # if we were breaking the last loop, we are waiting for # parts to get combined. we shall wait. time.sleep(0.1) if dl_obj._failed: log.error("Got DownloadFailedException() for %s" % url) self.error.emit(Main.SmartDL.DownloadFailedException()) self.terminate() return self.downloadProgress.emit(100, dl_obj.get_speed(), dl_obj.get_eta(), filesize, filesize) if self.isVideo: dest_video_path = dest_path t1 = time.time() if config.downloadAudio: # if we want an audio file log.debug("Encoding Audio...") self.status.emit(tr("Encoding Audio...")) cmd = r'bin\ffmpeg -y -i "%s" -vn -ac 2 -b:a %d -f mp3 "%s"' % (dest_video_path, config.youtube_audio_bitrates[self.songObj.video_itag.quality], dest_audio_path) log.debug("Running '%s'" % cmd) est_final_filesize = self.songObj.final_filesize print "Encoding: %s (%.2f MB) to %s" % (dest_audio_path, est_final_filesize / 1024.0 / 1024.0, self.dl_dir) self.encProgress.emit(0) proc = utils.launch_without_console(cmd) old_encoded_fs_counter = 0 while True: out = proc.stderr.read(54) if not out: break # size= 2930kB time=00:03:07.49 bitrate= 128.0kbits/s if 'size=' in out and 'time=' in out: encoded_fs_counter = out.split('size=')[1].split('kB')[0].strip() if encoded_fs_counter.isdigit(): encoded_fs_counter = int(encoded_fs_counter) if encoded_fs_counter > old_encoded_fs_counter: status = r"Encoding: %.2f MB / %.2f MB %s [%3.2f%%]" % (encoded_fs_counter / 1024.0, est_final_filesize / 1024.0**2, utils.progress_bar(1.0*encoded_fs_counter*1024/est_final_filesize) , encoded_fs_counter*1024 * 100.0 / est_final_filesize) status = status + chr(8)*(len(status)+1) print status, self.encProgress.emit(int(encoded_fs_counter*1024 * 100.0 / est_final_filesize)) old_encoded_fs_counter = encoded_fs_counter time.sleep(0.1) self.encProgress.emit(100) proc.wait() t2 = time.time() self.encode_time += t2-t1 if not config.downloadVideo: log.debug("Removing %s..." % dest_path) os.unlink(dest_path) if config.downloadAudio and config.trimSilence: t1 = time.time() log.debug("Trimming Silence...") self.status.emit(tr("Trimming Silence from edges...")) temp_audio_trimmed_path = "%s.tmp.mp3" % dest_audio_path if os.path.exists(temp_audio_trimmed_path): os.unlink(temp_audio_trimmed_path) os.rename(dest_audio_path, temp_audio_trimmed_path) cmd = r'bin\sox -S "%s" "%s" silence 1 0.1 1%% reverse silence 1 0.1 1%% reverse' % (temp_audio_trimmed_path, dest_audio_path) log.debug("Running '%s'" % cmd) est_final_filesize = self.songObj.final_filesize print "Trimming Silence: %s (%.2f MB) to %s" % (dest_audio_path, est_final_filesize / 1024.0**2, self.dl_dir) self.encProgress.emit(0) proc = utils.launch_without_console(cmd) samples = 1 in_value = 0 out_value = 0 while True: # print out out = proc.stderr.read(70) if not out: break # Duration : 00:04:24.06 = 11644870 samples = 19804.2 CDDA sectors if 'samples =' in out: samples = out.split('samples')[0].split('=')[-1].strip() if samples.isdigit(): samples = int(samples) # In:100% 00:04:23.96 [00:00:00.09] Out:11.6M [ | ] Hd:0.0 Clip:400 if 'In:' in out: t = out.split('In:')[1].split('.')[0].strip() if t.isdigit() and int(t) > in_value: in_value = int(t) # In:100% 00:04:23.96 [00:00:00.09] Out:11.6M [ | ] Hd:0.0 Clip:400 if 'Out:' in out: t = out.split('Out:')[1].split(' ')[0].strip() try: if 'k' in t: out_value = t.split('k')[0] out_value = float(out_value)*1000 elif 'M' in t: out_value = t.split('M')[0] out_value = float(out_value)*1000000 except: pass progress = in_value*0.3+(out_value/samples*100)*0.7+1 status = r"Trimming Silence: %s" % utils.progress_bar(progress/100.0) status = status + chr(8)*(len(status)+1) print status, self.encProgress.emit(progress) time.sleep(0.1) self.encProgress.emit(100) proc.wait() t2 = time.time() self.encode_time += t2-t1 if not os.path.exists(dest_audio_path): log.error('SoX failed: %s' % out) log.debug("Copying Files...") self.status.emit(tr("Copying Files...")) if self.isVideo: # IMPROVE: this crashes when a video is running in media player, os.unlink removes it, but it is still running in media player. if config.downloadAudio: log.debug("Moving %s to %s" % (dest_audio_path, audio_path)) shutil.move(dest_audio_path, audio_path) if config.downloadVideo: log.debug("Moving %s to %s" % (dest_video_path, video_path)) shutil.move(dest_video_path, video_path) if self.isAudio: log.debug("Moving %s to %s" % (dest_path, audio_path)) shutil.move(dest_path, audio_path) dl_time = dl_obj.get_dl_time() dl_time_s = int(dl_time)%60 dl_time_m = int(dl_time)/60 if filesize/dl_time/1024**2 > 1: # If dlRate is in MBs if dl_time_m: stats_str = tr('Download: %d:%.2d (%.2f MB/s)') % (dl_time_m, dl_time_s, filesize/dl_time/1024**2) else: stats_str = tr('Download: %ds (%.2f MB/s)') % (dl_time, filesize/dl_time/1024**2) else: if dl_time_m: stats_str = tr('Download: %d:%.2d (%.2f KB/s)') % (dl_time_m, dl_time_s, filesize/dl_time/1024) else: stats_str = tr('Download: %ds (%.2f KB/s)') % (dl_time, filesize/dl_time/1024) if self.encode_time: stats_str += tr('; Encoded: %ds') % self.encode_time self.status.emit(stats_str)