Skip to content

Commit

Permalink
feat: add mp4 defragmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianNoise committed Oct 17, 2024
1 parent 12eea3d commit 8eac72a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions dl-downer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ ENV AUTH_VRTMAX_EMAIL=
ENV AUTH_VRTMAX_PASSWORD=
ENV AUTH_GOPLAY_EMAIL=
ENV AUTH_GOPLAY_PASSWORD=
ENV AUTH_VTMGO_EMAIL=
ENV AUTH_VTMGO_PASSWORD=

ENV DL_GOPLAY_MERGE_METHOD=period

ENV PUID=6969
ENV PGID=6969
Expand Down
5 changes: 3 additions & 2 deletions dl-downer/src/downloaders/GOPLAY.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ def GOPLAY_DL(dl_request: DLRequest):
if len(keys) > 0:
download_options.decrypt_keys = keys
# ignore all periods prefixed with 'pre-roll'
download_options.ignore_periods = [ '^pre-roll.*' ]
download_options.ignore_periods = [ '^pre-roll.*', '^mid-roll.*' ]
# merge the files per format
download_options.merge_method = 'format'
merge_method = os.getenv('DL_GOPLAY_MERGE_METHOD', 'format')
download_options.merge_method = merge_method
# download the mpd
final_file = mpd.download('./tmp', download_options)
# move the final file to the downloads folder
Expand Down
7 changes: 6 additions & 1 deletion dl-downer/src/mpd/adaptation_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .segment_template import SegmentTemplate
from .representation import Representation
from .mpd_download_options import MPDDownloadOptions
from ..utils.files import decrypt_file
from ..utils.files import decrypt_file, defragment_mp4

class AdaptationSet:
def __init__(
Expand Down Expand Up @@ -181,6 +181,11 @@ def download(
# replace created_file with decrypted_file
created_file = decrypted_file

# defragment the file, just in case
defragmented_file = os.path.join(my_tmp_dir, f'defrag-{os.path.basename(created_file)}')
defragment_mp4(created_file, defragmented_file)
created_file = defragmented_file

# Move the file to the parent folder
mime_type_escaped = re.sub(r'[^a-zA-Z0-9]', '-', self.mime_type)
out_file = os.path.join(tmp_dir, f'{mime_type_escaped}-{os.path.basename(created_file)}')
Expand Down
14 changes: 14 additions & 0 deletions dl-downer/src/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,17 @@ def convert_to_mkv(
os.remove(input_file)

return output_file

def defragment_mp4(
input_file: str,
output_file: str,
):
command = [ 'ffmpeg',
'-i', input_file,
'-c', 'copy',
output_file,
]

logger.info(f'Defragmenting {input_file}...')
subprocess.run(command, check=True)
logger.info(f'Defragmented successfully!')

0 comments on commit 8eac72a

Please sign in to comment.