Skip to content

Commit

Permalink
add Chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Dec 14, 2023
1 parent f69518d commit 95e6e61
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion FFmpegKit
1 change: 1 addition & 0 deletions Sources/KSPlayer/AVPlayer/KSAVPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public class KSAVPlayer {
public private(set) var duration: TimeInterval = 0
public private(set) var fileSize: Double = 0
public private(set) var playableTime: TimeInterval = 0
public let chapters: [Chapter] = []
public var playbackRate: Float = 1 {
didSet {
if playbackState == .playing {
Expand Down
1 change: 1 addition & 0 deletions Sources/KSPlayer/AVPlayer/KSOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ public extension KSOptions {
/// seek完是否自动播放
static var isSeekedAutoPlay = true
static var hardwareDecode = true
// 默认不用自研的硬解,因为有些视频的AVPacket的pts顺序是不对的,只有解码后的AVFrame里面的pts是对的。
static var asynchronousDecompression = false
static var isPipPopViewController = false
static var displayCriteriaFormatDescriptionEnabled = false
Expand Down
7 changes: 7 additions & 0 deletions Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public protocol MediaPlayback: AnyObject {
var duration: TimeInterval { get }
var fileSize: Double { get }
var naturalSize: CGSize { get }
var chapters: [Chapter] { get }
var currentPlaybackTime: TimeInterval { get }
func prepareToPlay()
func shutdown()
Expand Down Expand Up @@ -57,6 +58,12 @@ public class DynamicInfo: ObservableObject {
}
}

public struct Chapter {
public let start: TimeInterval
public let end: TimeInterval
public let title: String
}

public protocol MediaPlayerProtocol: MediaPlayback {
var delegate: MediaPlayerDelegate? { get set }
var view: UIView? { get }
Expand Down
4 changes: 4 additions & 0 deletions Sources/KSPlayer/MEPlayer/KSMEPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ extension KSMEPlayer: MEPlayerDelegate {
}

extension KSMEPlayer: MediaPlayerProtocol {
public var chapters: [Chapter] {
playerItem.chapters
}

public var subtitleDataSouce: SubtitleDataSouce? { self }
public var playbackVolume: Float {
get {
Expand Down
16 changes: 16 additions & 0 deletions Sources/KSPlayer/MEPlayer/MEPlayerItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ final class MEPlayerItem {
private var videoDisplayCount = UInt8(0)
private var seekByBytes = false
private var lastVideoDisplayTime = CACurrentMediaTime()
public private(set) var chapters: [Chapter] = []
var currentPlaybackTime: TimeInterval {
state == .seeking ? seekTime : (mainClock().time - startTime).seconds
}
Expand Down Expand Up @@ -81,6 +82,7 @@ final class MEPlayerItem {
}

lazy var dynamicInfo = DynamicInfo { [weak self] in
// metadata可能会实时变化。所以把它放在DynamicInfo里面
toDictionary(self?.formatCtx?.pointee.metadata)
} bytesRead: { [weak self] in
self?.formatCtx?.pointee.pb?.pointee.bytes_read ?? 0
Expand Down Expand Up @@ -258,6 +260,20 @@ extension MEPlayerItem {
duration = TimeInterval(max(formatCtx.pointee.duration, 0) / Int64(AV_TIME_BASE))
fileSize = Double(formatCtx.pointee.bit_rate) * duration / 8
createCodec(formatCtx: formatCtx)
if formatCtx.pointee.nb_chapters > 0 {
chapters.removeAll()
for i in 0 ..< formatCtx.pointee.nb_chapters {
if let chapter = formatCtx.pointee.chapters[Int(i)]?.pointee {
let timeBase = Timebase(chapter.time_base)
let start = timeBase.cmtime(for: chapter.start).seconds
let end = timeBase.cmtime(for: chapter.end).seconds
let metadata = toDictionary(chapter.metadata)
let title = metadata["title"] ?? ""
chapters.append(Chapter(start: start, end: end, title: title))
}
}
}

if let outputURL = options.outputURL {
openOutput(url: outputURL)
}
Expand Down

0 comments on commit 95e6e61

Please sign in to comment.