def mp4(): # env my_env = os.environ.copy() my_env["LANG"] = "C" for count, filename in enumerate(filenames): # analyse file cutter = Cut(self.app, self.gui) fps, dar, sar, max_frames, ac3_stream, error = cutter.analyse_mediafile(filename) if fps == None: self.errors[filename] = error continue # mkvmerge pass yield 0, count self.progress = 0 if os.path.splitext(filename)[1] != ".mkv": mkvpass_file = fileoperations.make_unique_filename(os.path.splitext(filename)[0] + "_remux.mkv") try: p = subprocess.Popen( [ self.app.config.get_program("mkvmerge"), "--ui-language", "en_US", "-o", mkvpass_file, filename, ], stdout=subprocess.PIPE, env=my_env, ) except OSError: self.errors[filename] = "MKVmerge wurde nicht gefunden!" continue p.stdout.readline() line = "" while p.poll() == None: # read progress from stdout char = p.stdout.read(1) line += char progress = "" if char == ":": if "Error" in line or "Warning" in line: break while char != "%": char = p.stdout.read(1) progress += char try: self.progress = int(progress.strip(" %")) yield 4, self.progress except ValueError: pass exit_code = p.poll() if exit_code == 0 or exit_code == 1: pass else: error = p.stdout.readline() if os.path.exists(mkvpass_file): fileoperations.remove_file(mkvpass_file) try: error = error.split(":")[1] except IndexError: pass if "unknown type" in error: error = "Datei konnte nicht gelesen werden." self.errors[filename] = error continue else: mkvpass_file = filename # norm volume ausrechnen yield 5, count if self.Config["NormalizeAudio"] and self.Config["EncodeAudioToAAC"]: vol, error = self.get_norm_volume(filename) else: vol = 1.0 # ffmpeg pass yield 1, count self.progress = 0 ffmpegpass_file = fileoperations.make_unique_filename(os.path.splitext(filename)[0] + "_remux.mp4") if self.Config["EncodeAudioToAAC"]: if self.Config["EncodeOnlyFirstAudioToAAC"]: aacaudiostreams = "-c:a:0" else: aacaudiostreams = "-c:a" # convert first audio stream to aac ffmpeg = self.app.config.get_program("ffmpeg") if "nonfree" in ffmpeg: # nonfree ffmpeg version with fdk support available audiocodec = [ "-c:a", "copy", aacaudiostreams, "libfdk_aac", "-flags", "+qscale", "-profile:a:0", "aac_low", "-global_quality", "5", "-afterburner", "1", ] else: # only gpl version of ffmpeg available -> use standard aac codec audiocodec = [ "-c:a", "copy", aacaudiostreams, "aac", "-strict", "-2", "-profile:a:0", "aac_low", "-ab", "192k", "-cutoff", "18000", ] else: # only copy audio ffmpeg = path.get_tools_path("intern-ffmpeg") audiocodec = ["-c:a", "copy"] if self.Config["DownMixStereo"] and self.Config["EncodeAudioToAAC"]: audiocodec.extend(["-ac:0", "2"]) if ac3_stream == None: # no ac3 stream found - all streams are muxed map = ["-map", "0"] else: if self.Config["RemoveOtherAudioStreamsThanAC3"]: # mux only video and ac3 stream map = ["-map", "0:v", "-map", ac3_stream] else: map = ["-map", "0"] args = [ ffmpeg, "-loglevel", "info", "-y", "-drc_scale", "1.0", "-i", mkvpass_file, "-vcodec", "copy", "-af", "volume=volume=" + str(vol), "-vsync", "1", "-async", "1000", "-dts_delta_threshold", "100", "-vf", "fps=" + str(fps), ffmpegpass_file, ] map.extend(audiocodec) args[8:8] = map try: p = subprocess.Popen(args, stderr=subprocess.PIPE, universal_newlines=True) except OSError: self.errors[filename] = "FFMPEG (intern) wurde nicht gefunden!" if os.path.exists(mkvpass_file) and filename != mkvpass_file: fileoperations.remove_file(mkvpass_file) continue yield 4, 0 line = "" infos_match = re.compile(r"frame=\ {0,1}(\d{1,})") while p.poll() == None: line = p.stderr.readline() m = re.search(infos_match, line) if m and max_frames != 0: next = float(float(m.group(1)) / float(max_frames)) * 100 if next > self.progress: self.progress = next yield 4, self.progress else: pass exit_code = p.poll() if os.path.exists(mkvpass_file) and filename != mkvpass_file: fileoperations.remove_file(mkvpass_file) if exit_code == 0: if self.Config["DumpAVIs"]: yield 3, self.success new_filename = os.path.join( self.app.config.get("general", "folder_trash_avis"), os.path.basename(filename) ) if os.path.exists(new_filename): fileoperations.remove_file(new_filename) fileoperations.move_file(filename, self.app.config.get("general", "folder_trash_avis")) else: self.errors[filename] = "Fehler beim Erzeugen der MP4 Datei durch FFMPEG" if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) continue # mp4box - last turn self.progress = 0 mp4boxpass_file = fileoperations.make_unique_filename(os.path.splitext(filename)[0] + ".mp4") if self.Config["DontOptimizeMP4"]: os.rename(ffmpegpass_file, mp4boxpass_file) self.success += 1 continue yield 2, count try: p = subprocess.Popen( [ self.app.config.get_program("mp4box"), "-keep-all", "-new", "-packed", "-fps", str(fps), "-add", ffmpegpass_file, mp4boxpass_file, ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) except OSError: self.errors[filename] = "MP4Box (intern) wurde nicht gefunden!" if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) continue yield 4, 0 infos_match = re.compile(r".*\((\d{2,})\/\d{2,}\).*") while p.poll() == None: line = p.stdout.read(60) m = re.search(infos_match, line) if m: self.progress = int(m.group(1)) yield 4, self.progress if "Importing" in line: yield 2, count elif "Writing" in line: yield 6, count else: pass exit_code = p.poll() if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) if exit_code == 0: self.success += 1 else: self.errors[filename] = "Fehler beim Erzeugen der MP4 Datei durch MP4Box"
def mp4(): # env my_env = os.environ.copy() my_env["LANG"] = "C" for count, filename in enumerate(filenames): # analyse file cutter = Cut(self.app, self.gui) fps, dar, sar, max_frames, ac3_stream, error = cutter.analyse_mediafile( filename) if fps == None: self.errors[filename] = error continue # mkvmerge pass yield 0, count self.progress = 0 if os.path.splitext(filename)[1] != '.mkv': mkvpass_file = fileoperations.make_unique_filename( os.path.splitext(filename)[0] + "_remux.mkv") try: p = subprocess.Popen([ self.app.config.get_program('mkvmerge'), '--ui-language', 'en_US', "-o", mkvpass_file, filename ], stdout=subprocess.PIPE, env=my_env) except OSError: self.errors[ filename] = "MKVmerge wurde nicht gefunden!" continue p.stdout.readline() line = "" while p.poll() == None: # read progress from stdout char = p.stdout.read(1) line += char.decode('utf-8') progress = '' if char == ':': if "Error" in line or "Warning" in line: break while char != '%': char = p.stdout.read(1) progress += char try: self.progress = int(progress.strip(' %')) yield 4, self.progress except ValueError: pass exit_code = p.poll() if exit_code == 0 or exit_code == 1: pass else: error = p.stdout.readline() if os.path.exists(mkvpass_file): fileoperations.remove_file(mkvpass_file) try: error = error.split(":")[1] except IndexError: pass if "unknown type" in error: error = "Datei konnte nicht gelesen werden." self.errors[filename] = error continue else: mkvpass_file = filename # norm volume ausrechnen yield 5, count if self.Config['NormalizeAudio'] and self.Config[ 'EncodeAudioToAAC']: vol, error = self.get_norm_volume(filename) else: vol = 1.0 # ffmpeg pass yield 1, count self.progress = 0 ffmpegpass_file = fileoperations.make_unique_filename( os.path.splitext(filename)[0] + "_remux.mp4") if self.Config['EncodeAudioToAAC']: if self.Config['EncodeOnlyFirstAudioToAAC']: aacaudiostreams = '-c:a:0' else: aacaudiostreams = '-c:a' # convert first audio stream to aac ffmpeg = self.app.config.get_program('ffmpeg') if 'nonfree' in ffmpeg: # nonfree ffmpeg version with fdk support available audiocodec = [ '-c:a', 'copy', aacaudiostreams, 'libfdk_aac', '-flags', '+qscale', '-profile:a:0', 'aac_low', '-global_quality', '5', '-afterburner', '1' ] else: # only gpl version of ffmpeg available -> use standard aac codec audiocodec = [ '-c:a', 'copy', aacaudiostreams, 'aac', '-strict', '-2', '-profile:a:0', 'aac_low', '-ab', '192k', '-cutoff', '18000' ] else: # only copy audio ffmpeg = path.get_tools_path('intern-ffmpeg') audiocodec = ['-c:a', 'copy'] if self.Config['DownMixStereo'] and self.Config[ 'EncodeAudioToAAC']: audiocodec.extend(['-ac:0', '2']) if ac3_stream == None: # no ac3 stream found - all streams are muxed map = ['-map', '0'] else: if self.Config['RemoveOtherAudioStreamsThanAC3']: # mux only video and ac3 stream map = ['-map', '0:v', '-map', ac3_stream] else: map = ['-map', '0'] args = [ ffmpeg, "-loglevel", "info", "-y", "-drc_scale", "1.0", "-i", mkvpass_file, "-vcodec", "copy", '-af', 'volume=volume=' + str(vol), "-vsync", "1", '-async', '1000', "-dts_delta_threshold", "100", "-vf", "fps=" + str(fps), ffmpegpass_file ] map.extend(audiocodec) args[8:8] = map try: p = subprocess.Popen(args, stderr=subprocess.PIPE, universal_newlines=True) except OSError: self.errors[ filename] = "FFMPEG (intern) wurde nicht gefunden!" if os.path.exists( mkvpass_file) and filename != mkvpass_file: fileoperations.remove_file(mkvpass_file) continue yield 4, 0 line = "" infos_match = re.compile(r"frame=\ {0,1}(\d{1,})") while p.poll() == None: line = p.stderr.readline() m = re.search(infos_match, line) if m and max_frames != 0: next = float( float(m.group(1)) / float(max_frames)) * 100 if next > self.progress: self.progress = next yield 4, self.progress else: pass exit_code = p.poll() if os.path.exists(mkvpass_file) and filename != mkvpass_file: fileoperations.remove_file(mkvpass_file) if exit_code == 0: if self.Config['DumpAVIs']: yield 3, self.success new_filename = os.path.join( self.app.config.get('general', 'folder_trash_avis'), os.path.basename(filename)) if os.path.exists(new_filename): fileoperations.remove_file(new_filename) fileoperations.move_file( filename, self.app.config.get('general', 'folder_trash_avis')) else: self.errors[ filename] = "Fehler beim Erzeugen der MP4 Datei durch FFMPEG" if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) continue # mp4box - last turn self.progress = 0 mp4boxpass_file = fileoperations.make_unique_filename( os.path.splitext(filename)[0] + ".mp4") if self.Config['DontOptimizeMP4']: os.rename(ffmpegpass_file, mp4boxpass_file) self.success += 1 continue yield 2, count try: p = subprocess.Popen([ self.app.config.get_program('mp4box'), "-keep-all", "-new", "-packed", "-fps", str(fps), "-add", ffmpegpass_file, mp4boxpass_file ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) except OSError: self.errors[ filename] = "MP4Box (intern) wurde nicht gefunden!" if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) continue yield 4, 0 infos_match = re.compile(r".*\((\d{2,})\/\d{2,}\).*") while p.poll() == None: line = p.stdout.read(60) line = line.decode('utf-8') m = re.search(infos_match, line) if m: self.progress = int(m.group(1)) yield 4, self.progress if 'Importing' in line: yield 2, count elif 'Writing' in line: yield 6, count else: pass exit_code = p.poll() if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) if exit_code == 0: self.success += 1 else: self.errors[ filename] = "Fehler beim Erzeugen der MP4 Datei durch MP4Box"
def __init__(self, app, gui): Cut.__init__(self, app, gui) self.update_list = True self.app = app self.config = app.config self.gui = gui
def mkvmerge(): # env my_env = os.environ.copy() my_env["LANG"] = "C" for count, filename in enumerate(filenames): yield 0, count yield 3, 0 self.progress = 0 #analyse file cutter = Cut(self.app, self.gui) fps, dar, sar, max_frames, ac3_stream, error = cutter.analyse_mediafile( filename) if fps == None: self.errors[filename] = error continue # encode aac with ffmpeg if self.Config['EncodeAudioToAAC']: #norm volume ausrechnen yield 5, count if self.Config['NormalizeAudio'] and self.Config[ 'EncodeAudioToAAC']: vol, error = self.get_norm_volume(filename) else: vol = 1.0 # ffmpeg pass yield 1, count self.progress = 0 ffmpegpass_file = fileoperations.make_unique_filename( os.path.splitext(filename)[0] + "_remux.mkv") # convert first audio stream to aac if self.Config['EncodeOnlyFirstAudioToAAC']: aacaudiostreams = '-c:a:0' else: aacaudiostreams = '-c:a' # convert first audio stream to aac ffmpeg = self.app.config.get_program('ffmpeg') if 'nonfree' in ffmpeg: # nonfree ffmpeg version with fdk support available audiocodec = [ '-c:a', 'copy', aacaudiostreams, 'libfdk_aac', '-flags', '+qscale', '-profile:a:0', 'aac_low', '-global_quality', '5', '-afterburner', '1' ] else: # only gpl version of ffmpeg available -> use standard aac codec audiocodec = [ '-c:a', 'copy', aacaudiostreams, 'aac', '-strict', '-2', '-profile:a:0', 'aac_low', '-ab', '192k', '-cutoff', '18000' ] if self.Config['DownMixStereo'] and self.Config[ 'EncodeAudioToAAC']: audiocodec.extend(['-ac:0', '2']) if ac3_stream == None: # no ac3 stream found - all streams are muxed map = ['-map', '0'] else: if self.Config['RemoveOtherAudioStreamsThanAC3']: # mux only video and ac3 stream map = ['-map', '0:v', '-map', ac3_stream] else: map = ['-map', '0'] args = [ ffmpeg, "-loglevel", "info", "-y", "-drc_scale", "1.0", "-i", filename, "-vn", '-af', 'volume=volume=' + str(vol), "-vsync", "1", '-async', '1000', "-dts_delta_threshold", "100", "-vf", "fps=" + str(fps), '-threads', '0', ffmpegpass_file ] map.extend(audiocodec) args[8:8] = map try: p = subprocess.Popen(args, stderr=subprocess.PIPE, universal_newlines=True) except OSError: self.errors[ filename] = "FFMPEG (intern) wurde nicht gefunden!" continue yield 4, 0 line = "" infos_match = re.compile( r"time=(\d{2,}):(\d{2,}):(\d{2,}.\d{2,})") while p.poll() == None: line = p.stderr.readline() m = re.search(infos_match, line) if m and max_frames != 0: frame = (float(m.group(1)) * 3600 + float(m.group(2)) * 60 + float(m.group(3))) * fps next = float(frame / float(max_frames)) * 100 if next > self.progress: self.progress = next yield 4, self.progress else: pass exit_code = p.poll() if exit_code == 0: pass else: self.errors[ filename] = "Fehler beim Erzeugen der MP4 Datei durch FFMPEG" if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) continue # mkvmerge pass yield 2, count self.progress = 0 mkvpass_file = fileoperations.make_unique_filename( os.path.splitext(filename)[0] + ".mkv") if self.Config['EncodeAudioToAAC']: args = [ self.app.config.get_program('mkvmerge'), '--engage', 'no_cue_duration', '--engage', 'no_cue_relative_position', '--ui-language', 'en_US', "-o", mkvpass_file, '-A', filename, '-D', ffmpegpass_file ] else: if self.Config[ 'RemoveOtherAudioStreamsThanAC3'] and ac3_stream: args = [ self.app.config.get_program('mkvmerge'), '--engage', 'no_cue_duration', '--engage', 'no_cue_relative_position', '--ui-language', 'en_US', "-o", mkvpass_file, '-a', ac3_stream[2], filename ] else: args = [ self.app.config.get_program('mkvmerge'), '--engage', 'no_cue_duration', '--engage', 'no_cue_relative_position', '--ui-language', 'en_US', "-o", mkvpass_file, filename ] p = subprocess.Popen(args, stdout=subprocess.PIPE, env=my_env) p.stdout.readline() line = "" while p.poll() == None: # read progress from stdout char = p.stdout.read(1) line += char progress = '' if char == ':': if "Error" in line or "Warning" in line: break while char != '%': char = p.stdout.read(1) progress += char try: self.progress = int(progress.strip(' %')) yield 3, self.progress except ValueError: pass exit_code = p.poll() if exit_code == 0 or exit_code == 1: self.success += 1 if self.Config['EncodeAudioToAAC']: fileoperations.remove_file(ffmpegpass_file) if self.Config['DumpAVIs']: if self.Config['DumpAVIs_delete']: fileoperations.remove_file(filename) else: new_filename = os.path.join( self.app.config.get('general', 'folder_trash_avis'), os.path.basename(filename)) if os.path.exists(new_filename): fileoperations.remove_file(new_filename) fileoperations.move_file( filename, self.app.config.get('general', 'folder_trash_avis')) else: error = p.stdout.readline() try: error = error.split(":")[1] except IndexError: pass if "unknown type" in error: error = "Datei konnte nicht gelesen werden." self.errors[filename] = error
def mkvmerge(): # env my_env = os.environ.copy() my_env["LANG"] = "C" for count, filename in enumerate(filenames): yield 0, count yield 3, 0 self.progress = 0 #analyse file cutter = Cut(self.app, self.gui) fps, dar, sar, max_frames, ac3_stream, error = cutter.analyse_mediafile(filename) if fps == None: self.errors[filename] = error continue # encode aac with ffmpeg if self.Config['EncodeAudioToAAC']: #norm volume ausrechnen yield 5, count if self.Config['NormalizeAudio'] and self.Config['EncodeAudioToAAC']: vol, error = self.get_norm_volume(filename) else: vol = 1.0 # ffmpeg pass yield 1, count self.progress = 0 ffmpegpass_file = fileoperations.make_unique_filename(os.path.splitext(filename)[0] + "_remux.mkv") # convert first audio stream to aac if self.Config['EncodeOnlyFirstAudioToAAC']: aacaudiostreams = '-c:a:0' else: aacaudiostreams = '-c:a' # convert first audio stream to aac ffmpeg = self.app.config.get_program('ffmpeg') if 'nonfree' in ffmpeg: # nonfree ffmpeg version with fdk support available audiocodec = ['-c:a', 'copy', aacaudiostreams, 'libfdk_aac', '-flags', '+qscale', '-profile:a:0', 'aac_low', '-global_quality', '5' ,'-afterburner', '1'] else: # only gpl version of ffmpeg available -> use standard aac codec audiocodec = ['-c:a', 'copy', aacaudiostreams, 'aac', '-strict', '-2','-profile:a:0', 'aac_low', '-ab' ,'192k', '-cutoff', '18000'] if self.Config['DownMixStereo'] and self.Config['EncodeAudioToAAC']: audiocodec.extend(['-ac:0', '2']) if ac3_stream == None: # no ac3 stream found - all streams are muxed map = ['-map', '0'] else: if self.Config['RemoveOtherAudioStreamsThanAC3']: # mux only video and ac3 stream map = ['-map', '0:v', '-map', ac3_stream] else: map = ['-map' ,'0'] args = [ffmpeg, "-loglevel", "info", "-y", "-drc_scale", "1.0", "-i", filename, "-vn", '-af', 'volume=volume=' + str(vol), "-vsync", "1", '-async', '1000', "-dts_delta_threshold", "100", "-vf", "fps="+ str(fps), '-threads', '0', ffmpegpass_file] map.extend(audiocodec) args[8:8] = map try: p = subprocess.Popen(args, stderr=subprocess.PIPE, universal_newlines=True) except OSError: self.errors[filename] = "FFMPEG (intern) wurde nicht gefunden!" continue yield 4, 0 line = "" infos_match = re.compile(r"time=(\d{2,}):(\d{2,}):(\d{2,}.\d{2,})") while p.poll() == None: line = p.stderr.readline() m = re.search(infos_match,line) if m and max_frames != 0: frame = (float(m.group(1))*3600 + float(m.group(2))*60 + float(m.group(3)))*fps next = float( frame / float(max_frames) ) * 100 if next > self.progress: self.progress = next yield 4, self.progress else: pass exit_code = p.poll() if exit_code == 0: pass else: self.errors[filename] = "Fehler beim Erzeugen der MP4 Datei durch FFMPEG" if os.path.exists(ffmpegpass_file): fileoperations.remove_file(ffmpegpass_file) continue # mkvmerge pass yield 2, count self.progress = 0 mkvpass_file = fileoperations.make_unique_filename(os.path.splitext(filename)[0] + ".mkv") if self.Config['EncodeAudioToAAC']: args = [self.app.config.get_program('mkvmerge'), '--engage', 'no_cue_duration', '--engage', 'no_cue_relative_position', '--ui-language', 'en_US',"-o", mkvpass_file, '-A', filename, '-D', ffmpegpass_file] else: if self.Config['RemoveOtherAudioStreamsThanAC3'] and ac3_stream: args = [self.app.config.get_program('mkvmerge'), '--engage', 'no_cue_duration', '--engage', 'no_cue_relative_position', '--ui-language', 'en_US', "-o", mkvpass_file, '-a', ac3_stream[2], filename] else: args = [self.app.config.get_program('mkvmerge'), '--engage', 'no_cue_duration', '--engage', 'no_cue_relative_position', '--ui-language', 'en_US', "-o", mkvpass_file, filename] p = subprocess.Popen(args, stdout=subprocess.PIPE, env=my_env) p.stdout.readline() line = "" while p.poll() == None: # read progress from stdout char = p.stdout.read(1) line += char progress = '' if char == ':': if "Error" in line or "Warning" in line: break while char != '%': char = p.stdout.read(1) progress += char try: self.progress = int(progress.strip(' %')) yield 3, self.progress except ValueError: pass exit_code = p.poll() if exit_code == 0 or exit_code == 1: self.success += 1 if self.Config['EncodeAudioToAAC']: fileoperations.remove_file(ffmpegpass_file) if self.Config['DumpAVIs']: if self.Config['DumpAVIs_delete']: fileoperations.remove_file(filename) else: new_filename = os.path.join(self.app.config.get('general', 'folder_trash_avis'), os.path.basename(filename)) if os.path.exists(new_filename): fileoperations.remove_file(new_filename) fileoperations.move_file(filename, self.app.config.get('general', 'folder_trash_avis')) else: error = p.stdout.readline() try: error = error.split(":")[1] except IndexError: pass if "unknown type" in error: error = "Datei konnte nicht gelesen werden." self.errors[filename] = error