Skip to content

Commit

Permalink
fix: ivitools
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo2011 committed Aug 28, 2024
1 parent c966641 commit 19524c3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
6 changes: 4 additions & 2 deletions bilibili_api/tools/ivitools/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import httpx
from colorama import Fore

from bilibili_api import sync, interactive_video, Credential
from bilibili_api import sync, interactive_video, video

def download_interactive_video(bvid: str, out: str):
ivideo = interactive_video.InteractiveVideo(bvid)
downloader = interactive_video.InteractiveVideoDownloader(
ivideo, out
ivideo, out, stream_detecting_params={
"codecs": [video.VideoCodecs.AVC]
}
)

@downloader.on("START")
Expand Down
32 changes: 25 additions & 7 deletions bilibili_api/tools/ivitools/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ def setup(self, Form):
self.player = QtMultimediaWidgets.QVideoWidget(Form)
self.player.setGeometry(QtCore.QRect(0, 0, 800, 450))
self.player.setObjectName("player")
self.videoplayer = QtMultimedia.QMediaPlayer()
self.videoplayer.setVideoOutput(self.player)
self.mediaplayer = QtMultimedia.QMediaPlayer()
self.mediaplayer.setVideoOutput(self.player)
self.audio_output = QtMultimedia.QAudioOutput()
self.audio_output.setVolume(0.0)
self.mediaplayer.setAudioOutput(self.audio_output)
Expand Down Expand Up @@ -514,6 +515,7 @@ def get_info(node_id: int):
) and (not self.has_end):
self.has_end = True
self.mediaplayer.pause()
self.videoplayer.pause()
self.final_position = self.mediaplayer.position()
self.mediaplayer.setAudioOutput(
QtMultimedia.QAudioOutput().setVolume(0)
Expand All @@ -540,6 +542,7 @@ def get_info(node_id: int):
self.mediaplayer.setAudioOutput(
QtMultimedia.QAudioOutput().setVolume(0)
)
self.videoplayer.setPosition(self.final_position)
duration = self.mediaplayer.duration() // 1000
duration_sec = duration % 60
duration_min = duration // 60
Expand Down Expand Up @@ -621,14 +624,17 @@ def mouseReleaseEvent(event: QtGui.QMouseEvent):
self.win.mouseReleaseEvent = mouseReleaseEvent

def start_playing(self):
self.videoplayer.play()
self.mediaplayer.play()
self.is_stoping = False

def stop_playing(self):
self.videoplayer.stop()
self.mediaplayer.stop()
self.is_stoping = True

def pause_playing(self):
self.videoplayer.pause()
self.mediaplayer.pause()
self.is_stoping = True

Expand Down Expand Up @@ -661,11 +667,16 @@ def set_source(self, cid: int):
)
self.stop_playing()
self.pp.setText("Pause")
dest = self.temp_dir + str(cid) + ".mp4"
dest = self.temp_dir + str(cid) + ".audio.mp4"
dest_v = self.temp_dir + str(cid) + ".video.mp4"
self.mediaplayer.setSource(QtCore.QUrl(dest))
self.mediaplayer.setPosition(0)
if self.mediaplayer.duration() <= 7:
self.mediaplayer.setPosition(self.mediaplayer.duration())
self.videoplayer.setSource(QtCore.QUrl(dest_v))
self.videoplayer.setPosition(0)
if self.videoplayer.duration() <= 7:
self.videoplayer.setPosition(self.videoplayer.duration())
self.start_playing()

def extract_ivi(self, path: str):
Expand Down Expand Up @@ -693,15 +704,17 @@ def extract_ivi(self, path: str):
self.graph = json.load(
open(self.temp_dir + "ivideo.json", "r", encoding="utf-8")
)
self.current_node = 1
variables = self.graph["1"]["vars"]
self.current_node = bilivideo_parser.decode(
open(self.temp_dir + "bilivideo.json", "r", encoding="utf-8").read()
)["root_id"]
variables = self.graph[str(self.current_node)]["vars"]
for var in variables:
self.variables.append(
InteractiveVariable(
var["name"], var["id"], var["value"], var["show"], var["random"]
)
)
self.set_source(self.graph["1"]["cid"])
self.set_source(self.graph[str(self.current_node)]["cid"])
self.volume_change_event()

def close_ivi(self):
Expand All @@ -722,10 +735,12 @@ def close_ivi(self):
self.pp.setText("Pause")
self.has_end = False
self.mediaplayer = QtMultimedia.QMediaPlayer() # Clear the multimedia source
self.mediaplayer.setVideoOutput(self.player)
self.mediaplayer.setAudioOutput(QtMultimedia.QAudioOutput())
self.videoplayer = QtMultimedia.QMediaPlayer()
self.videoplayer.setVideoOutput(self.player)
self.volume_change_event()
shutil.rmtree(self.temp_dir)
if os.path.exists(self.temp_dir):
shutil.rmtree(self.temp_dir)
while True:
if not os.path.exists(self.temp_dir):
break
Expand Down Expand Up @@ -781,10 +796,12 @@ def volume_change_event(self):
pass
else:
self.mediaplayer.setPosition(position)
self.videoplayer.setPosition(position)
self.start_playing()

def position_start_change_event(self):
self.mediaplayer.pause()
self.videoplayer.pause()
self.is_draging_slider = True

def position_change_event(self):
Expand All @@ -793,6 +810,7 @@ def position_change_event(self):
self.slider.setValue(100)
return
self.mediaplayer.setPosition(int(self.mediaplayer.duration() * volume / 100))
self.videoplayer.setPosition(int(self.videoplayer.duration() * volume / 100))
if not self.is_stoping:
self.start_playing()
self.is_draging_slider = False
Expand Down
5 changes: 3 additions & 2 deletions bilibili_api/tools/ivitools/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ def scan_ivi_file(path: str):
bar.set_description("Scanning node ")
if not ("cid" in graph[str(item)].keys()):
raise Exception(f"Missing CID in the node {item}")
return
else:
cid = graph[str(item)]["cid"]
if not (os.path.exists(os.path.join(extract_dir, str(cid) + ".mp4"))):
if not (os.path.exists(os.path.join(extract_dir, str(cid) + ".video.mp4"))):
raise Exception(f"Missing video source of the node {item}")
if not (os.path.exists(os.path.join(extract_dir, str(cid) + ".audio.mp4"))):
raise Exception(f"Missing video source of the node {item}")
time.sleep(0.01)
print(Fore.GREEN + "Congratulation! Your file is OK. ", Fore.RESET)

0 comments on commit 19524c3

Please sign in to comment.