Skip to content

Instantly share code, notes, and snippets.

@Renkai
Last active April 29, 2023 09:15
Show Gist options
  • Save Renkai/b678f60d4fa1bd8d03e205e2693fafdd to your computer and use it in GitHub Desktop.
Save Renkai/b678f60d4fa1bd8d03e205e2693fafdd to your computer and use it in GitHub Desktop.
Convert your bilibili local cache to mp4
import subprocess
import shlex
import json
import sys
from pathlib import Path
from numpy import fromfile, uint8 # pip install numpy
ROOT = Path(sys.argv[0]).resolve().parent
for dir in ROOT.iterdir():
if not dir.name.startswith("22"):
continue
metapath = Path.joinpath(dir, ".videoInfo")
print("dir",dir)
with open(metapath) as f:
meta = json.load(f)
# print("meta",meta)
title = meta["title"]
print("title", title)
for f in dir.rglob("**/*.m4s"):
read = fromfile(f, dtype=uint8)
if all(read[0:3] == [48, 48, 48]):
print("48!")
if '300' in f.name:
outfile = "video.mp4"
read[9:].tofile(ROOT / outfile)
if '302' in f.name:
outfile = "audio.mp3"
read[9:].tofile(ROOT / outfile)
else:
print("else")
print("read 0 9", read[0:9])
subprocess.call(shlex.split(f"ffmpeg -i video.mp4 -i audio.mp3 -codec copy {title}.mp4"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment