diff --git a/transcoder/main.py b/transcoder/main.py index 793d40c..e325f88 100644 --- a/transcoder/main.py +++ b/transcoder/main.py @@ -29,12 +29,12 @@ def main(args): m = movie.Movie( filename, every_n_video_frames=args.every_n_video_frames, - audio_normalization=args.audio_normalization) + audio_normalization=args.audio_normalization, + max_bytes_out = 1024. * 1024 * args.max_output_mb + ) print("Input frame rate = %f" % m.video.input_frame_rate) - max_bytes_out = 1024. * 1024 * args.max_output_mb - if args.output: out_filename = args.output else: @@ -45,11 +45,6 @@ def main(args): for bytes_out, b in enumerate(m.emit_stream(m.encode())): out.write(bytearray([b])) - if max_bytes_out and bytes_out >= max_bytes_out: - break - - out.write(bytes(m.done())) - if __name__ == "__main__": main(parser.parse_args()) diff --git a/transcoder/movie.py b/transcoder/movie.py index 2e270c9..d5bed33 100644 --- a/transcoder/movie.py +++ b/transcoder/movie.py @@ -12,9 +12,13 @@ class Movie: def __init__( self, filename: str, every_n_video_frames: int = 1, - audio_normalization: float = None): + audio_normalization: float = None, + max_bytes_out: int = None + ): self.filename = filename # type: str self.every_n_video_frames = every_n_video_frames # type: int + self.max_bytes_out = max_bytes_out # type: int + self.audio = audio.Audio( filename, normalization=audio_normalization) # type: audio.Audio self.video = video.Video(filename) # type: video.Video @@ -75,6 +79,9 @@ class Movie: :return: """ for op in ops: + if self.stream_pos >= self.max_bytes_out: + yield from self.done() + return # Keep track of where we are in TCP client socket buffer socket_pos = self.stream_pos % 2048 if socket_pos >= 2044: @@ -88,3 +95,7 @@ class Movie: :return: """ yield from self._emit_bytes(opcodes.Terminate()) + + # Player expects to fill 2K TCP buffer so pad it out + for _ in range(2048 - (self.stream_pos % 2048)): + yield 0x00