Upscale Video¶
How to scale up a 480p (854x480) video to Full HD 1080p (1920x1080) video.
The code snippets in this article are from the simple_video_upscale Python sample.
Source Video¶
For a source video we use the MP4 file from the TED talk video What’s the next window into our universe? by Andrew Connolly. The original video format is Wide 480p or 16:9, 854 x 480.
Code¶
This code takes an MP4 file with H.264 video and AAC audio, and scales the video stream up to Full HD 1920x1080 (1080p) using cubic interpolation for best upscaling quality. The audio stream is copied from the source as is.
Initialize AVBlocks and Configure Transcoder¶
The sample uses hardcoded input and output file paths. It creates a MediaInfo object to probe the input file, then creates input and output sockets. The output socket is cloned from the input socket, then the frame size is set to 1920x1080 and the interpolation method is set to InterpolationMethod.Cubic.
def main():
Library.initialize()
# Set license information. To run AVBlocks in demo mode, comment the next line out
# Library.set_license("<license-string>")
input_path = "AndrewConnolly_2014.mp4"
output_path = "AndrewConnolly_2014_1080p.mp4"
try:
with MediaInfo() as media_info:
media_info.inputs[0].file = input_path
if not media_info.open():
print_error("Open MediaInfo", media_info.error)
return
input_socket = MediaSocket.from_media_info(media_info)
output_socket = input_socket.clone()
output_socket.file = output_path
out_video_pin = output_socket.pins[0]
out_video_stream_info = out_video_pin.stream_info
if isinstance(out_video_stream_info, VideoStreamInfo):
out_video_stream_info.frame_width = 1920
out_video_stream_info.frame_height = 1080
out_video_pin.params[Param.Video.Resize.InterpolationMethod] = int(InterpolationMethod.Cubic)
with Transcoder() as transcoder:
transcoder.allow_demo_mode = True
transcoder.inputs.add(input_socket)
transcoder.outputs.add(output_socket)
if transcoder.open():
transcoder.run()
transcoder.close()
else:
print_error("Transcoder Open", transcoder.error)
except Exception as ex: # pylint: disable=broad-exception-caught
print(f"Exception: {ex}")
finally:
Library.shutdown()
print_error Helper¶
def print_error(action: str, error) -> None:
"""Print error information."""
if action:
print(f"{action}: ", end="")
if error.facility == ErrorFacility.Success:
print("Success")
return
print(f"{error.message or ''}, facility:{error.facility} code:{error.code} hint:{error.hint or ''}")
Complete Code¶
Here’s the complete working example that demonstrates video upscaling using AVBlocks for Python.
"""
Scale a 480p (854x480) video up to Full HD 1080p (1920x1080).
This sample demonstrates the basic AVBlocks video upscaling workflow with
hardcoded input and output file paths (no command line parsing).
"""
from avblocks import (
Library, MediaInfo, MediaSocket, Transcoder,
VideoStreamInfo, Param, InterpolationMethod, ErrorFacility
)
def print_error(action: str, error) -> None:
"""Print error information."""
if action:
print(f"{action}: ", end="")
if error.facility == ErrorFacility.Success:
print("Success")
return
print(f"{error.message or ''}, facility:{error.facility} code:{error.code} hint:{error.hint or ''}")
def main():
Library.initialize()
# Set license information. To run AVBlocks in demo mode, comment the next line out
# Library.set_license("<license-string>")
input_path = "AndrewConnolly_2014.mp4"
output_path = "AndrewConnolly_2014_1080p.mp4"
try:
with MediaInfo() as media_info:
media_info.inputs[0].file = input_path
if not media_info.open():
print_error("Open MediaInfo", media_info.error)
return
input_socket = MediaSocket.from_media_info(media_info)
output_socket = input_socket.clone()
output_socket.file = output_path
out_video_pin = output_socket.pins[0]
out_video_stream_info = out_video_pin.stream_info
if isinstance(out_video_stream_info, VideoStreamInfo):
out_video_stream_info.frame_width = 1920
out_video_stream_info.frame_height = 1080
out_video_pin.params[Param.Video.Resize.InterpolationMethod] = int(InterpolationMethod.Cubic)
with Transcoder() as transcoder:
transcoder.allow_demo_mode = True
transcoder.inputs.add(input_socket)
transcoder.outputs.add(output_socket)
if transcoder.open():
transcoder.run()
transcoder.close()
else:
print_error("Transcoder Open", transcoder.error)
except Exception as ex: # pylint: disable=broad-exception-caught
print(f"Exception: {ex}")
finally:
Library.shutdown()
if __name__ == '__main__':
main()
How to Run¶
See the simple_video_upscale Python sample for details.
Command Line¶
This sample uses hardcoded input and output file paths — no command line parsing.
simple-video-upscale
Examples¶
Install the package in development mode from the repository root (this installs the sample command):
source .venv/bin/activate
uv pip install --editable .
Download the sample video, the MP4 file from the TED talk video What’s the next window into our universe? by Andrew Connolly (480p, 854x480):
cd samples/simple_video_upscale
curl -L -o AndrewConnolly_2014.mp4 \
https://archive.org/download/AndrewConnolly_2014/AndrewConnolly_2014.mp4
Run the sample (the input/output file paths are relative to the working directory):
simple-video-upscale
The upscaled output file AndrewConnolly_2014_1080p.mp4 will be created in the samples/simple_video_upscale directory.