Skip to content

Commit

Permalink
Add headers support for FFmpeg recording
Browse files Browse the repository at this point in the history
Extended the FFmpeg recorder to support custom headers. Updated configuration file and logic to pass headers from the app to the FFmpeg process. This allows more flexible and secure handling of streams requiring specific headers.

Signed-off-by: hldh214 <[email protected]>
  • Loading branch information
hldh214 committed Sep 11, 2024
1 parent 1bf5a03 commit c0490c4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sess_key = "018c6d30a09720dd7aba6a9113fa5227bc9c78e033eb1a2be5fec88af599e3c6"
max_size = "1989M"
save_metadata = false
proxy = "http://"
headers = ["Origin: https://m.pandalive.co.kr", "Referer: https://m.pandalive.co.kr/"]

[source]
[source."668668"]
Expand Down
5 changes: 4 additions & 1 deletion recorder/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def record_thread(source_type, room_id, interval=5, **kwargs):
max_duration = kwargs['app'].get('max_duration', 0)
max_size = kwargs['app'].get('max_size', 0)
save_metadata = kwargs['app'].get('save_metadata', False)
headers = None

# overwrite app's config by source_type's config
if source_type in kwargs:
Expand All @@ -54,6 +55,8 @@ def record_thread(source_type, room_id, interval=5, **kwargs):
max_size = kwargs[source_type]['max_size']
if kwargs[source_type].get('save_metadata'):
save_metadata = kwargs[source_type]['save_metadata']
if kwargs[source_type].get('headers'):
headers = kwargs[source_type]['headers']

while True:
stream = source.get_stream(room_id, **kwargs)
Expand All @@ -76,7 +79,7 @@ def record_thread(source_type, room_id, interval=5, **kwargs):

logger.info(f'recording: {url} -> {output_file}')

exit_code = ffmpeg.record(url, output_file, max_duration, max_size)
exit_code = ffmpeg.record(url, output_file, max_duration, max_size, headers=headers)
logger.info(f'({kwargs["source_name"]})recorded with exit_code {exit_code}: {url}')

if not ffmpeg.valid(output_file):
Expand Down
10 changes: 7 additions & 3 deletions recorder/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@
)


def record(input_url, output_file, max_duration, max_size, args=None):
def record(input_url, output_file, max_duration, max_size, headers=None, args=None):
popen_args = [
FFMPEG_BINARY, '-y', '-user_agent', random.choice(USER_AGENTS), '-hide_banner',
# If set, then even streamed/non seekable streams will be reconnected on errors.
'-reconnect_streamed', '1',
# Sets the maximum delay in seconds after which to give up reconnecting.
'-reconnect_delay_max', '16',
'-rw_timeout', TIMEOUT_US, '-timeout', TIMEOUT_US,
'-i', input_url, '-c', 'copy'
'-rw_timeout', TIMEOUT_US, '-timeout', TIMEOUT_US
]

if headers is not None:
popen_args.extend(['-headers', headers.join('\n')])

popen_args.extend(['-i', input_url, '-c', 'copy'])

if max_size:
popen_args.extend(['-fs', str(max_size)])

Expand Down

0 comments on commit c0490c4

Please sign in to comment.