Using Video Presets¶
How to use the Transcoder class to convert a raw YUV video file into a compressed video file. The format of the output is configured with an AVBlocks preset.
The code snippets in this article are from the enc_preset_file sample.
Create a Transcoder object¶
transcoder = Transcoder()
# Transcoder demo mode must be enabled,
# in order to use the OEM release for testing (without a valid license).
transcoder.allow_demo_mode = True
Configure inputs and outputs¶
Use the MediaSocket class to configure media sockets and the MediaPin class to configure media pins.
Configure video input¶
Create and configure a VideoStreamInfo object
Create a MediaPin object and set the
MediaPin.stream_infoproperty.Create a MediaSocket object and add the pin to
MediaSocket.pins.Add the socket to
Transcoder.inputs.
# Configure input
# The input stream frame rate determines the playback speed
in_stream = VideoStreamInfo()
in_stream.stream_type = StreamType.UncompressedVideo
in_stream.frame_rate = fps
in_stream.frame_width = width
in_stream.frame_height = height
in_stream.color_format = color_id
in_stream.scan_type = ScanType.Progressive
in_pin = MediaPin()
in_pin.stream_info = in_stream
in_socket = MediaSocket()
in_socket.stream_type = StreamType.UncompressedVideo
in_socket.file = input_file
in_socket.pins.add(in_pin)
transcoder.inputs.add(in_socket)
Configure video output¶
Call the
MediaSocket.from_presetstatic method to create a MediaSocket object for a predefined presetSet the
MediaSocket.fileproperty to the output filename.Add the socket to
Transcoder.outputs.
# Configure output
out_socket = MediaSocket.from_preset(preset_name)
if out_socket is None:
print(f"Invalid preset: {preset_name}")
return False
out_socket.file = output_file
transcoder.outputs.add(out_socket)
Run Transcoder¶
Call the
Transcoder.openmethod.Call the
Transcoder.runmethod.Call the
Transcoder.closemethod.
# Open transcoder
if not transcoder.open():
print_error("Open Transcoder", transcoder.error)
return False
# Run transcoder
if not transcoder.run():
print_error("Run Transcoder", transcoder.error)
transcoder.close()
return False
transcoder.close()
The complete encode function¶
def encode(input_file: str, output_file: str, width: int, height: int,
fps: float, color_id, preset_name: str) -> bool:
"""Encode raw YUV video to compressed video using preset."""
# Delete output file if it exists
delete_file(output_file)
# Create output directory if it doesn't exist
output_dir = os.path.dirname(output_file)
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir)
transcoder = Transcoder()
# Transcoder demo mode must be enabled,
# in order to use the OEM release for testing (without a valid license).
transcoder.allow_demo_mode = True
# Configure input
# The input stream frame rate determines the playback speed
in_stream = VideoStreamInfo()
in_stream.stream_type = StreamType.UncompressedVideo
in_stream.frame_rate = fps
in_stream.frame_width = width
in_stream.frame_height = height
in_stream.color_format = color_id
in_stream.scan_type = ScanType.Progressive
in_pin = MediaPin()
in_pin.stream_info = in_stream
in_socket = MediaSocket()
in_socket.stream_type = StreamType.UncompressedVideo
in_socket.file = input_file
in_socket.pins.add(in_pin)
transcoder.inputs.add(in_socket)
# Configure output
out_socket = MediaSocket.from_preset(preset_name)
if out_socket is None:
print(f"Invalid preset: {preset_name}")
return False
out_socket.file = output_file
transcoder.outputs.add(out_socket)
# Open transcoder
if not transcoder.open():
print_error("Open Transcoder", transcoder.error)
return False
# Run transcoder
if not transcoder.run():
print_error("Run Transcoder", transcoder.error)
transcoder.close()
return False
transcoder.close()
print("Encoding completed successfully.")
return True