Skip to content

Commit

Permalink
fix #604
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Nov 8, 2023
1 parent 994b6c0 commit 09f1f6b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 34 deletions.
1 change: 0 additions & 1 deletion Sources/KSPlayer/AVPlayer/KSOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ open class KSOptions {
public var audioLocale: Locale?
// sutile
public var autoSelectEmbedSubtitle = true
public var subtitleDisable = false
public var isSeekImageSubtitle = false
// video
public var display = DisplayEnum.plane
Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/AVPlayer/KSVideoPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ extension KSVideoPlayer.Coordinator: KSPlayerLayerDelegate {
guard let self else { return }
self.subtitleModel.addSubtitle(dataSouce: subtitleDataSouce)
if self.subtitleModel.selectedSubtitleInfo == nil, layer.options.autoSelectEmbedSubtitle {
self.subtitleModel.selectedSubtitleInfo = subtitleDataSouce.infos.first
self.subtitleModel.selectedSubtitleInfo = subtitleDataSouce.infos.first { $0.isEnabled }
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions Sources/KSPlayer/MEPlayer/EmbedDataSouce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import Libavcodec
import Libavutil

extension FFmpegAssetTrack: SubtitleInfo {
public func subtitle(isEnabled: Bool) {
if isImageSubtitle {
self.isEnabled = isEnabled
}
}

public var subtitleID: String {
String(trackID)
}
Expand Down
16 changes: 10 additions & 6 deletions Sources/KSPlayer/MEPlayer/FFmpegAssetTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ public class FFmpegAssetTrack: MediaPlayerTrack {
if codecpar.codec_type == AVMEDIA_TYPE_VIDEO {
description += ", \(nominalFrameRate) fps"
}
if let value = metadata["language"] {
language = Locale.current.localizedString(forLanguageCode: value)
} else {
language = nil
}
language = metadata["language"]
if let value = metadata["title"] {
name = value
} else {
name = language ?? mediaType.rawValue
}
description = name + ", " + description
// AV_DISPOSITION_DEFAULT
if mediaType == .subtitle {
isEnabled = !isImageSubtitle || stream.pointee.disposition & AV_DISPOSITION_FORCED == AV_DISPOSITION_FORCED
}
// var buf = [Int8](repeating: 0, count: 256)
// avcodec_string(&buf, buf.count, codecpar, 0)
}
Expand Down Expand Up @@ -226,7 +226,11 @@ public class FFmpegAssetTrack: MediaPlayerTrack {
stream?.pointee.discard == AVDISCARD_DEFAULT
}
set {
stream?.pointee.discard = newValue ? AVDISCARD_DEFAULT : AVDISCARD_ALL
var discard = newValue ? AVDISCARD_DEFAULT : AVDISCARD_ALL
if mediaType == .subtitle, !isImageSubtitle {
discard = AVDISCARD_ALL
}
stream?.pointee.discard = discard
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Sources/KSPlayer/MEPlayer/FFmpegDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class FFmpegDecode: DecodeProtocol {
assetTrack.startTime = packet.assetTrack.startTime
assetTrack.timebase = packet.assetTrack.timebase
let subtitle = SyncPlayerItemTrack<SubtitleFrame>(assetTrack: assetTrack, options: options)
assetTrack.isEnabled = !assetTrack.isImageSubtitle
assetTrack.subtitle = subtitle
packet.assetTrack.closedCaptionsTrack = assetTrack
subtitle.decode()
Expand Down
6 changes: 1 addition & 5 deletions Sources/KSPlayer/MEPlayer/MEPlayerItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,8 @@ extension MEPlayerItem {
coreStream.pointee.discard = AVDISCARD_ALL
if let assetTrack = FFmpegAssetTrack(stream: coreStream) {
assetTrack.startTime = startTime
if !options.subtitleDisable, assetTrack.mediaType == .subtitle {
if assetTrack.mediaType == .subtitle {
let subtitle = SyncPlayerItemTrack<SubtitleFrame>(assetTrack: assetTrack, options: options)
assetTrack.isEnabled = !assetTrack.isImageSubtitle
assetTrack.subtitle = subtitle
allPlayerItemTracks.append(subtitle)
}
Expand All @@ -339,9 +338,6 @@ extension MEPlayerItem {
}
return nil
}
if options.autoSelectEmbedSubtitle {
assetTracks.first { $0.mediaType == .subtitle }?.isEnabled = true
}
var videoIndex: Int32 = -1
if !options.videoDisable {
let videos = assetTracks.filter { $0.mediaType == .video }
Expand Down
6 changes: 3 additions & 3 deletions Sources/KSPlayer/Subtitle/KSSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public protocol SubtitleInfo: KSSubtitleProtocol, AnyObject, Hashable, Identifia
// var userInfo: NSMutableDictionary? { get set }
// var subtitleDataSouce: SubtitleDataSouce? { get set }
// var comment: String? { get }
func subtitle(isEnabled: Bool)
var isEnabled: Bool { get set }
}

public extension SubtitleInfo {
Expand Down Expand Up @@ -299,8 +299,8 @@ open class SubtitleModel: ObservableObject {
@Published
public var selectedSubtitleInfo: (any SubtitleInfo)? {
didSet {
oldValue?.subtitle(isEnabled: false)
selectedSubtitleInfo?.subtitle(isEnabled: true)
oldValue?.isEnabled = false
selectedSubtitleInfo?.isEnabled = true
}
}

Expand Down
21 changes: 11 additions & 10 deletions Sources/KSPlayer/Subtitle/SubtitleDataSouce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@
//
import Foundation
public class EmptySubtitleInfo: SubtitleInfo {
public var isEnabled: Bool = true
public let subtitleID: String = ""
public var delay: TimeInterval = 0
public let name = NSLocalizedString("no show subtitle", comment: "")
public func search(for _: TimeInterval) -> [SubtitlePart] {
[]
}

public func subtitle(isEnabled _: Bool) {}
}

public class URLSubtitleInfo: KSSubtitle, SubtitleInfo {
public var isEnabled: Bool = false {
didSet {
if isEnabled, parts.isEmpty {
Task {
try? await parse(url: downloadURL, userAgent: userAgent)
}
}
}
}

private var downloadURL: URL
public var delay: TimeInterval = 0
public private(set) var name: String
Expand Down Expand Up @@ -48,14 +57,6 @@ public class URLSubtitleInfo: KSSubtitle, SubtitleInfo {
}
}
}

public func subtitle(isEnabled: Bool) {
if isEnabled, parts.isEmpty {
Task {
try? await parse(url: downloadURL, userAgent: userAgent)
}
}
}
}

public protocol SubtitleDataSouce: AnyObject {
Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/Video/VideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ open class VideoPlayerView: PlayerView {
guard let self else { return }
self.srtControl.addSubtitle(dataSouce: subtitleDataSouce)
if self.srtControl.selectedSubtitleInfo == nil, layer.options.autoSelectEmbedSubtitle {
self.srtControl.selectedSubtitleInfo = self.srtControl.subtitleInfos.first
self.srtControl.selectedSubtitleInfo = self.srtControl.subtitleInfos.first { $0.isEnabled }
}
self.toolBar.srtButton.isHidden = self.srtControl.subtitleInfos.isEmpty
if #available(iOS 14.0, tvOS 15.0, *) {
Expand Down

0 comments on commit 09f1f6b

Please sign in to comment.