This commit is contained in:
2025-10-01 16:06:53 +02:00
commit 93c6b1c261
12 changed files with 874 additions and 0 deletions

183
utils/encodeCDN.py Normal file
View File

@@ -0,0 +1,183 @@
import os
import subprocess
import shutil
import platform
segment = ' -init_seg_name chunks/init-stream$RepresentationID$.m4s -media_seg_name chunks/chunk-stream$RepresentationID$-$Number%05d$.m4s'
if platform.system() == 'Linux':
segment = ' -init_seg_name chunks/init-stream\$RepresentationID\$.m4s -media_seg_name chunks/chunk-stream\$RepresentationID\$-\$Number%05d\$.m4s'
def changeM4SToHTML(mpdpath, chunkspath):
for filename in os.listdir(chunkspath):
infilename = os.path.join(chunkspath, filename)
if not os.path.isfile(infilename): continue
newname = infilename.replace('.m4s', '.webp')
_ = os.rename(infilename, newname)
# Modify manifest
with open(mpdpath, 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('.m4s', '.webp')
# Write the file out again
with open(mpdpath, 'w') as file:
file.write(filedata)
def createFolder(cdnFolder, resolution):
if platform.system() == 'Linux':
if not os.path.exists(os.path.join(cdnFolder, resolution, 'chunks')):
os.makedirs(os.path.join(cdnFolder, resolution, 'chunks'))
else:
if not os.path.exists('chunks'):
os.makedirs('chunks')
if not os.path.exists(os.path.join(cdnFolder, resolution)):
os.makedirs(os.path.join(cdnFolder, resolution))
def create_sprites(cdn_folder):
"""
Creates video player sprites
"""
video_file = os.path.join(cdn_folder, 'x264.720p.mp4')
# Generating Sprites
if not os.path.exists(os.path.join(cdn_folder, 'thumbs.vtt')) and os.path.exists(video_file):
os.system(f'python makesprites.py "{video_file}"')
os.rename("thumbs.vtt", os.path.join(cdn_folder, 'thumbs.vtt'))
os.rename("sprite.jpg", os.path.join(cdn_folder, 'sprite.jpg'))
shutil.rmtree('thumbs')
shutil.rmtree('logs')
return
print('Skipped Sprites')
def encode_720p_fallback(cdn_folder, video_source, upscale_output, aspect_ratio):
"""
Fallback video stream for apple devices
"""
output = os.path.join(cdn_folder, 'x264.720p.mp4')
if os.path.exists(output):
print('Skipped 720p Encode')
return
command = (
f'ffmpeg -v quiet -stats -i "{upscale_output}" -i "{video_source}" '
'-map 0:v:0 -map 1:a:0 '
'-c:v libx264 -crf 22 -pix_fmt yuv420p '
f'-vf scale=1280:720,setsar=1:1 -aspect {aspect_ratio} '
'-c:a aac -b:a 128k '
f'-sn -map_metadata -1 -movflags +faststart "{output}"'
)
subprocess.call(command, shell=True)
def encode_720p(cdn_folder, video_source, upscale_output, aspect_ratio):
output = os.path.join(cdn_folder, '720', 'manifest.mpd')
if os.path.exists(output):
print('Skipped 720p DASH Encode')
return
print('Encoding 720p')
createFolder(cdn_folder, '720')
command = (
f'ffmpeg -v quiet -stats -i "{upscale_output}" -i "{video_source}" '
'-map 0:v:0 -map 1:a:0 '
'-c:v libx264 -crf 22 -preset medium -pix_fmt yuv420p -g 24 -keyint_min 24 -sc_threshold 0 -x264-params keyint=24:min-keyint=24:scenecut=0 '
f'-vf scale=1280:720,setsar=1:1 -aspect {aspect_ratio} '
f'{segment} "{output}"'
)
subprocess.call(command, shell=True)
if platform.system() != 'Linux':
shutil.move('chunks', os.path.join(cdn_folder, '720', 'chunks'))
changeM4SToHTML(output, os.path.join(cdn_folder, '720', 'chunks'))
def EncodeCDN(f, cdnFolder, inputAspect, upscaleOut, interpolateOut, INTERPOLATE_4K, interpolate_4k_output):
out1080mpd = os.path.join(cdnFolder, '1080', 'manifest.mpd')
out1080mpd48 = os.path.join(cdnFolder, '1080i', 'manifest.mpd')
out2160mpd = os.path.join(cdnFolder, '2160', 'manifest.mpd')
out2160mpd48 = os.path.join(cdnFolder, '2160i', 'manifest.mpd')
# 720p
encode_720p(cdnFolder, f, upscaleOut, inputAspect)
encode_720p_fallback(cdnFolder, f, upscaleOut, inputAspect)
create_sprites(cdnFolder)
# 1080p
if not os.path.exists(out1080mpd):
print('Encoding 1080p')
createFolder(cdnFolder, '1080')
subprocess.call('ffmpeg -v quiet -stats -i "' + upscaleOut + '" -i "' + f + '" -map 0:v:0 -map 1:a:0'
+ ' -c:v libsvtav1 -preset 6 -crf 26 -pix_fmt yuv420p -svtav1-params keyint=1s:tune=0 -vf scale=1920:1080,setsar=1:1 -aspect ' + inputAspect
+ ' -c:a aac -b:a 128k -ac 2 -sn -map_metadata -1 -seg_duration 10 -use_template 1 -use_timeline 1'
+ segment + ' "' + out1080mpd + '"', shell=True)
if platform.system() != 'Linux':
shutil.move('chunks', os.path.join(cdnFolder, '1080', 'chunks'))
changeM4SToHTML(out1080mpd, os.path.join(cdnFolder, '1080', 'chunks'))
else:
print('Skipped 1080p Encode')
# 1080p 48fps
if not os.path.exists(out1080mpd48):
print('Encoding 1080p 48fps')
createFolder(cdnFolder, '1080i')
# DASH av1 1080p
subprocess.call('ffmpeg -v quiet -stats -i "' + interpolateOut + '" -i "' + f + '" -map 0:v:0 -map 1:a:0'
+ ' -c:v libsvtav1 -preset 6 -crf 26 -pix_fmt yuv420p -svtav1-params keyint=1s:tune=0 -vf scale=1920:1080,setsar=1:1 -aspect ' + inputAspect
+ ' -c:a aac -b:a 128k -ac 2 -sn -map_metadata -1 -seg_duration 10 -use_template 1 -use_timeline 1'
+ segment + ' "' + out1080mpd48 + '"', shell=True)
if platform.system() != 'Linux':
shutil.move('chunks', os.path.join(cdnFolder, '1080i', 'chunks'))
changeM4SToHTML(out1080mpd48, os.path.join(cdnFolder, '1080i', 'chunks'))
else:
print('Skipped 1080p 48fps Encode')
# 2160p
if not os.path.exists(out2160mpd):
print('Encoding 2160p')
createFolder(cdnFolder, '2160')
subprocess.call('ffmpeg -v quiet -stats -i "' + upscaleOut + '" -i "' + f + '" -map 0:v:0 -map 1:a:0'
+ ' -c:v libsvtav1 -preset 6 -crf 26 -pix_fmt yuv420p -svtav1-params keyint=1s:tune=0 -vf "scale=\'min(3840,iw)\':-2,setsar=1:1" -aspect ' + inputAspect
+ ' -c:a aac -b:a 128k -ac 2 -sn -map_metadata -1 -seg_duration 10 -use_template 1 -use_timeline 1'
+ segment + ' "' + out2160mpd + '"', shell=True)
if platform.system() != 'Linux':
shutil.move('chunks', os.path.join(cdnFolder, '2160', 'chunks'))
changeM4SToHTML(out2160mpd, os.path.join(cdnFolder, '2160', 'chunks'))
else:
print('Skipped 2160p Encode')
# 2160p 48fps
if not os.path.exists(out2160mpd48) and INTERPOLATE_4K:
print('Encoding 2160p 48fps')
createFolder(cdnFolder, '2160i')
subprocess.call('ffmpeg -v quiet -stats -i "' + interpolate_4k_output + '" -i "' + f + '" -map 0:v:0 -map 1:a:0'
+ ' -c:v libsvtav1 -preset 6 -crf 26 -pix_fmt yuv420p -svtav1-params keyint=1s:tune=0 -vf "scale=\'min(3840,iw)\':-2,setsar=1:1" -aspect ' + inputAspect
+ ' -c:a aac -b:a 128k -ac 2 -sn -map_metadata -1 -seg_duration 10 -use_template 1 -use_timeline 1'
+ segment + ' "' + out2160mpd48 + '"', shell=True)
if platform.system() != 'Linux':
shutil.move('chunks', os.path.join(cdnFolder, '2160i', 'chunks'))
changeM4SToHTML(out2160mpd48, os.path.join(cdnFolder, '2160i', 'chunks'))
else:
print('Skipped 2160p 48fps Encode')

52
utils/encodeDDL.py Normal file
View File

@@ -0,0 +1,52 @@
import os
import subprocess
def extract_subs(video_source, subtitle_out, vtt_out):
if os.path.exists(subtitle_out):
print('Skipped Sub Extract')
return
print('Extracting Sub')
subprocess.call(f'ffmpeg -v quiet -stats -i "{video_source}" -c copy "{subtitle_out}"', shell=True)
subprocess.call(f'ffmpeg -v quiet -stats -i "{subtitle_out}" "{vtt_out}"', shell=True)
def encode_video(video_source, input_file, output_file, mux_file, temp_name, input_aspect, width, height):
if os.path.exists(output_file):
print(f'Skipped {height}p HEVC Encode')
return
print(f'Encoding {height}p HEVC')
command = (f'ffmpeg -v quiet -stats -i "{input_file}" -i "{video_source}"'
' -map 0:v:0 -map 1:a:0 -map 1:s:0 -map 1:t? -map 1:d?'
f' -disposition:v:0 default -metadata Title="{temp_name} [hstream.moe]"'
' -metadata:s:v:0 title="Upscaled by hstream.moe"'
' -c:v hevc_nvenc -qp 18 -pix_fmt yuv420p10le'
f' -vf "scale=\'min({width},iw)\':-2,setsar=1:1" -aspect {input_aspect}'
' -c:a aac -b:a 160k -c:s copy'
f' "{output_file}"'
)
subprocess.call(command, shell=True)
subprocess.run(f'mkvmerge --output "{mux_file}" "{output_file}"', shell=True)
def EncodeDDL(video_source, cdn_folder, folder_name, temp_name, upscale_out, input_aspect, interpolate_out, INTERPOLATE_4K, interpolate_4k_output):
# Extract subtitles
out_ass = os.path.join(cdn_folder, 'eng.ass')
out_vtt = os.path.join(cdn_folder, 'eng.vtt')
extract_subs(video_source, out_ass, out_vtt)
# Encoding settings
resolutions = [
(1920, 1080, upscale_out, "[1080p-HEVC]"),
(1920, 1080, interpolate_out, "[1080p-HEVC][48fps]"),
(3840, 2160, upscale_out, "[2160p-HEVC]")
]
# Also encode 4k 48fps if enabled
if INTERPOLATE_4K:
resolutions.append((3840, 2160, interpolate_4k_output, "[2160p-HEVC][48fps]"))
for width, height, input_file, suffix in resolutions:
tmp_out = os.path.join('2-Out', folder_name, f"{temp_name} {suffix}[hstream.moe].mkv")
mux_out = os.path.join('2-Out', folder_name, 'Muxed', f"{temp_name} {suffix}[hstream.moe].mkv")
encode_video(video_source, input_file, tmp_out, mux_out, temp_name, input_aspect, width, height)

50
utils/interpolate.py Normal file
View File

@@ -0,0 +1,50 @@
import os
import subprocess
def createInterpolateScript(upscaleOut, tempName, inputAspect):
if os.path.isfile(upscaleOut + '.vpy'):
print('Interpolate script exists')
return
script = []
if inputAspect == '4:3':
script = ['from vsrife import rife',
'import vapoursynth as vs',
'from vapoursynth import core',
'clip = core.ffms2.Source(source="./' + tempName + ' [4k][HEVC].mkv")',
'clip = vs.core.resize.Bicubic(clip, width=1440, height=1080, format=vs.RGBS, matrix_in_s="709")',
'clip = rife(clip=clip, model="4.15", factor_num=2, factor_den=1)',
'clip = vs.core.resize.Bicubic(clip, format=vs.YUV420P8, matrix_s="709")',
'clip.set_output()']
else:
script = ['from vsrife import rife',
'import vapoursynth as vs',
'from vapoursynth import core',
'clip = core.ffms2.Source(source="./' + tempName + ' [4k][HEVC].mkv")',
'clip = vs.core.resize.Bicubic(clip, width=1920, height=1080, format=vs.RGBS, matrix_in_s="709")',
'clip = rife(clip=clip, model="4.15", factor_num=2, factor_den=1)',
'clip = vs.core.resize.Bicubic(clip, format=vs.YUV420P8, matrix_s="709")',
'clip.set_output()']
if not os.path.isfile(upscaleOut + '.vpy'):
with open(upscaleOut + '.vpy', 'a') as fs:
fs.writelines([i + '\n' for i in script])
def Interpolate(interpolateOut, upscaleOut, tempName, inputAspect):
if os.path.isfile(interpolateOut):
print('Already interpolated')
return
createInterpolateScript(upscaleOut, tempName, inputAspect)
if not os.path.isfile(upscaleOut + '.vpy'):
print('=== Interpolation script not found ===')
return
print('Interpolating')
subprocess.call('vspipe -c y4m "' + upscaleOut + '.vpy" - | ffmpeg -v quiet -stats -i - -c:v hevc_nvenc -qp 5 "' + interpolateOut + '"', shell=True)
# Remove Temp Files
os.remove(upscaleOut + '.ffindex')
os.remove(upscaleOut + '.vpy')

42
utils/interpolate4k.py Normal file
View File

@@ -0,0 +1,42 @@
import os
import subprocess
def createInterpolateScript(upscaleOut, tempName):
if os.path.isfile(upscaleOut + '.vpy'):
print('Interpolate script exists')
return
script = ['from vsrife import rife',
'import vapoursynth as vs',
'from vapoursynth import core',
'clip = core.ffms2.Source(source="./' + tempName + ' [4k][HEVC].mkv")',
'clip = vs.core.resize.Bicubic(clip, width=3840, height=2160, format=vs.RGBS, matrix_in_s="709")',
'clip = rife(clip=clip, model="4.15.lite", factor_num=2, factor_den=1)',
'clip = vs.core.resize.Bicubic(clip, format=vs.YUV420P8, matrix_s="709")',
'clip.set_output()']
if not os.path.isfile(upscaleOut + '.vpy'):
with open(upscaleOut + '.vpy', 'a') as fs:
fs.writelines([i + '\n' for i in script])
def Interpolate4K(interpolateOut, upscaleOut, interpolateVideo, tempName):
if not interpolateVideo:
print('Skipped interpolation')
return
if os.path.isfile(interpolateOut):
print('Already interpolated')
return
createInterpolateScript(upscaleOut, tempName)
if not os.path.isfile(upscaleOut + '.vpy'):
print('=== Interpolation script not found ===')
return
print('Interpolating')
subprocess.call('vspipe -c y4m "' + upscaleOut + '.vpy" - | ffmpeg -v quiet -stats -i - -c:v hevc_nvenc -qp 5 "' + interpolateOut + '"', shell=True)
# Remove Temp Files
os.remove(upscaleOut + '.ffindex')
os.remove(upscaleOut + '.vpy')

30
utils/mediainfo.py Normal file
View File

@@ -0,0 +1,30 @@
from pymediainfo import MediaInfo
def get_aspect_ratio(inputFile):
media_info = MediaInfo.parse(inputFile)
for track in media_info.tracks:
if track.track_type == "Video":
aspect_ratio = track.other_display_aspect_ratio
print('Detected Aspect Ratio : ' + aspect_ratio[0])
return aspect_ratio[0]
# Fallback value
print('Falling Back To Aspect Ratio: 16:9')
return '16:9'
def get_framerate(inputFile):
media_info = MediaInfo.parse(inputFile)
for track in media_info.tracks:
if track.track_type == "Video":
frame_rate = track.frame_rate
if frame_rate == '29.970':
print(f"Detected Framerate : 30000/1001")
return '30000/1001'
if frame_rate == '24.000':
print(f"Detected Framerate : 24000/1000")
return '24000/1000'
print(f"Detected Framerate : 24000/1001")
return '24000/1001'

38
utils/upcale.py Normal file
View File

@@ -0,0 +1,38 @@
import os
import subprocess
from utils.mediainfo import get_framerate
def re_encode(input_file, upscale_out, temp_out, max_width, input_aspect):
if os.path.exists(upscale_out):
print('Skipped Pre-Encode')
return
command = (
f'ffmpeg -v quiet -stats -i "{input_file}" '
'-c:v ffv1 -level 3 '
f'-vf "fps={get_framerate(input_file)},scale=-1:\'min({max_width},ih)\'" -aspect {input_aspect} '
'-pix_fmt yuv420p -color_primaries 1 -color_trc 1 -colorspace 1 '
'-an -sn -map_metadata -1 '
f'"{temp_out}"'
)
subprocess.call(command, shell=True)
def upscale(input_file, upscale_out, max_width, input_aspect):
temp_out = os.path.join('1-Temp', 'source.mkv')
vsgan = os.path.join('utils', 'vs-realesrgan.vpy')
# Re-Encode to fix issues
re_encode(input_file, upscale_out, temp_out, max_width, input_aspect)
if os.path.exists(upscale_out):
print('Skipped Upscale')
return
print('Started Upscale')
subprocess.call(f'vspipe -c y4m {vsgan} - | ffmpeg -v quiet -stats -f yuv4mpegpipe -i - -c:v hevc_nvenc -qp 5 -aspect {input_aspect} "{upscale_out}"', shell=True)
# Remove Temp Files
os.remove(temp_out)
os.remove(temp_out + '.ffindex')

17
utils/vs-realesrgan.vpy Normal file
View File

@@ -0,0 +1,17 @@
import vapoursynth as vs
from vapoursynth import core
from vsrealesrgan import realesrgan
# Video Source
clip = core.ffms2.Source(source='../1-Temp/source.mkv')
# Set Input Colorspace
clip = core.resize.Bicubic(clip, format=vs.RGBS, matrix_in_s="709")
# Upscale
clip = realesrgan(clip=clip, model=4)
# Set Output Colorspace
clip = core.resize.Bicubic(clip, format=vs.YUV420P8, matrix_s="709")
clip.set_output()