Each X264 fileis approx 500Mb, a X265 would be 200mb at same quality (but less portable) compared to 70Gb of raw float32, relative error lower that 0.01% 5000X compression ratio or more (based on x265 parameters), with video data quantized to 12-bit luminance.
Decompress the video and extract metadata:
ffmpeg -i float2t2_video.mp4 -f rawvideo -pix_fmt gray16 uint16_float2t2.raw
ffprobe -v error -select_streams v:0 -show_entries format_tags=comment -of default=noprint_wrappers=1:nokey=1 float2t2_video.mp4
Reads the decompressed raw video using mmap, rescales it, and structures it into a NumPy array:
import numpy as np
import mmap
import sys
def process_raw_video(raw_video_path, ni, nj, min_val, max_val):
with open(raw_video_path, 'rb') as f:
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
frame_size = ni * nj * 2 # bytes per frame (uint16)
nframes = mm.size() // frame_size
raw_data = np.frombuffer(mm, dtype=np.uint16, count=nframes*nj*ni)
raw_data = raw_data.reshape((nframes, nj, ni))
float_data = raw_data.astype(np.float32)
float_data = (float_data / 65535.0) * (max_val - min_val) + min_val
mm.close()
return float_data
if __name__ == "__main__":
if len(sys.argv) != 6:
print("Usage: python process_raw_video.py ")
sys.exit(1)
raw_video_path = sys.argv[1]
ni = int(sys.argv[2])
nj = int(sys.argv[3])
min_val = float(sys.argv[4])
max_val = float(sys.argv[5])
float_data = process_raw_video(raw_video_path, ni, nj, min_val, max_val)
print(f"Data shape: {float_data.shape}")
mmap for efficient memory-mapped file access and takes the following arguments:
raw_video_path: Path to the decompressed raw uint16 video file.ni: Width of the video (e.g., 1920).nj: Height of the video (e.g., 1080).min_val: Minimum value extracted from the video metadata.max_val: Maximum value extracted from the video metadata.Run the script as follows:
python process_raw_video.py uint16_float2t2.raw 1920 1080 250.344 304.073