【PHP】YouTube JSON字幕格式转srt格式并下载

<?php
function formatNum(int $time)
{
    if ($time < 10) {
        return "0{$time}";
    }
    return $time;
}
function changeTimeType(int $num): string
{
    $millisecond = substr($num, -3);
    $temp = intval($num / 1000);
    $second = formatNum($temp % 60);
    $temp = intval($temp / 60);
    $minute = formatNum($temp % 60);
    $temp = intval($temp / 60);
    $hour = formatNum($temp % 60);
    return "{$hour}:{$minute}:{$second},{$millisecond}";
}

function transSingleSubtitle($sequence,$start,$end,$subtitle): string
{
    return "{$sequence}\n{$start} --> {$end}\n{$subtitle}\n\n";
}

function transSubtitle($subtitle): string
{
    $key = 1;
    $srt = '';
    foreach ($subtitle as $v) {
        $start = changeTimeType($v['tStartMs']);
        $end = changeTimeType($v['tStartMs'] + $v['dDurationMs']);
        $content = trim($v['segs'][0]['utf8']);
        $srt .= transSingleSubtitle($key, $start, $end, $content);
        $key++;
    }
    return $srt;
}

if (!empty($srt = $_POST['srt']) && strlen($srt)>0) {
    $srt = json_decode($srt, true);
    $subtitle = $srt['events'];
    $ua = $_SERVER["HTTP_USER_AGENT"];
    $filename = date('YmdHis');
    $filename = "{$filename}.srt";
    $encoded_filename = urlencode($filename);
    $encoded_filename = str_replace("+", "%20", $encoded_filename);
    header("Content-Type: application/octet-stream");
    if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
        header('Content-Disposition:  attachment; filename="' . $encoded_filename . '"');
    } elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
        header('Content-Disposition: attachment; filename*="utf8' .  $filename . '"');
    } else {
        header('Content-Disposition: attachment; filename="' .  $filename . '"');
    }
    echo $srt = transSubtitle($subtitle);
    exit();
}
?>
    <!doctype html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>YouTube字幕下载</title>
        <link rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css">
    </head>
    <body>
    <form method="post" class="layui-form" action="">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">输入字幕JSON</label>
            <div class="layui-input-block">
                <textarea rows="10" lay-verify="required" name="srt" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">提交</button>
            </div>
        </div>
    </form>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://www.layuicdn.com/layui/layui.js"></script>
    <script>
        function isJSON(str) {
            if (typeof str == 'string') {
                try {
                    let obj = JSON.parse(str);
                    return !!(typeof obj == 'object' && obj);
                } catch(e) {
                    console.log('error:' + str + '!!!' + e);
                    return false;
                }
            }
            console.log('It is not a string!')
            return false;
        }
        //Demo
        layui.use('form', function(){
            let form = layui.form;
            //监听提交
            form.on('submit(formDemo)', function(data){
                let srt = data.field.srt;
                if (!isJSON(srt)) {
                    layer.msg("请输入有效的JSON格式", {icon: 2, anim: 6});
                    return false;
                }
                srt = JSON.parse(srt);
                if (!srt.hasOwnProperty('events')) {
                    layer.msg("没有[events]", {icon: 2, anim: 6});
                    return false;
                }
                return true;
            });
        });
    </script>
    </body>
    </html>

 

posted @ 2022-11-16 19:54  _迷途  阅读(218)  评论(0编辑  收藏  举报