Skip to content

Commit

Permalink
Set can_copy_video and can_device_direct_play as variables instead of…
Browse files Browse the repository at this point in the history
… functions since they got called many times and the logging got annoying to look at
  • Loading branch information
thomaserlang committed Dec 9, 2023
1 parent e1d5574 commit e655d37
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
3 changes: 1 addition & 2 deletions seplis_play_server/routes/request_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ async def request_media(

t = Transcoder(settings=settings, metadata=metadata[source_index])

can_device_direct_play = t.can_device_direct_play()
format_supported = any(fmt in settings.supported_video_containers \
for fmt in metadata[source_index]['format']['format_name'].split(','))
return Request_media(
direct_play_url=f'/source?play_id={settings.play_id}&source_index={source_index}',
can_direct_play=format_supported and can_device_direct_play and t.can_copy_audio(),
can_direct_play=format_supported and t.can_device_direct_play and t.can_copy_audio(),
transcode_url=f'/hls/media.m3u8?{urlencode(settings.to_args_dict())}',
)
3 changes: 2 additions & 1 deletion seplis_play_server/transcoders/hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def ffmpeg_extend_args(self) -> None:
{'-hls_time': str(self.segment_time())},
{'-hls_list_size': '0'},
{'-start_number': str(self.settings.start_segment or 0)},
{'-y': None},
{self.media_path: None},
])

Expand Down Expand Up @@ -100,7 +101,7 @@ def generate_hls_playlist(self):
return '\n'.join(l)

def get_segments(self):
if self.can_copy_video():
if self.can_copy_video:
return self.calculate_keyframe_segments()
else:
return self.calculate_equal_segments()
Expand Down
2 changes: 1 addition & 1 deletion seplis_play_server/transcoders/subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict
from aiofile import async_open
from seplis_play_server import config, logger, models, database
from .video import stream_index_by_lang, subprocess_env, to_subprocess_arguments
from .video import stream_index_by_lang, to_subprocess_arguments

async def get_subtitle_file(metadata: Dict, lang: str, start_time: int):
if not lang:
Expand Down
44 changes: 30 additions & 14 deletions seplis_play_server/transcoders/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def __init__(self, settings: Transcode_settings, metadata: Dict):
self.input_codec = self.video_stream['codec_name']
self.video_color = get_video_color(self.video_stream)
self.video_color_bit_depth = get_video_color_bit_depth(self.video_stream)
self.can_device_direct_play = self.get_can_device_direct_play()
self.can_copy_video = self.get_can_copy_video()
self.output_codec_lib = None
self.ffmpeg_args = None
self.transcode_folder = None
Expand Down Expand Up @@ -182,10 +184,6 @@ async def set_ffmpeg_args(self):
{'-map_metadata': '-1'},
{'-map_chapters': '-1'},
{'-threads': '0'},
{'-start_at_zero': None},
#{'-copyts': None},
{'-avoid_negative_ts': 'disabled'},
{'-muxdelay': '0'},
{'-max_delay': '5000000'},
{'-max_muxing_queue_size': '2048'},
])
Expand All @@ -209,6 +207,9 @@ def closest_keyframe_time(self, time: float):
def set_hardware_decoder(self):
if not config.ffmpeg_hwaccel_enabled:
return

if self.can_copy_video:
return

if config.ffmpeg_hwaccel == 'qsv':
self.ffmpeg_args.extend([
Expand All @@ -229,12 +230,23 @@ def set_hardware_decoder(self):
def set_video(self):
codec = codecs_to_library.get(self.settings.transcode_video_codec, self.settings.transcode_video_codec)

if self.can_copy_video():
if self.can_copy_video:
codec = 'copy'
self.ffmpeg_args.insert(0, {'-noaccurate_seek': None})
if self.settings.start_time > 0:
i = self.find_ffmpeg_arg_index('-ss')
# Audio goes out if sync
self.ffmpeg_args.insert(i+1, {'-noaccurate_seek': None})

self.ffmpeg_args.insert(i+2, {'-fflags': '+genpts'})

self.ffmpeg_args.extend([
{'-start_at_zero': None},
{'-avoid_negative_ts': 'disabled'},
])
else:
if config.ffmpeg_hwaccel_enabled:
codec = f'{self.settings.transcode_video_codec}_{config.ffmpeg_hwaccel}'

self.output_codec_lib = codec
self.ffmpeg_args.extend([
{'-map': '0:v:0'},
Expand All @@ -261,8 +273,8 @@ def set_video(self):
self.ffmpeg_args.append({'-vf': ','.join(vf)})
self.ffmpeg_args.extend(self.get_quality_params(width, codec))

def can_copy_video(self):
if not self.can_device_direct_play():
def get_can_copy_video(self):
if not self.can_device_direct_play:
return False

# We need the key frames to determin the actually start time when seeking
Expand All @@ -274,7 +286,7 @@ def can_copy_video(self):
logger.debug(f'[{self.settings.session}] Can copy video, codec: {self.input_codec}')
return True

def can_device_direct_play(self):
def get_can_device_direct_play(self):
if self.input_codec not in self.settings.supported_video_codecs:
logger.debug(f'[{self.settings.session}] Input codec not supported: {self.input_codec}')
return False
Expand All @@ -296,7 +308,7 @@ def can_device_direct_play(self):
return False

return True

def get_video_filter(self, width: int):
vf = []
if self.video_color_bit_depth <= self.settings.supported_video_color_bit_depth:
Expand Down Expand Up @@ -425,10 +437,9 @@ def set_audio(self):
stream = self.metadata['streams'][index.index]
codec = codecs_to_library.get(stream['codec_name'], '')

# Audio goes out of sync audio copy is used while the video is being transcoded
if self.can_copy_video() and self.can_copy_audio(stream):
# Audio goes out of sync if audio copy is used while the video is being transcoded
if self.can_copy_video and self.can_copy_audio(stream):
codec = 'copy'
self.ffmpeg_args.insert(5, {'-noaccurate_seek': None})
else:
if not codec or codec not in self.settings.supported_audio_codecs:
codec = codecs_to_library.get(self.settings.transcode_audio_codec, '')
Expand Down Expand Up @@ -541,14 +552,19 @@ def change_ffmpeg_arg(self, key, new_value):
a[key] = new_value
break

def find_ffmpeg_arg_index(self, key):
for i, a in enumerate(self.ffmpeg_args):
if key in a:
return i

def create_transcode_folder(self):
transcode_folder = os.path.join(config.transcode_folder, self.settings.session)
if not os.path.exists(transcode_folder):
os.makedirs(transcode_folder)
return transcode_folder

def segment_time(self):
return 6 if self.can_copy_video() else 3
return 6 if self.get_can_copy_video() else 3


def subprocess_env(session, type_):
Expand Down

0 comments on commit e655d37

Please sign in to comment.