PSHomeCacheDepot / MEDIA /scripts /encode_video.py
pebxcvi's picture
update
e5981d7
Raw
History Blame Contribute Delete
5.7 kB
import subprocess
import os
# Input files
input_files = [
"wipEoutHOME.mp4"
]
output_dir = "output"
os.makedirs(output_dir, exist_ok=True)
for input_file in input_files:
output_file = os.path.join(output_dir, os.path.basename(input_file))
cmd = [
"ffmpeg",
"-i", input_file,
# copy streams exactly as-is
"-c", "copy",
# DO NOT include any movflags
# (this keeps moov atom at the end)
"-y",
output_file
]
print("Running command:", " ".join(cmd))
result = subprocess.run(cmd)
if result.returncode != 0:
print(f"Error processing {input_file}")
else:
print(f"Successfully created {output_file}")
# PS Home supports the following video codecs:
# MPEG-4 Part 2 MPEG-4 Video Simple Profile - up to Level 3
# MPEG-4 Part 2 MPEG-4 Video Advanced Simple Profile - up to Level 5
# MPEG-4 Part 10 AVC H.264 - up to Level 3.1
#
# AVC H.264 Level 3.1 720p, 30fps is supported only in video spaces. Other types of spaces or scene types except
# game spaces support up to Level 3.0.
#
# PS Home supports the following video container formats:
# MPEG-4 Part 14 MP4
# MPEG-2 Part 1 Transport Stream TS
#
# If you require audio, you can add an AAC audio track to the container. For more information on MPEG-4 Levels and
# supported resolutions, see Screen Memory Usage.
#
# The recommended tool for encoding video for PS Home is FFmpeg or an application based on FFmpeg, though any application which
# creates files which adhere to supported MPEG-4 standards should be compatible. For best results, make sure that both the video
# width and the video height are multiples of 16.
#
# Video is automatically stretched to fit the screen, so you dont need to encode video at the correct aspect ratio
# for it to display correctly assuming that the screen is the correct aspect ratio. This makes it easier to
# select video dimensions that are multiples of 16.
#
# Selecting an Appropriate Bit Rate
# Although PS Home is able to play video with bit rates up to the maximum allowed for each supported MPEG-4 profile level up to 8
# Mbps for MPEG-4 Video and 14 Mbps for AVC H.264, bit rates this high are rarely required to maintain a high level of video
# quality.
#
# The bit rate required will depend on the following factors:
#
# Resolution: High-resolution videos require a higher bit rate.
# Frame rate: Higher frame rates require higher bit rates, note that there is no visible benefit to using a frame rate higher
# than 29.97 frames per second fps as this is the frame rate of the PS Home client.
#
# Video format: MPEG-4 Video requires higher bit rates than AVC H.264 at the same resolution in order to maintain similar
# quality levels.
#
# Video content: Video files with large amounts of fast-moving footage will require a higher bit rate, whereas video
# containing mostly static backgrounds can use a lower bit rate without sacrificing quality.
# For these reasons, it is not possible to specify what bit rates should be used to ensure high-quality video in all situations,
# but the following examples may be used as guidelines when encoding video:
#
# Format Width Height Frame Rate fps Bit Rate Kbps
# MPEG-4 Part 2, Simple Profile 176 144 15 64
# MPEG-4 Part 2, Simple Profile 352 288 29.97 fps* 384
# MPEG-4 Part 2, Advanced Simple Profile 352 288 29.97 fps* 768
# MPEG-4 Part 2, Advanced Simple Profile 640 480 29.97 fps* 1536
# MPEG-4 Part 2, Advanced Simple Profile 720 576 29.97 fps* 2048
# MPEG-4 Part 2, Advanced Simple Profile
# cmd = [
# "ffmpeg",
# "-i", input_file,
# "-c:v", "mpeg4",
# "-profile:v", "15",
# "-level:v", "3",
# "-s", "720x480",
# "-r", "29.97",
# "-b:v", "1024k",
# "-c:a", "aac",
# "-af", "volume=2.0",
# "-y",
# output_file
# ]
# Format Width Height Frame Rate (fps) Bit Rate (Kbps)
# MPEG-4 Part 10 (AVC/H.264) 320 240 29.97 fps* 256
# MPEG-4 Part 10 (AVC/H.264) 640 480 29.97 fps* 768
# MPEG-4 Part 10 (AVC/H.264) 720 480 29.97 fps* 1024
# MPEG-4 Part 10 (AVC/H.264) 1280 720 29.97 fps* 3072
# https://trac.ffmpeg.org/wiki/Encode/H.264
# Preset Encode speed File size / compression efficiency
# ultrafast Very fast Largest file size / least efficient
# superfast Faster Larger file size
# veryfast Fast (default for some builds) Decent compression
# faster Slightly slower Smaller files
# fast Slower Better compression
# medium Balanced (default) Good compromise
# slow Slower Smaller file size
# slower Very slow Even smaller files
# veryslow Extremely slow Best compression, smallest file size
# The range of the CRF scale is 0-51, where 0 is lossless (for 8 bit only, for 10 bit use -qp 0), 23 is the default, and 51 is worst quality possible.
# A lower value generally leads to higher quality, and a subjectively sane range is 17-28. Consider 17 or 18 to be visually lossless or nearly so;
# it should look the same or nearly the same as the input but it isn't technically lossless.
# using -crf
# lower crf, higher quality
# "-vf", "scale=720:480,fps=29.97",
#cmd = [
# "ffmpeg",
# "-i", input_file,
# "-vf", "scale=640:480,fps=29.97",
# "-c:v", "libx264",
# "-preset", "medium",
# "-crf", "22",
# "-c:a", "aac",
# "-y",
# output_file
#]
# using -b:v
# "-vf", "scale=720:480,fps=29.97",
#cmd = [
# "ffmpeg",
# "-i", input_file,
# "-vf", "scale=640:480,fps=29.97",
# "-c:v", "libx264",
# "-preset", "medium",
# "-b:v", "1024k",
# "-c:a", "aac",
# "-y",
# output_file
#]