Compare commits

...

7 Commits

Author SHA1 Message Date
alacleaker 54d640230a bump version 2024-04-08 18:25:10 -03:00
alacleaker 3d272a6891 use m4v with ffmpeg when compatible codecs 2024-04-08 17:29:45 -03:00
alacleaker e99ed0eb5a fix wrong format and rename to use_mp4_format 2024-04-08 17:00:20 -03:00
alacleaker 86b5029773 add stream info codec to remux 2024-04-08 16:59:44 -03:00
alacleaker 3df0a91d3f adjust mp4 flag codecs 2024-04-08 16:54:17 -03:00
alacleaker d356596cf4 add codec to StreamInfo 2024-04-08 16:54:03 -03:00
alacleaker cbd2df79b7 add -f mp4 for atmos 2024-04-08 16:46:36 -03:00
5 changed files with 60 additions and 31 deletions
+1 -1
View File
@@ -1 +1 @@
__version__ = "2.0"
__version__ = "2.1"
+7 -1
View File
@@ -527,7 +527,11 @@ def main(
encrypted_path, decrypted_path, decryption_key
)
logger.debug(f"Remuxing to {final_path}")
downloader_song.remux(decrypted_path, remuxed_path)
downloader_song.remux(
decrypted_path,
remuxed_path,
stream_info.codec,
)
logger.debug("Applying tags")
downloader.apply_tags(remuxed_path, tags, cover_url)
logger.debug(f"Moving to {final_path}")
@@ -635,6 +639,8 @@ def main(
decrypted_path_video,
decrypted_path_audio,
remuxed_path,
stream_info_video.codec,
stream_info_audio.codec,
)
logger.debug("Applying tags")
downloader.apply_tags(remuxed_path, tags, cover_url)
+35 -25
View File
@@ -1,3 +1,4 @@
import re
import subprocess
import urllib.parse
from pathlib import Path
@@ -13,6 +14,8 @@ from .models import StreamInfo
class DownloaderMusicVideo:
MP4_FORMAT_CODECS = ["hvc1", "ec-3"]
def __init__(
self,
downloader: Downloader,
@@ -33,10 +36,10 @@ class DownloaderMusicVideo:
).geturl()
return m3u8.load(stream_url_master_new).data
def get_stream_url_video(
def get_playlist_video(
self,
playlists: list[dict],
):
) -> dict:
playlists_filtered = [
playlist
for playlist in playlists
@@ -53,12 +56,12 @@ class DownloaderMusicVideo:
)
]
playlists_filtered.sort(key=lambda x: x["stream_info"]["bandwidth"])
return playlists_filtered[-1]["uri"]
return playlists_filtered[-1]
def get_stream_url_video_from_user(
def get_playlist_video_from_user(
self,
playlists: list[dict],
):
) -> dict:
table = [
[
i,
@@ -76,12 +79,12 @@ class DownloaderMusicVideo:
)
except click.exceptions.Abort:
raise KeyboardInterrupt()
return playlists[choice]["uri"]
return playlists[choice]
def get_stream_url_audio(
def get_playlist_audio(
self,
playlists: list[dict],
) -> str:
) -> dict:
stream_url = next(
(
playlist
@@ -89,13 +92,13 @@ class DownloaderMusicVideo:
if playlist["group_id"] == "audio-stereo-256"
),
None,
)["uri"]
)
return stream_url
def get_stream_url_audio_from_user(
def get_playlist_audio_from_user(
self,
playlists: list[dict],
):
) -> dict:
table = [
[
i,
@@ -113,7 +116,7 @@ class DownloaderMusicVideo:
)
except click.exceptions.Abort:
raise KeyboardInterrupt()
return playlists[choice]["uri"]
return playlists[choice]
def get_pssh(self, m3u8_data: dict):
return next(
@@ -128,13 +131,11 @@ class DownloaderMusicVideo:
def get_stream_info_video(self, m3u8_master_data: dict) -> StreamInfo:
stream_info = StreamInfo()
if self.codec != MusicVideoCodec.ASK:
stream_info.stream_url = self.get_stream_url_video(
m3u8_master_data["playlists"]
)
playlist = self.get_playlist_video(m3u8_master_data["playlists"])
else:
stream_info.stream_url = self.get_stream_url_video_from_user(
m3u8_master_data["playlists"]
)
playlist = self.get_playlist_video_from_user(m3u8_master_data["playlists"])
stream_info.stream_url = playlist["uri"]
stream_info.codec = playlist["stream_info"]["codecs"]
m3u8_data = m3u8.load(stream_info.stream_url).data
stream_info.pssh = self.get_pssh(m3u8_data)
return stream_info
@@ -142,13 +143,13 @@ class DownloaderMusicVideo:
def get_stream_info_audio(self, m3u8_master_data: dict) -> StreamInfo:
stream_info = StreamInfo()
if self.codec != MusicVideoCodec.ASK:
stream_info.stream_url = self.get_stream_url_audio(
m3u8_master_data["media"]
)
playlist = self.get_playlist_audio(m3u8_master_data["media"])
else:
stream_info.stream_url = self.get_stream_url_audio_from_user(
m3u8_master_data["media"]
)
playlist = self.get_playlist_audio_from_user(m3u8_master_data["media"])
stream_info.stream_url = playlist["uri"]
stream_info.codec = re.search(r"_([^_]+)\.m3u8", stream_info.stream_url).group(
1
)
m3u8_data = m3u8.load(stream_info.stream_url).data
stream_info.pssh = self.get_pssh(m3u8_data)
return stream_info
@@ -255,7 +256,12 @@ class DownloaderMusicVideo:
decrypted_path_video: Path,
decrypte_path_audio: Path,
fixed_path: Path,
codec_video: str,
codec_audio: str,
):
use_mp4_flag = any(
codec_video.startswith(codec) for codec in self.MP4_FORMAT_CODECS
) or any(codec_audio.startswith(codec) for codec in self.MP4_FORMAT_CODECS)
subprocess.run(
[
self.downloader.ffmpeg_path_full,
@@ -269,7 +275,7 @@ class DownloaderMusicVideo:
"-movflags",
"+faststart",
"-f",
"mp4",
"mp4" if use_mp4_flag else "ipod",
"-c",
"copy",
"-c:s",
@@ -284,6 +290,8 @@ class DownloaderMusicVideo:
decrypted_path_video: Path,
decrypted_path_audio: Path,
remuxed_path: Path,
codec_video: str,
codec_audio: str,
):
if self.downloader.remux_mode == RemuxMode.MP4BOX:
self.remux_mp4box(
@@ -296,6 +304,8 @@ class DownloaderMusicVideo:
decrypted_path_video,
decrypted_path_audio,
remuxed_path,
codec_video,
codec_audio,
)
def get_cover_path(self, final_path: Path) -> Path:
+16 -4
View File
@@ -19,6 +19,7 @@ from .models import Lyrics, StreamInfo
class DownloaderSong:
DEFAULT_DECRYPTION_KEY = "32b8ade1769e26b1ffb8986352793fc6"
MP4_FORMAT_CODECS = ["ec-3"]
def __init__(
self,
@@ -123,6 +124,7 @@ class DownloaderSong:
drm_ids = asset_infos[variant_id]["AUDIO-SESSION-KEY-IDS"]
pssh = self.get_pssh(drm_infos, drm_ids)
stream_info.pssh = pssh
stream_info.codec = playlist["stream_info"]["codecs"]
return stream_info
@staticmethod
@@ -294,13 +296,13 @@ class DownloaderSong:
check=True,
)
def remux(self, decrypted_path: Path, remuxed_path: Path) -> None:
def remux(self, decrypted_path: Path, remuxed_path: Path, codec: str):
if self.downloader.remux_mode == RemuxMode.MP4BOX:
self.remux_mp4box(decrypted_path, remuxed_path)
elif self.downloader.remux_mode == RemuxMode.FFMPEG:
self.remux_ffmpeg(decrypted_path, remuxed_path)
self.remux_ffmpeg(decrypted_path, remuxed_path, codec)
def remux_mp4box(self, decrypted_path: Path, remuxed_path: Path) -> None:
def remux_mp4box(self, decrypted_path: Path, remuxed_path: Path):
subprocess.run(
[
self.downloader.mp4box_path_full,
@@ -315,7 +317,15 @@ class DownloaderSong:
check=True,
)
def remux_ffmpeg(self, decrypted_path: Path, remuxed_path: Path) -> None:
def remux_ffmpeg(
self,
decrypted_path: Path,
remuxed_path: Path,
codec: str,
):
use_mp4_format = any(
codec.startswith(possible_codec) for possible_codec in self.MP4_FORMAT_CODECS
)
subprocess.run(
[
self.downloader.ffmpeg_path_full,
@@ -326,6 +336,8 @@ class DownloaderSong:
decrypted_path,
"-c",
"copy",
"-f",
"mp4" if use_mp4_format else "ipod",
"-movflags",
"+faststart",
remuxed_path,
+1
View File
@@ -23,3 +23,4 @@ class Lyrics:
class StreamInfo:
stream_url: str = None
pssh: str = None
codec: str = None