kingslay / KSPlayer

A video player for iOS、macOS、tvOS、visionOS , based on AVPlayer and FFmpeg, support the horizontal, vertical screen. support adjust volume, brightness and seek by slide, SwiftUI, support subtitles.
https://apps.apple.com/app/tracyplayer/id6450770064
GNU General Public License v3.0
1.01k stars 199 forks source link

video error: "Error splitting the input into NAL units." #30

Closed wanwe closed 2 years ago

wanwe commented 4 years ago

Hi Kingslay,

After upgrading to latest develop, I'm getting below error very often when playing bilibili videos:

KSPlayer: MEPlayerItem.swift:88 init(url:options:) | Invalid NAL unit size (2389 > 1387). KSPlayer: MEPlayerItem.swift:88 init(url:options:) | Error splitting the input into NAL units. KSPlayerTime 211.91648526077097 of 4079.0 KSPlayerTime 4079.0 of 4079.0

when this error happens, video will stop playing. this issue was not there back in Apr, when I first tried the code. do you have any idea what could be the root cause and how to solve it? any info would be helpful, thanks !

sample video: https://www.bilibili.com/video/BV1Wb411S7eE?p=6

kingslay commented 4 years ago

i use this mpd. produce an error: Server returned 403 Forbidden (access denied)

wanwe commented 4 years ago

the URL will expire after some time due to bilibili limit, can you try below script to create a test server locally? save the code as bilimdp.py and run it with "python3 bilimpd.py", test url will be: "http://127.0.0.1/test"

kingslay commented 4 years ago

This page contains the following errors: error on line 6 at column 333: EntityRef: expecting ';' Below is a rendering of the page up to the first error.

wanwe commented 4 years ago

so sorry, fixed the code earlier missed the 2nd replace in ".replace('http:', 'https:').replace('&', '&')" when I convert this code from js to python

kingslay commented 4 years ago

Unable to parse '/Users/kingslay/Library/Developer/Xcode/DerivedData/Demo-atfnkhqcmxsbaxhiuyrjxwhemsfx/Build/Products/Debug-maccatalyst/demo-iOS.app/Contents/Resources/1.mpd' - missing root node

wanwe commented 4 years ago

Oh..you are saving the mpd as local file? I didn’t test local file, I was playing URL http://127.0.0.1/test directly, and it works fine.

Thanks for your time! let me try save as local file as well

kingslay commented 4 years ago

但我直接用 http://127.0.0.1/test 播放报错 KSPlayer: MEPlayerItem.swift:88 init(url:options:) | HTTP error 403 Forbidden

KSPlayer: MEPlayerItem.swift:88 init(url:options:) | Failed to open fragment of playlist 0

wanwe commented 4 years ago

但我直接用 http://127.0.0.1/test 播放报错

KSPlayer: MEPlayerItem.swift:88 init(url:options:) | HTTP error 403 Forbidden

KSPlayer: MEPlayerItem.swift:88 init(url:options:) | Failed to open fragment of playlist 0

有点奇怪。。我测试了存成本地文件和直接用url都没有错误

我先把这个问题关掉了,让我再研究下,多谢!

kingslay commented 4 years ago

@wanwe 你可以试着把videoAdaptable的默认值改成是false。看下是不是因为多码率切换导致的问题。

wanwe commented 4 years ago

@kingslay 多谢!

wanwe commented 4 years ago

不好意思,重开一下这个issue 我发现:

  1. 用hardware decoder的时候会报错误-8969(simulator)不知道是不是这个原因,看不明白。。哈哈
  2. 不用hardwae decoder的时候报上面的NAL错误,差不多同一个时间点上,看起来上相关的?
  3. videoAdaptable=false也不行

我试着用以前的版本试了一下,有同样的问题(同一个视频)。可能以前的时候看的别的视频都比较短,这种错误出现的几率低?

