fixed and finish sponsor block

This commit is contained in:
Hellow 2023-06-14 17:43:20 +02:00
parent cc5c7809fb
commit 5c31d6f7a8
6 changed files with 29 additions and 18 deletions

View File

@ -4,6 +4,7 @@
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/.idea/music-downloader.iml" filepath="$PROJECT_DIR$/.idea/music-downloader.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/music-downloader.iml" filepath="$PROJECT_DIR$/.idea/music-downloader.iml" />
<module fileurl="file://$PROJECT_DIR$/../rythmbox-id3-lyrics-support/.idea/rythmbox-id3-lyrics-support.iml" filepath="$PROJECT_DIR$/../rythmbox-id3-lyrics-support/.idea/rythmbox-id3-lyrics-support.iml" /> <module fileurl="file://$PROJECT_DIR$/../rythmbox-id3-lyrics-support/.idea/rythmbox-id3-lyrics-support.iml" filepath="$PROJECT_DIR$/../rythmbox-id3-lyrics-support/.idea/rythmbox-id3-lyrics-support.iml" />
<module fileurl="file://$PROJECT_DIR$/../forks/sponsorblock.py/.idea/sponsorblock.py.iml" filepath="$PROJECT_DIR$/../forks/sponsorblock.py/.idea/sponsorblock.py.iml" />
</modules> </modules>
</component> </component>
</project> </project>

View File

@ -7,5 +7,6 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="rythmbox-id3-lyrics-support" /> <orderEntry type="module" module-name="rythmbox-id3-lyrics-support" />
<orderEntry type="module" module-name="sponsorblock.py" />
</component> </component>
</module> </module>

View File

@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" /> <mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/../forks/sponsorblock.py" vcs="Git" />
</component> </component>
</project> </project>

View File

@ -15,3 +15,5 @@ peewee~=3.15.4
ffmpeg-python~=0.2.0 ffmpeg-python~=0.2.0
platformdirs~=3.2.0 platformdirs~=3.2.0
transliterate~=1.10.2 transliterate~=1.10.2
sponsorblock~=0.1.3
regex~=2022.9.13

View File

@ -11,16 +11,16 @@ def remove_intervals(target: Target, interval_list: List[Tuple[float, float]]):
LOGGER.warning(f"Target doesn't exist: {target.file_path}") LOGGER.warning(f"Target doesn't exist: {target.file_path}")
return return
temp_target = Target( output_target = Target(
path=target._path, path=target._path,
file=str(target._file) + ".temp" file=str(target._file) + "." + AUDIO_FORMAT
) )
target.copy_content(temp_target)
# https://stackoverflow.com/questions/50594412/cut-multiple-parts-of-a-video-with-ffmpeg # https://stackoverflow.com/questions/50594412/cut-multiple-parts-of-a-video-with-ffmpeg
aselect_list: List[str] = [] aselect_list: List[str] = []
start = 0 start = 0
next_start = 0
for end, next_start in interval_list: for end, next_start in interval_list:
aselect_list.append(f"between(t,{start},{end})") aselect_list.append(f"between(t,{start},{end})")
@ -30,12 +30,12 @@ def remove_intervals(target: Target, interval_list: List[Tuple[float, float]]):
select = f"aselect='{'+'.join(aselect_list)}',asetpts=N/SR/TB" select = f"aselect='{'+'.join(aselect_list)}',asetpts=N/SR/TB"
command = f"ffmpeg -i {str(temp_target.file_path)} -af \"{select}\" {str(target.file_path)}" r = subprocess.run(["ffmpeg", "-i", str(target.file_path), "-af", select, str(output_target.file_path)])
r = subprocess.run(command)
if r.stderr != "": if r.stderr != "":
LOGGER.debug(r.stderr) LOGGER.debug(r.stderr)
temp_target.delete() output_target.copy_content(target)
output_target.delete()
def correct_codec(target: Target, bitrate_kb: int = BITRATE, audio_format: str = AUDIO_FORMAT): def correct_codec(target: Target, bitrate_kb: int = BITRATE, audio_format: str = AUDIO_FORMAT):

View File

@ -3,11 +3,9 @@ from urllib.parse import urlparse, urlunparse, parse_qs
from enum import Enum from enum import Enum
import sponsorblock import sponsorblock
from sponsorblock.errors import HTTPException, NotFoundException
from music_kraken.objects import Song, Target from ..objects import Source, DatabaseObject, Song, Target
from music_kraken.utils.support_classes import DownloadResult
from ..objects import Source, DatabaseObject
from .abstract import Page from .abstract import Page
from ..objects import ( from ..objects import (
Artist, Artist,
@ -145,7 +143,7 @@ class YouTube(Page):
) )
# the stuff with the connection is, to ensure sponsorblock uses the proxies, my programm does # the stuff with the connection is, to ensure sponsorblock uses the proxies, my programm does
_sponsorblock_connection: Connection = Connection() _sponsorblock_connection: Connection = Connection(host="https://sponsor.ajay.app/")
self.sponsorblock_client = sponsorblock.Client(session=_sponsorblock_connection.session) self.sponsorblock_client = sponsorblock.Client(session=_sponsorblock_connection.session)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -390,7 +388,7 @@ class YouTube(Page):
if not ENABLE_SPONSOR_BLOCK: if not ENABLE_SPONSOR_BLOCK:
return return
# find the youtube id # find the YouTube id
source_list = song.source_collection.get_sources_from_page(self.SOURCE_TYPE) source_list = song.source_collection.get_sources_from_page(self.SOURCE_TYPE)
if len(source_list) <= 0: if len(source_list) <= 0:
self.LOGGER.warning(f"Couldn't find a youtube source in the post_processing_hook for {song.option_string}") self.LOGGER.warning(f"Couldn't find a youtube source in the post_processing_hook for {song.option_string}")
@ -403,5 +401,13 @@ class YouTube(Page):
return return
# call the sponsorblock api, and remove the segments from the audio # call the sponsorblock api, and remove the segments from the audio
segments = self.sponsorblock_client.get_skip_segments(video_id=parsed.id) segments = []
try:
segments = self.sponsorblock_client.get_skip_segments(parsed.id)
except NotFoundException:
self.LOGGER.debug(f"No sponsor found for the video {parsed.id}.")
except HTTPException as e:
self.LOGGER.warning(f"{e}")
if len(segments) <= 0:
remove_intervals(temp_target, [(segment.start, segment.end) for segment in segments]) remove_intervals(temp_target, [(segment.start, segment.end) for segment in segments])