Something about ts seg dec

Something about ts seg dec

Tools

  • powershell execution enviroment
  • ffmpeg, ffplay
  • nodejs
  • m3u8dl(or internet_download_manager as idman)

Steps

  • Paste the news url address to console window
  • Analyze content to obtain res ID
  • Get res info from api interface
  • Get the h5e enc adress
  • Replace to high quality address
  • Download m3u8 address
  • Download ts segments as enc res
  • Decode the resources and save to dec folder
  • Preview the high quality video from cache folder
  • Merge dec resources as mp4 file

Code

execution_as_ps.bat

Param ( $MainEntry )

$oScript = @"
    @echo off
    cls
    pushd %~dp0
    >%tmp%\%~n0.ps1 more +0 %~s0
    powershell -Command "Set-ExecutionPolicy -Scope Process Bypass; . %tmp%\%~n0.ps1 -MainEntry '%cd%'"
    goto :EOF
"@

function pause() {
    Write-Host "Press any key to contiune..."
    [Console]::ReadKey() | Out-Null
}

function GetVID() {
    Param( $oPage )

    $strRegInnerContent = ".* guid(_0)* *=.*"
    $strRet = ""
    $bRet = $oPage.Content -Match $strRegInnerContent
    if ($bRet) {
        $strInnerCon = $Matches[0]
        $strReg = ".* guid(_0)* *= *\`"([^`"]*)\`".*"
        $bRet = $strInnerCon -match $strReg
        if ($bRet) {
            $strRet = $Matches[2] 
        }
    }

    return $strRet
}

function Download() {
    Param ( $strUrl, $strFile, $bUseTool = $false )

    if (!$bUseTool) {
        Invoke-WebRequest -Uri "$strUrl" -Method GET -OutFile "$strFile"
    } else {
        $strCMD = "/d `"$strUrl`" /p `"${Script:WORK_ROOT}/cache/enc`" /f `"$(Split-Path -Leaf "$strFile")`" /n /q"
        #$strCMD
        Start idman -Args $strCMD -Wait
    }
}

function DownloadAndDec() {
    Param ($strUrl, $strTitle)

    Write-Host "Current action:"
    Write-Host "`tDownloading: ${strTitle}"
    if (!(Test-Path "${Script:WORK_ROOT}")) { mkdir "${Script:WORK_ROOT}" }
    if ((Test-Path "${Script:WORK_ROOT}/cache/enc")) { rm -Recurse -Force "${Script:WORK_ROOT}/cache/enc" }
    if ((Test-Path "${Script:WORK_ROOT}/cache/dec")) { rm -Recurse -Force "${Script:WORK_ROOT}/cache/dec" }
    mkdir "${Script:WORK_ROOT}/cache/enc"
    mkdir "${Script:WORK_ROOT}/cache/dec"

    Download -strUrl "$strUrl" -strFile "${Script:WORK_ROOT}/cache/dec/list.m3u8"
    m3u8dl "$strUrl" --workDir "${Script:WORK_ROOT}" --noMerge --saveName cache
    # Step 1: Download encrypt segments
    $arrListUrl = @()
    cat "${Script:WORK_ROOT}/cache/dec/list.m3u8" | findstr /v "^#" | %{
        $strFile = $_.Trim()
        Write-Host "Current file: $strFile"
        $strFileUrl = $strUrl -Replace "2000.m3u8.*", "$strFile"
        Write-Host "Current file url: $strFileUrl"

        $oItem = @{
            strFile = $strFile
            strFileUrl = $strFileUrl
        }
        $arrListUrl += $oItem
    }
    #pause
    $nCodecLength = ([string]$arrListUrl.Length).Length
    for ($nIndex = 0; $nIndex -lt $arrListUrl.Length; $nIndex++) {
        Write-Host "=====================Segment Process Mission========================="

        Write-Host "Step 1: Download encrypt segments"
        # Download each segment to cache folder 
        # Replaced by m3u8dl with --noMerge param [2024/11/05]
        #Download -strUrl $($oItem.strFileUrl) -strFile "${Script:WORK_ROOT}/cache/enc/$($oItem.strFile)" -bUseTool $true
        
        # Step 2: Decrypt segments
        Write-Host "Step 2: Decrypt segments"
        $strFileNameEnc = "00000$($arrListUrl[$nIndex].strFile)"
        $strFileNameEnc = $strFileNameEnc.Substring($strFileNameEnc.Length - (3 + $nCodecLength))
        Write-Host "Search file: $strFileNameEnc"
        $strFileEnc = $(ls ${Script:WORK_ROOT}/cache/Part_0/*.ts | ?{ $_.FullName -like "*0*$strFileNameEnc" } | %{ $_.FullName})
        Write-Host "Process file: $strFileEnc"
        node cctv_dec/ts_decrypt.js "$strFileEnc" "${Script:WORK_ROOT}/cache/dec/$($arrListUrl[$nIndex].strFile)"

        if ($nIndex -ge 6 -and ${Script:PREVIEWING} -eq $false) {
            ${Script:PREVIEWING} = $true
            Preview -strUrl "${Script:WORK_ROOT}/cache/dec/list.m3u8" -strTitle "$strTitle"
        }

        #pause
    }
    #Write-Host "Press any key till downloading segments mission complete successfully."
    #pause

    # Step 3: Merge segments
    Write-Host "Step 3: Merge segments"
    $strCMD = "ffmpeg -hide_banner -i `"${Script:WORK_ROOT}/cache/dec/list.m3u8`" -c copy -y `"${strTitle}.mp4`""
    Start cmd -NoNewWindow -Args "/c $strCMD" -Wait
    #rm ${Script:WORK_ROOT}/* -Recurse -Force
}

function Preview() {
    Param ( $strUrl, $strTitle )

    Write-Host "Current action:"
    Write-Host "`tPlaying: ${strTitle}"
    $strCMD = "ffplay -hide_banner -i `"$strUrl`" -window_title `"XXTV_Preview::${strTitle}`" -autoexit"
    Start cmd -NoNewWindow -args "/c $strCMD" -RedirectStandardError .\Nul
}

function main() {
    Param ( $Entry )
    pushd $Entry

    $strNewsUrl = Read-Host "请输入视频观看网址"
    $oPage = Invoke-WebRequest "$strNewsUrl"
    $strVID = GetVID $oPage
    Write-Host "当前视频信息:"
    Write-Host "`t$strVID"

    if ("" -ne "$strVID") {
        $strAPIUrl = "https://FQDN/api/getHttpVideoInfo.do?pid=${strVID}"
        #Write-Host "API Url: $strAPIUrl"

        $oRet = Invoke-RestMethod "$strAPIUrl"
        Write-Host $($oRet | ConvertTo-Json)
        $strPreviewUrl = "$($oRet.hls_url)"
        $strTitle = "$($oRet.title)"
        Write-Host "Preview URL: $strTrueUrl"
        #Preview "$strPreviewUrl" "$strTitle"

        $strH5eUrl = $oRet.manifest.hls_h5e_url
        $strH5eUrl = $strH5eUrl -replace "main", "2000"
        Write-Host "h5e_url : $strH5eUrl"

        DownloadAndDec -strUrl "$strH5eUrl" -strTitle "$strTitle"
    } else {
        Write-Host "No valiable info found in page. Contact the author of script."
    }
}

$PREVIEWING = $false
$WORK_ROOT = "${env:localappdata}/xxtv_downloader"
main -Entry $MainEntry
pause

execution_preview

References

  • https://www.52pojie.cn/thread-1875225-1-1.html
  • https://www.52pojie.cn/thread-1957747-1-1.html
  • https://github.com/nilaoda/N_m3u8DL-CLI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值