-
I need to use pyav library to encode h264, and I need to set the slices value, which does not work after I set the value in options. How can I set it to work |
Beta Was this translation helpful? Give feedback.
Answered by
WyattBlue
Jun 11, 2024
Replies: 2 comments 2 replies
-
Something like this? import av
av.logging.set_level(av.logging.VERBOSE)
def reencode_with_slices(
input_filename: str, output_filename: str, slices: int
) -> None:
input_container = av.open(input_filename)
output_container = av.open(output_filename, mode="w")
video_stream = output_container.add_stream("h264", rate=30)
video_stream.codec_context.options = {"slices": f"{slices}"}
input_video_stream = input_container.streams.video[0]
video_stream.width = input_video_stream.width
video_stream.height = input_video_stream.height
video_stream.pix_fmt = "yuv420p"
for frame in input_container.decode(input_video_stream):
for packet in video_stream.encode(frame):
output_container.mux(packet)
# Flush the encoder
for packet in video_stream.encode():
output_container.mux(packet)
input_container.close()
output_container.close()
if __name__ == "__main__":
input_filename = "example.mp4"
output_filename = "output.mp4"
slices = 4 # Set your desired number of slices here
reencode_with_slices(input_filename, output_filename, slices) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
WyattBlue
-
I tested this code, set slices to 1, analyzed the stream with the tool, and found that slices did not work |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something like this?