哔哩哔哩弹幕xml批量转json(DPlayer支持的格式)

php正则替换,可能很慢且很卡

先上代码

<?php
header("Content-type: text/html; charset=utf-8");
function ReplaceSpecialChar($C_char) {
        //过滤特殊字符
        $C_char = HTMLSpecialChars($C_char);
        //将特殊字元转成 HTML 格式
        $C_char = str_replace(",",",",$C_char);
        //替换英文逗号,
        $C_char = str_replace("<","《",$C_char);
        //替换英文小破折号<
        $C_char = str_replace(">","》",$C_char);
        //替换英文小破折号>
        $C_char = str_replace("'","~",$C_char);
        //替换英文单引号 '
        $C_char = str_replace("{","《",$C_char);
        //替换英文大括号{
        $C_char = str_replace("}","》",$C_char);
        //替换英文大括号}
        $C_char = str_replace("(","《",$C_char);
        //替换英文小括号(
        $C_char = str_replace(")","》",$C_char);
        //替换英文小括号)
        htmlentities($C_char,ENT_QUOTES);
        //替换英文双引号 "
        return $C_char;
        //返回处理结果
    }
    
function x2j($path) {
    $preg_p = '|<d p="(.*?)">|i';
    $preg_d = '|">(.*?)<\/d>|i';
    $preg_id = '|<chatid>(.*?)<\/chatid>|i';
    $fopath = 'xml/'.$path['basename'];
    $contents = file_get_contents($fopath);
    preg_match_all($preg_p,$contents,$p);
    preg_match_all($preg_d,$contents,$d);
    preg_match_all($preg_id,$contents,$id);
    $arrlength = count($p[1]);
    
    for ($i = 0;$i < $arrlength;$i++) {
        $pieces = explode(",", $p[1][$i]);
        $type = 0;
        if ($pieces[1] === '4') {
            $type = 2;
        } else if ($pieces[1] === '5') {
            $type = 1;
        }
        $addtime=0;
        $minustime=0;
        $time=floatval($pieces[0])+$addtime+$minustime;
        $data .= '['.$time.','. $type.','. $pieces[3].',"'. $pieces[6].'","'.ReplaceSpecialChar(preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F-\\\]/','',$d[1][$i])).'"],';
    }
    $newstr = substr($data,0,strlen($data)-1);
    $fstr = '{"code":0,"data":['.$newstr.']}';
    $fwpath = 'json/'.basename($path['basename'],'.xml').'.json';
    $res = file_put_contents($fwpath,$fstr);
}
function fRename($dirname) {
    if (!is_dir($dirname)) {
        echo "{$dirname}不是一个有效的目录!";
        exit();
    }
    $handle = opendir($dirname);
    while (($fn = readdir($handle)) !== false) {

        if ($fn != '.' && $fn != '..') {
            echo "<br>正在将:".$fn."\n\r";
            $curDir = $dirname.'/'.$fn;
            if (is_dir($curDir)) {
                fRename($curDir);
            } else {
                $path = pathinfo($curDir);
                echo "转换成:".basename($path['basename'],".xml").'.json'."\r\n";
                x2j($path);
            }
        }
    }
}
fRename('xml/');
echo "<br><strong style='color:green'>Done!</strong>";
exit();
?>

使用方法

  1. 将上方代码复制另存为fx2j.php文件
  2. 然后在php文件所在目录新建两个文件夹:xmljson
  3. 修改json文件夹权限为777,即可写状态
  4. 将需要转换的xml文件放入xml文件夹
  5. 浏览器访问fx2j.php文件

在线转换

以前从bilibilijj下载的xml文件可以用这个接口:
https://api.menhood.wang/xml2json/index.php?url=

加载B站下线弹幕的例子

https://api.menhood.wang/xml2json/index.php?url=https://api.menhood.wang/xml2json/2.xml

直接获取B站弹幕数据用DIYgod的接口即可:
https://api.prprpr.me/dplayer/v3/bilibili?aid=

用aid加载B站在线弹幕的例子

https://api.prprpr.me/dplayer/v3/bilibili?aid=15670164

https://api.prprpr.me/dplayer/v3/bilibili?cid=

用cid加载B站在线弹幕的例子

https://api.prprpr.me/dplayer/v3/bilibili?cid=25494475

标签: DPlayer, php, 哔哩哔哩, 弹幕, 格式, 转换