KSPlayerTime 1158.4547165532879 of 4079.0
KSPlayer: MEPlayerItemTrack.swift:312 doDecode(packet:) | Decoder did Failed : Error Domain=KSPlayerErrorDomain Code=11 "avcodec can't receive video frame" UserInfo={NSLocalizedDescription=avcodec can't receive video frame, NSUnderlyingError=0x60000187f0f0 {Error Domain=FFmpegDomain Code=-8969 "Error number -8969 occurred" UserInfo={NSLocalizedDescription=Error number -8969 occurred}}}
KSPlayer: MEPlayerItem.swift:87 init(url:options:) | detected 4 logical cores

KSPlayer: MEPlayerItem.swift:87 init(url:options:) | nal_unit_type: 7(SPS), nal_ref_idc: 3

KSPlayer: MEPlayerItem.swift:87 init(url:options:) | nal_unit_type: 8(PPS), nal_ref_idc: 3

KSPlayer: MEPlayerItemTrack.swift:315 doDecode(packet:) | VideoCodec switch to software decompression
KSPlayerTime 1158.9539455782312 of 4079.0
KSPlayerTime 4079.0 of 4079.0
KSPlayer: KSPlayerLayer.swift:100 state | playerStateDidChange - playedToTheEnd
KSPlayerState playedToTheEnd
KSPlayer true
KSPlayer error: nil

你可以再试一下这个code吗? 会自动生成bilibili_test.mpd文件,我的测试的时候, 播放没问题。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 24 09:07:45 2020

@author: meow
"""
from flask import Flask, request, send_from_directory
from flask import render_template
from flask import request, jsonify, Response

import requests
import json
from lxml import etree
import os

requests.packages.urllib3.disable_warnings()

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3970.5 Safari/537.36',
    'Referer': 'https://www.bilibili.com/'
}

app = Flask(__name__, static_url_path='')
app.config['DEBUG'] = False
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

def GetBiliVideo(homeurl,num,session=requests.session()):
    print(homeurl)
    #print("=======videoinforms")
    res = session.get(url=homeurl, headers=headers, verify=False)
    html = etree.HTML(res.content)
    #print(res.content)
    videoinforms = res.content #
    videoinforms = str(html.xpath('//head/script[3]/text()')[0])[20:]
    print(videoinforms)
    videojson = json.loads(videoinforms)    
    return GenMPD(videojson)

def GenMPD(resp):      
    dash = resp['data']['dash']
    video = dash['video'][0]
    audio = dash['audio'][0]
    mpd_template=f"""<MPD profiles="urn:hbbtv:dash:profile:isoff-live:2012,urn:mpeg:dash:profile:isoff-live:2011" type="static" xmlns="urn:mpeg:dash:schema:mpd:2011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mpeg:DASH:schema:MPD:2011 DASH-MPD.xsd" minBufferTime="PT{dash['minBufferTime']}S" mediaPresentationDuration="PT{resp['data']['timelength']/1000}S">
     <BaseURL>./</BaseURL>
     <Period>
      <AdaptationSet mimeType="video/mp4" contentType="video" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
       <SegmentTemplate duration="{dash['duration']}" media="{video['baseUrl'].replace('http:', 'https:').replace('&', '&amp;')}" startNumber="1"/>
       <Representation id="bilibili_{video['id']}" codecs="{video['codecs']}" bandwidth="{video['bandwidth']}" width="{video['width']}" height="{video['height']}" frameRate="{video['frameRate']}" sar="1:1"/>
      </AdaptationSet>
      <AdaptationSet mimeType="audio/mp4" contentType="audio" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
       <Accessibility schemeIdUri="urn:tva:metadata:cs:AudioPurposeCS:2007" value="6"/>
       <Role schemeIdUri="urn:mpeg:dash:role:2011" value="main"/>
       <SegmentTemplate duration="{dash['duration']}" media="{audio['baseUrl'].replace('http:', 'https:').replace('&', '&amp;')}" startNumber="1"/>
       <Representation id="bbb_a64k" codecs="{audio['codecs']}" bandwidth="{audio['bandwidth']}">
        <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
       </Representation>
      </AdaptationSet>
     </Period>
    </MPD>
    """
    return mpd_template

def test():
    av="43149523"
    p=6
    url=f'https://www.bilibili.com/video/av{av}'
    i=p
    mpd = GetBiliVideo(url+'?p='+str(i),i)
    #mpd = GetBiliVideo(url,i)
    with open("bilibili_test.mpd", mode="w") as fp:
      fp.write(mpd)
      fp.flush()
    print("saved to bilibili_test.mpd")
    return mpd

if __name__ == "__main__":
    test()

我的测试

        if let url = URL(string: "/Users/wei/projects/bilibili_test.mpd") {
            objects.append(KSPlayerResource(url: url, options: KSOptions(), name: "dash视频"))
        }

大部分时候问题在4:00~6:00或者18:00~21:00之间,不知道为什么不是每次都有。。。

kingslay commented 4 years ago

你是用iPhone模拟器吗?我这边播放报错"Invalid data found when processing input"。我用其他的播放器例如 vlc也是无法播放这个mpd。你有改ffmpeg吗?

wanwe commented 4 years ago

Iphone/Apple tv simulator 都试了,都没问题 ffmpeg是ksplayer自带的,我没动过

好奇怪啊,你可以把生成的mpd内容贴上来吗?看看和我的有啥不一样

kingslay commented 4 years ago
<MPD profiles="urn:hbbtv:dash:profile:isoff-live:2012,urn:mpeg:dash:profile:isoff-live:2011" type="static" xmlns="urn:mpeg:dash:schema:mpd:2011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mpeg:DASH:schema:MPD:2011 DASH-MPD.xsd" minBufferTime="PT1.5S" mediaPresentationDuration="PT4079.815S">
     <BaseURL>./</BaseURL>
     <Period>
      <AdaptationSet mimeType="video/mp4" contentType="video" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
       <SegmentTemplate duration="4080" media="https://cn-zjhz3-wasu-bcache-01.bilivideo.com/upgcxcode/26/11/75641126/75641126-1-30064.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&amp;uipk=5&amp;nbs=1&amp;deadline=1593930405&amp;gen=playurl&amp;os=bcache&amp;oi=3664551470&amp;trid=ab7ab543945740e6a8d5f2751a748dc9u&amp;platform=pc&amp;upsig=a1112ca227aebab51f7f67a2150cd0fd&amp;uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&amp;cdnid=20008&amp;mid=0&amp;orderid=0,3&amp;agrr=0&amp;logo=80000000" startNumber="1"/>
       <Representation id="bilibili_64" codecs="avc1.64001F" bandwidth="751460" width="1280" height="720" frameRate="25" sar="1:1"/>
      </AdaptationSet>
      <AdaptationSet mimeType="audio/mp4" contentType="audio" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
       <Accessibility schemeIdUri="urn:tva:metadata:cs:AudioPurposeCS:2007" value="6"/>
       <Role schemeIdUri="urn:mpeg:dash:role:2011" value="main"/>
       <SegmentTemplate duration="4080" media="https://upos-sz-mirrorhw.bilivideo.com/upgcxcode/26/11/75641126/75641126-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&amp;uipk=5&amp;nbs=1&amp;deadline=1593930405&amp;gen=playurl&amp;os=hwbv&amp;oi=3664551470&amp;trid=ab7ab543945740e6a8d5f2751a748dc9u&amp;platform=pc&amp;upsig=10414594be8e99e57058e5ad29ce44ad&amp;uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&amp;mid=0&amp;orderid=0,3&amp;agrr=0&amp;logo=80000000" startNumber="1"/>
       <Representation id="bbb_a64k" codecs="mp4a.40.2" bandwidth="127138">
        <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
       </Representation>
      </AdaptationSet>
     </Period>
    </MPD>
wanwe commented 4 years ago

神了。。我用你的iphone/appletv可以打开这个mpd,但是直接用ffplay播放就会有这个错误"Invalid data found when processing input“。。不知道什么魔法 我用ffplay吓测了一下,貌似和网络有关,这也解释了为啥同一个视频有时候有问题有时候没问题。。就是不知道为什么在浏览器里播放或者vlc播放的时候为什么一直没问题,难道可以在ffmpeg外面做什么错误处理或者自动跳过这一帧啥的? Screenshot 2020-07-05 at 8 28 27 PM