def merge(self): if self.detect is None: log.error("Cant detect ffmpeg or avconv. Cant mux files without it.") return if self.stream.finished is False: return orig_filename = formatname(self.stream.output, self.config, self.stream.output_extention) cmd = [self.detect, "-i", orig_filename] _, stdout, stderr = run_program(cmd, False) # return 1 is good here. videotrack, audiotrack = self._checktracks(stderr) if self.config.get("merge_subtitle"): log.info("Merge audio, video and subtitle into {0}".format(orig_filename)) else: log.info("Merge audio and video into {0}".format(orig_filename)) tempfile = u"{0}.temp".format(orig_filename) name, ext = os.path.splitext(orig_filename) arguments = ["-c:v", "copy", "-c:a", "copy", "-f", "mp4"] if ext == ".ts": audio_filename = u"{0}.audio.ts".format(name) arguments += ["-bsf:a", "aac_adtstoasc"] else: audio_filename = u"{0}.m4a".format(name) cmd = [self.detect, "-i", orig_filename, "-i", audio_filename] if self.config.get("merge_subtitle"): langs = self.sublanguage() for stream_num, language in enumerate(langs, start=audiotrack + 1): arguments += ["-map", "{}".format(videotrack), "-map", "{}".format(audiotrack), "-map", str(stream_num), "-c:s:" + str(stream_num - 2), "mov_text", "-metadata:s:s:" + str(stream_num - 2), "language=" + language] if self.subfixes and len(self.subfixes) >= 2: for subfix in self.subfixes: subfile = "{0}.srt".format(name + subfix) cmd += ["-i", subfile] else: subfile = "{0}.srt".format(name) cmd += ["-i", subfile] arguments += ["-y", tempfile] cmd += arguments returncode, stdout, stderr = run_program(cmd) if returncode != 0: return log.info("Merging done, removing old files.") os.remove(orig_filename) os.remove(audio_filename) if self.config.get("merge_subtitle") and not self.config.get("subtitle"): if self.subfixes and len(self.subfixes) >= 2: for subfix in self.subfixes: subfile = "{0}.srt".format(name + subfix) os.remove(subfile) else: os.remove(subfile) os.rename(tempfile, orig_filename)
def remux(self): if self.detect is None: logging.error( "Cant detect ffmpeg or avconv. Cant mux files without it.") return if self.stream.finished is False: return if formatname(self.stream.output, self.config, self.stream.output_extention).endswith(".mp4") is False: orig_filename = formatname(self.stream.output, self.config, self.stream.output_extention) name, ext = os.path.splitext(orig_filename) new_name = "{}.mp4".format(name) cmd = [self.detect, "-i", orig_filename] _, stdout, stderr = run_program(cmd, False) # return 1 is good here. streams = _streams(stderr) videotrack, audiotrack = _checktracks(streams) if self.config.get("merge_subtitle"): logging.info( "Muxing {} and merging its subtitle into {}".format( orig_filename, new_name)) else: logging.info("Muxing {} into {}".format( orig_filename, new_name)) tempfile = "{}.temp".format(orig_filename) arguments = [] if videotrack: arguments += ["-map", "{}".format(videotrack)] if audiotrack: arguments += ["-map", "{}".format(audiotrack)] arguments += ["-c", "copy", "-f", "mp4"] if ext == ".ts" and "aac" in _getcodec(streams, audiotrack): arguments += ["-bsf:a", "aac_adtstoasc"] if self.config.get("merge_subtitle"): langs = _sublanguage(self.stream, self.config, self.subfixes) for stream_num, language in enumerate(langs): arguments += [ "-map", str(stream_num + 1), "-c:s:" + str(stream_num), "mov_text", "-metadata:s:s:" + str(stream_num), "language=" + language, ] if self.subfixes and len(self.subfixes) >= 2: for subfix in self.subfixes: subfile = "{}.srt".format(name + subfix) cmd += ["-i", subfile] else: subfile = "{}.srt".format(name) cmd += ["-i", subfile] arguments += ["-y", tempfile] cmd += arguments returncode, stdout, stderr = run_program(cmd) if returncode != 0: return if self.config.get( "merge_subtitle") and not self.config.get("subtitle"): logging.info("Muxing done, removing the old files.") if self.subfixes and len(self.subfixes) >= 2: for subfix in self.subfixes: subfile = "{}.srt".format(name + subfix) os.remove(subfile) else: os.remove(subfile) else: logging.info("Muxing done, removing the old file.") os.remove(orig_filename) os.rename(tempfile, new_name)
def merge(self): if self.detect is None: logging.error( "Cant detect ffmpeg or avconv. Cant mux files without it.") return if self.stream.finished is False: return orig_filename = formatname(self.stream.output, self.config, self.stream.output_extention) name, ext = os.path.splitext(orig_filename) if ext == ".ts": audio_filename = f"{name}.audio.ts" else: audio_filename = f"{name}.m4a" cmd = [self.detect] if self.config.get("only_video") or not self.config.get("only_audio"): cmd += ["-i", orig_filename] if self.config.get("only_audio") or not self.config.get("only_video"): cmd += ["-i", audio_filename] _, stdout, stderr = run_program(cmd, False) # return 1 is good here. streams = _streams(stderr) videotrack, audiotrack = _checktracks(streams) if self.config.get("merge_subtitle"): logging.info( f"Merge audio, video and subtitle into {orig_filename}") else: logging.info(f"Merge audio and video into {orig_filename}") tempfile = f"{orig_filename}.temp" arguments = ["-c:v", "copy", "-c:a", "copy", "-f", "mp4"] if ext == ".ts": if audiotrack and "aac" in _getcodec(streams, audiotrack): arguments += ["-bsf:a", "aac_adtstoasc"] cmd = [self.detect] if self.config.get("only_video") or not self.config.get("only_audio"): cmd += ["-i", orig_filename] if self.config.get("only_audio") or not self.config.get("only_video"): cmd += ["-i", audio_filename] if videotrack: arguments += ["-map", f"{videotrack}"] if audiotrack: arguments += ["-map", f"{audiotrack}"] if self.config.get("merge_subtitle"): langs = _sublanguage(self.stream, self.config, self.subfixes) tracks = [x for x in [videotrack, audiotrack] if x] for stream_num, language in enumerate(langs, start=len(tracks)): arguments += [ "-map", str(stream_num), "-c:s:" + str(stream_num - 2), "mov_text", "-metadata:s:s:" + str(stream_num - 2), "language=" + language, ] if self.subfixes and len(self.subfixes) >= 2: for subfix in self.subfixes: subfile = "{}.srt".format(name + subfix) cmd += ["-i", subfile] else: subfile = f"{name}.srt" cmd += ["-i", subfile] arguments += ["-y", tempfile] cmd += arguments returncode, stdout, stderr = run_program(cmd) if returncode != 0: return logging.info("Merging done, removing old files.") if self.config.get("only_video") or not self.config.get("only_audio"): os.remove(orig_filename) if self.config.get("only_audio") or not self.config.get("only_video"): os.remove(audio_filename) if self.config.get( "merge_subtitle") and not self.config.get("subtitle"): if self.subfixes and len(self.subfixes) >= 2: for subfix in self.subfixes: subfile = "{}.srt".format(name + subfix) os.remove(subfile) else: os.remove(subfile) os.rename(tempfile, orig_filename)