Add detection of container/video_codec/audio_codec compatibility for live file streaming or transcoding (#384)

* add forceMKV, forceHEVC config options
* drop audio stream instead of trying to transcode for ffmpeg unsupported/unknown audio codecs
This commit is contained in:
bnkai
2020-04-10 01:38:34 +03:00
committed by GitHub
parent dc37a3045b
commit d5617307f1
21 changed files with 632 additions and 60 deletions

View File

@@ -4,6 +4,8 @@ fragment ConfigGeneralData on ConfigGeneralResult {
generatedPath generatedPath
maxTranscodeSize maxTranscodeSize
maxStreamingTranscodeSize maxStreamingTranscodeSize
forceMkv
forceHevc
username username
password password
maxSessionAge maxSessionAge

View File

@@ -18,6 +18,10 @@ input ConfigGeneralInput {
maxTranscodeSize: StreamingResolutionEnum maxTranscodeSize: StreamingResolutionEnum
"""Max streaming transcode size""" """Max streaming transcode size"""
maxStreamingTranscodeSize: StreamingResolutionEnum maxStreamingTranscodeSize: StreamingResolutionEnum
"""Force MKV as supported format"""
forceMkv: Boolean!
"""Force HEVC as a supported codec"""
forceHevc: Boolean!
"""Username""" """Username"""
username: String username: String
"""Password""" """Password"""
@@ -49,6 +53,10 @@ type ConfigGeneralResult {
maxTranscodeSize: StreamingResolutionEnum maxTranscodeSize: StreamingResolutionEnum
"""Max streaming transcode size""" """Max streaming transcode size"""
maxStreamingTranscodeSize: StreamingResolutionEnum maxStreamingTranscodeSize: StreamingResolutionEnum
"""Force MKV as supported format"""
forceMkv: Boolean!
"""Force HEVC as a supported codec"""
forceHevc: Boolean!
"""Username""" """Username"""
username: String! username: String!
"""Password""" """Password"""

View File

@@ -45,6 +45,8 @@ func (r *mutationResolver) ConfigureGeneral(ctx context.Context, input models.Co
if input.MaxStreamingTranscodeSize != nil { if input.MaxStreamingTranscodeSize != nil {
config.Set(config.MaxStreamingTranscodeSize, input.MaxStreamingTranscodeSize.String()) config.Set(config.MaxStreamingTranscodeSize, input.MaxStreamingTranscodeSize.String())
} }
config.Set(config.ForceMKV, input.ForceMkv)
config.Set(config.ForceHEVC, input.ForceHevc)
if input.Username != nil { if input.Username != nil {
config.Set(config.Username, input.Username) config.Set(config.Username, input.Username)

View File

@@ -41,6 +41,8 @@ func makeConfigGeneralResult() *models.ConfigGeneralResult {
GeneratedPath: config.GetGeneratedPath(), GeneratedPath: config.GetGeneratedPath(),
MaxTranscodeSize: &maxTranscodeSize, MaxTranscodeSize: &maxTranscodeSize,
MaxStreamingTranscodeSize: &maxStreamingTranscodeSize, MaxStreamingTranscodeSize: &maxStreamingTranscodeSize,
ForceMkv: config.GetForceMKV(),
ForceHevc: config.GetForceHEVC(),
Username: config.GetUsername(), Username: config.GetUsername(),
Password: config.GetPasswordHash(), Password: config.GetPasswordHash(),
MaxSessionAge: config.GetMaxSessionAge(), MaxSessionAge: config.GetMaxSessionAge(),

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"io" "io"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
@@ -42,13 +43,32 @@ func (rs sceneRoutes) Routes() chi.Router {
// region Handlers // region Handlers
func (rs sceneRoutes) Stream(w http.ResponseWriter, r *http.Request) { func (rs sceneRoutes) Stream(w http.ResponseWriter, r *http.Request) {
scene := r.Context().Value(sceneKey).(*models.Scene) scene := r.Context().Value(sceneKey).(*models.Scene)
container := ""
if scene.Format.Valid {
container = scene.Format.String
} else { // container isn't in the DB
// shouldn't happen, fallback to ffprobe
tmpVideoFile, err := ffmpeg.NewVideoFile(manager.GetInstance().FFProbePath, scene.Path)
if err != nil {
logger.Errorf("[transcode] error reading video file: %s", err.Error())
return
}
container = string(ffmpeg.MatchContainer(tmpVideoFile.Container, scene.Path))
}
// detect if not a streamable file and try to transcode it instead // detect if not a streamable file and try to transcode it instead
filepath := manager.GetInstance().Paths.Scene.GetStreamPath(scene.Path, scene.Checksum) filepath := manager.GetInstance().Paths.Scene.GetStreamPath(scene.Path, scene.Checksum)
videoCodec := scene.VideoCodec.String videoCodec := scene.VideoCodec.String
audioCodec := ffmpeg.MissingUnsupported
if scene.AudioCodec.Valid {
audioCodec = ffmpeg.AudioCodec(scene.AudioCodec.String)
}
hasTranscode, _ := manager.HasTranscode(scene) hasTranscode, _ := manager.HasTranscode(scene)
if ffmpeg.IsValidCodec(videoCodec) || hasTranscode { if ffmpeg.IsValidCodec(videoCodec) && ffmpeg.IsValidCombo(videoCodec, ffmpeg.Container(container)) && ffmpeg.IsValidAudioForContainer(audioCodec, ffmpeg.Container(container)) || hasTranscode {
manager.RegisterStream(filepath, &w) manager.RegisterStream(filepath, &w)
http.ServeFile(w, r, filepath) http.ServeFile(w, r, filepath)
manager.WaitAndDeregisterStream(filepath, &w, r) manager.WaitAndDeregisterStream(filepath, &w, r)
@@ -69,16 +89,50 @@ func (rs sceneRoutes) Stream(w http.ResponseWriter, r *http.Request) {
encoder := ffmpeg.NewEncoder(manager.GetInstance().FFMPEGPath) encoder := ffmpeg.NewEncoder(manager.GetInstance().FFMPEGPath)
stream, process, err := encoder.StreamTranscode(*videoFile, startTime, config.GetMaxStreamingTranscodeSize()) var stream io.ReadCloser
var process *os.Process
mimeType := ffmpeg.MimeWebm
if audioCodec == ffmpeg.MissingUnsupported {
//ffmpeg fails if it trys to transcode a non supported audio codec
stream, process, err = encoder.StreamTranscodeVideo(*videoFile, startTime, config.GetMaxStreamingTranscodeSize())
} else {
copyVideo := false // try to be smart if the video to be transcoded is in a Matroska container
// mp4 has always supported audio so it doesn't need to be checked
// while mpeg_ts has seeking issues if we don't reencode the video
if config.GetForceMKV() { // If MKV is forced as supported and video codec is also supported then only transcode audio
if ffmpeg.Container(container) == ffmpeg.Matroska {
switch videoCodec {
case ffmpeg.H264, ffmpeg.Vp9, ffmpeg.Vp8:
copyVideo = true
case ffmpeg.Hevc:
if config.GetForceHEVC() {
copyVideo = true
}
}
}
}
if copyVideo { // copy video stream instead of transcoding it
stream, process, err = encoder.StreamMkvTranscodeAudio(*videoFile, startTime, config.GetMaxStreamingTranscodeSize())
mimeType = ffmpeg.MimeMkv
} else {
stream, process, err = encoder.StreamTranscode(*videoFile, startTime, config.GetMaxStreamingTranscodeSize())
}
}
if err != nil { if err != nil {
logger.Errorf("[stream] error transcoding video file: %s", err.Error()) logger.Errorf("[stream] error transcoding video file: %s", err.Error())
return return
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "video/webm") w.Header().Set("Content-Type", mimeType)
logger.Info("[stream] transcoding video file") logger.Infof("[stream] transcoding video file to %s", mimeType)
// handle if client closes the connection // handle if client closes the connection
notify := r.Context().Done() notify := r.Context().Done()

View File

@@ -19,7 +19,7 @@ import (
var DB *sqlx.DB var DB *sqlx.DB
var dbPath string var dbPath string
var appSchemaVersion uint = 5 var appSchemaVersion uint = 6
var databaseSchemaVersion uint var databaseSchemaVersion uint
const sqlite3Driver = "sqlite3_regexp" const sqlite3Driver = "sqlite3_regexp"

View File

@@ -0,0 +1 @@
ALTER TABLE `scenes` ADD COLUMN `format` varchar(255);

View File

@@ -69,6 +69,49 @@ func (e *Encoder) Transcode(probeResult VideoFile, options TranscodeOptions) {
_, _ = e.run(probeResult, args) _, _ = e.run(probeResult, args)
} }
//transcode the video, remove the audio
//in some videos where the audio codec is not supported by ffmpeg
//ffmpeg fails if you try to transcode the audio
func (e *Encoder) TranscodeVideo(probeResult VideoFile, options TranscodeOptions) {
scale := calculateTranscodeScale(probeResult, options.MaxTranscodeSize)
args := []string{
"-i", probeResult.Path,
"-an",
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-profile:v", "high",
"-level", "4.2",
"-preset", "superfast",
"-crf", "23",
"-vf", "scale=" + scale,
options.OutputPath,
}
_, _ = e.run(probeResult, args)
}
//copy the video stream as is, transcode audio
func (e *Encoder) TranscodeAudio(probeResult VideoFile, options TranscodeOptions) {
args := []string{
"-i", probeResult.Path,
"-c:v", "copy",
"-c:a", "aac",
"-strict", "-2",
options.OutputPath,
}
_, _ = e.run(probeResult, args)
}
//copy the video stream as is, drop audio
func (e *Encoder) CopyVideo(probeResult VideoFile, options TranscodeOptions) {
args := []string{
"-i", probeResult.Path,
"-an",
"-c:v", "copy",
options.OutputPath,
}
_, _ = e.run(probeResult, args)
}
func (e *Encoder) StreamTranscode(probeResult VideoFile, startTime string, maxTranscodeSize models.StreamingResolutionEnum) (io.ReadCloser, *os.Process, error) { func (e *Encoder) StreamTranscode(probeResult VideoFile, startTime string, maxTranscodeSize models.StreamingResolutionEnum) (io.ReadCloser, *os.Process, error) {
scale := calculateTranscodeScale(probeResult, maxTranscodeSize) scale := calculateTranscodeScale(probeResult, maxTranscodeSize)
args := []string{} args := []string{}
@@ -92,3 +135,53 @@ func (e *Encoder) StreamTranscode(probeResult VideoFile, startTime string, maxTr
return e.stream(probeResult, args) return e.stream(probeResult, args)
} }
//transcode the video, remove the audio
//in some videos where the audio codec is not supported by ffmpeg
//ffmpeg fails if you try to transcode the audio
func (e *Encoder) StreamTranscodeVideo(probeResult VideoFile, startTime string, maxTranscodeSize models.StreamingResolutionEnum) (io.ReadCloser, *os.Process, error) {
scale := calculateTranscodeScale(probeResult, maxTranscodeSize)
args := []string{}
if startTime != "" {
args = append(args, "-ss", startTime)
}
args = append(args,
"-i", probeResult.Path,
"-an",
"-c:v", "libvpx-vp9",
"-vf", "scale="+scale,
"-deadline", "realtime",
"-cpu-used", "5",
"-row-mt", "1",
"-crf", "30",
"-b:v", "0",
"-f", "webm",
"pipe:",
)
return e.stream(probeResult, args)
}
//it is very common in MKVs to have just the audio codec unsupported
//copy the video stream, transcode the audio and serve as Matroska
func (e *Encoder) StreamMkvTranscodeAudio(probeResult VideoFile, startTime string, maxTranscodeSize models.StreamingResolutionEnum) (io.ReadCloser, *os.Process, error) {
args := []string{}
if startTime != "" {
args = append(args, "-ss", startTime)
}
args = append(args,
"-i", probeResult.Path,
"-c:v", "copy",
"-c:a", "libopus",
"-b:a", "96k",
"-vbr", "on",
"-f", "matroska",
"pipe:",
)
return e.stream(probeResult, args)
}

View File

@@ -10,11 +10,105 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/stashapp/stash/pkg/manager/config"
) )
var ValidCodecs = []string{"h264", "h265", "vp8", "vp9"} type Container string
type AudioCodec string
const (
Mp4 Container = "mp4"
M4v Container = "m4v"
Mov Container = "mov"
Wmv Container = "wmv"
Webm Container = "webm"
Matroska Container = "matroska"
Avi Container = "avi"
Flv Container = "flv"
Mpegts Container = "mpegts"
Aac AudioCodec = "aac"
Mp3 AudioCodec = "mp3"
Opus AudioCodec = "opus"
Vorbis AudioCodec = "vorbis"
MissingUnsupported AudioCodec = ""
Mp4Ffmpeg string = "mov,mp4,m4a,3gp,3g2,mj2" // browsers support all of them
M4vFfmpeg string = "mov,mp4,m4a,3gp,3g2,mj2" // so we don't care that ffmpeg
MovFfmpeg string = "mov,mp4,m4a,3gp,3g2,mj2" // can't differentiate between them
WmvFfmpeg string = "asf"
WebmFfmpeg string = "matroska,webm"
MatroskaFfmpeg string = "matroska,webm"
AviFfmpeg string = "avi"
FlvFfmpeg string = "flv"
MpegtsFfmpeg string = "mpegts"
H264 string = "h264"
H265 string = "h265" // found in rare cases from a faulty encoder
Hevc string = "hevc"
Vp8 string = "vp8"
Vp9 string = "vp9"
MimeWebm string = "video/webm"
MimeMkv string = "video/x-matroska"
)
var ValidCodecs = []string{H264, H265, Vp8, Vp9}
var validForH264Mkv = []Container{Mp4, Matroska}
var validForH264 = []Container{Mp4}
var validForH265Mkv = []Container{Mp4, Matroska}
var validForH265 = []Container{Mp4}
var validForVp8 = []Container{Webm}
var validForVp9Mkv = []Container{Webm, Matroska}
var validForVp9 = []Container{Webm}
var validForHevcMkv = []Container{Mp4, Matroska}
var validForHevc = []Container{Mp4}
var validAudioForMkv = []AudioCodec{Aac, Mp3, Vorbis, Opus}
var validAudioForWebm = []AudioCodec{Vorbis, Opus}
var validAudioForMp4 = []AudioCodec{Aac, Mp3}
//maps user readable container strings to ffprobe's format_name
//on some formats ffprobe can't differentiate
var ContainerToFfprobe = map[Container]string{
Mp4: Mp4Ffmpeg,
M4v: M4vFfmpeg,
Mov: MovFfmpeg,
Wmv: WmvFfmpeg,
Webm: WebmFfmpeg,
Matroska: MatroskaFfmpeg,
Avi: AviFfmpeg,
Flv: FlvFfmpeg,
Mpegts: MpegtsFfmpeg,
}
var FfprobeToContainer = map[string]Container{
Mp4Ffmpeg: Mp4,
WmvFfmpeg: Wmv,
AviFfmpeg: Avi,
FlvFfmpeg: Flv,
MpegtsFfmpeg: Mpegts,
MatroskaFfmpeg: Matroska,
}
func MatchContainer(format string, filePath string) Container { // match ffprobe string to our Container
container := FfprobeToContainer[format]
if container == Matroska {
container = MagicContainer(filePath) // use magic number instead of ffprobe for matroska,webm
}
if container == "" { // if format is not in our Container list leave it as ffprobes reported format_name
container = Container(format)
}
return container
}
func IsValidCodec(codecName string) bool { func IsValidCodec(codecName string) bool {
forceHEVC := config.GetForceHEVC()
if forceHEVC {
if codecName == Hevc {
return true
}
}
for _, c := range ValidCodecs { for _, c := range ValidCodecs {
if c == codecName { if c == codecName {
return true return true
@@ -23,6 +117,78 @@ func IsValidCodec(codecName string) bool {
return false return false
} }
func IsValidAudio(audio AudioCodec, ValidCodecs []AudioCodec) bool {
// if audio codec is missing or unsupported by ffmpeg we can't do anything about it
// report it as valid so that the file can at least be streamed directly if the video codec is supported
if audio == MissingUnsupported {
return true
}
for _, c := range ValidCodecs {
if c == audio {
return true
}
}
return false
}
func IsValidAudioForContainer(audio AudioCodec, format Container) bool {
switch format {
case Matroska:
return IsValidAudio(audio, validAudioForMkv)
case Webm:
return IsValidAudio(audio, validAudioForWebm)
case Mp4:
return IsValidAudio(audio, validAudioForMp4)
}
return false
}
func IsValidForContainer(format Container, validContainers []Container) bool {
for _, fmt := range validContainers {
if fmt == format {
return true
}
}
return false
}
//extend stream validation check to take into account container
func IsValidCombo(codecName string, format Container) bool {
forceMKV := config.GetForceMKV()
forceHEVC := config.GetForceHEVC()
switch codecName {
case H264:
if forceMKV {
return IsValidForContainer(format, validForH264Mkv)
}
return IsValidForContainer(format, validForH264)
case H265:
if forceMKV {
return IsValidForContainer(format, validForH265Mkv)
}
return IsValidForContainer(format, validForH265)
case Vp8:
return IsValidForContainer(format, validForVp8)
case Vp9:
if forceMKV {
return IsValidForContainer(format, validForVp9Mkv)
}
return IsValidForContainer(format, validForVp9)
case Hevc:
if forceHEVC {
if forceMKV {
return IsValidForContainer(format, validForHevcMkv)
}
return IsValidForContainer(format, validForHevc)
}
}
return false
}
type VideoFile struct { type VideoFile struct {
JSON FFProbeJSON JSON FFProbeJSON
AudioStream *FFProbeStream AudioStream *FFProbeStream

View File

@@ -0,0 +1,66 @@
package ffmpeg
import (
"bytes"
"github.com/stashapp/stash/pkg/logger"
"os"
)
// detect file format from magic file number
// https://github.com/lex-r/filetype/blob/73c10ad714e3b8ecf5cd1564c882ed6d440d5c2d/matchers/video.go
func mkv(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x1A && buf[1] == 0x45 &&
buf[2] == 0xDF && buf[3] == 0xA3 &&
containsMatroskaSignature(buf, []byte{'m', 'a', 't', 'r', 'o', 's', 'k', 'a'})
}
func webm(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x1A && buf[1] == 0x45 &&
buf[2] == 0xDF && buf[3] == 0xA3 &&
containsMatroskaSignature(buf, []byte{'w', 'e', 'b', 'm'})
}
func containsMatroskaSignature(buf, subType []byte) bool {
limit := 4096
if len(buf) < limit {
limit = len(buf)
}
index := bytes.Index(buf[:limit], subType)
if index < 3 {
return false
}
return buf[index-3] == 0x42 && buf[index-2] == 0x82
}
//returns container as string ("" on error or no match)
//implements only mkv or webm as ffprobe can't distinguish between them
//and not all browsers support mkv
func MagicContainer(file_path string) Container {
file, err := os.Open(file_path)
if err != nil {
logger.Errorf("[magicfile] %v", err)
return ""
}
defer file.Close()
buf := make([]byte, 4096)
_, err = file.Read(buf)
if err != nil {
logger.Errorf("[magicfile] %v", err)
return ""
}
if webm(buf) {
return Webm
}
if mkv(buf) {
return Matroska
}
return ""
}

View File

@@ -55,6 +55,10 @@ const AutostartVideo = "autostart_video"
const ShowStudioAsText = "show_studio_as_text" const ShowStudioAsText = "show_studio_as_text"
const CSSEnabled = "cssEnabled" const CSSEnabled = "cssEnabled"
// Playback force codec,container
const ForceMKV = "forceMKV"
const ForceHEVC = "forceHEVC"
// Logging options // Logging options
const LogFile = "logFile" const LogFile = "logFile"
const LogOut = "logOut" const LogOut = "logOut"
@@ -291,6 +295,15 @@ func GetCSSEnabled() bool {
return viper.GetBool(CSSEnabled) return viper.GetBool(CSSEnabled)
} }
// force codec,container
func GetForceMKV() bool {
return viper.GetBool(ForceMKV)
}
func GetForceHEVC() bool {
return viper.GetBool(ForceHEVC)
}
// GetLogFile returns the filename of the file to output logs to. // GetLogFile returns the filename of the file to output logs to.
// An empty string means that file logging will be disabled. // An empty string means that file logging will be disabled.
func GetLogFile() string { func GetLogFile() string {

View File

@@ -22,6 +22,7 @@ type SceneFile struct {
Duration string `json:"duration"` Duration string `json:"duration"`
VideoCodec string `json:"video_codec"` VideoCodec string `json:"video_codec"`
AudioCodec string `json:"audio_codec"` AudioCodec string `json:"audio_codec"`
Format string `json:"format"`
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
Framerate string `json:"framerate"` Framerate string `json:"framerate"`

View File

@@ -165,6 +165,9 @@ func (t *ExportTask) ExportScenes(ctx context.Context) {
if scene.AudioCodec.Valid { if scene.AudioCodec.Valid {
newSceneJSON.File.AudioCodec = scene.AudioCodec.String newSceneJSON.File.AudioCodec = scene.AudioCodec.String
} }
if scene.Format.Valid {
newSceneJSON.File.Format = scene.Format.String
}
if scene.Width.Valid { if scene.Width.Valid {
newSceneJSON.File.Width = int(scene.Width.Int64) newSceneJSON.File.Width = int(scene.Width.Int64)
} }

View File

@@ -501,6 +501,9 @@ func (t *ImportTask) ImportScenes(ctx context.Context) {
if sceneJSON.File.AudioCodec != "" { if sceneJSON.File.AudioCodec != "" {
newScene.AudioCodec = sql.NullString{String: sceneJSON.File.AudioCodec, Valid: true} newScene.AudioCodec = sql.NullString{String: sceneJSON.File.AudioCodec, Valid: true}
} }
if sceneJSON.File.Format != "" {
newScene.Format = sql.NullString{String: sceneJSON.File.Format, Valid: true}
}
if sceneJSON.File.Width != 0 { if sceneJSON.File.Width != 0 {
newScene.Width = sql.NullInt64{Int64: int64(sceneJSON.File.Width), Valid: true} newScene.Width = sql.NullInt64{Int64: int64(sceneJSON.File.Width), Valid: true}
} }

View File

@@ -133,8 +133,31 @@ func (t *ScanTask) scanScene() {
qb := models.NewSceneQueryBuilder() qb := models.NewSceneQueryBuilder()
scene, _ := qb.FindByPath(t.FilePath) scene, _ := qb.FindByPath(t.FilePath)
if scene != nil { if scene != nil {
// We already have this item in the database, check for thumbnails,screenshots // We already have this item in the database
//check for thumbnails,screenshots
t.makeScreenshots(nil, scene.Checksum) t.makeScreenshots(nil, scene.Checksum)
//check for container
if !scene.Format.Valid {
videoFile, err := ffmpeg.NewVideoFile(instance.FFProbePath, t.FilePath)
if err != nil {
logger.Error(err.Error())
return
}
container := ffmpeg.MatchContainer(videoFile.Container, t.FilePath)
logger.Infof("Adding container %s to file %s", container, t.FilePath)
ctx := context.TODO()
tx := database.DB.MustBeginTx(ctx, nil)
err = qb.UpdateFormat(scene.ID, string(container), tx)
if err != nil {
logger.Error(err.Error())
_ = tx.Rollback()
} else if err := tx.Commit(); err != nil {
logger.Error(err.Error())
}
}
return return
} }
@@ -143,6 +166,7 @@ func (t *ScanTask) scanScene() {
logger.Error(err.Error()) logger.Error(err.Error())
return return
} }
container := ffmpeg.MatchContainer(videoFile.Container, t.FilePath)
// Override title to be filename if UseFileMetadata is false // Override title to be filename if UseFileMetadata is false
if !t.UseFileMetadata { if !t.UseFileMetadata {
@@ -182,6 +206,7 @@ func (t *ScanTask) scanScene() {
Duration: sql.NullFloat64{Float64: videoFile.Duration, Valid: true}, Duration: sql.NullFloat64{Float64: videoFile.Duration, Valid: true},
VideoCodec: sql.NullString{String: videoFile.VideoCodec, Valid: true}, VideoCodec: sql.NullString{String: videoFile.VideoCodec, Valid: true},
AudioCodec: sql.NullString{String: videoFile.AudioCodec, Valid: true}, AudioCodec: sql.NullString{String: videoFile.AudioCodec, Valid: true},
Format: sql.NullString{String: string(container), Valid: true},
Width: sql.NullInt64{Int64: int64(videoFile.Width), Valid: true}, Width: sql.NullInt64{Int64: int64(videoFile.Width), Valid: true},
Height: sql.NullInt64{Int64: int64(videoFile.Height), Valid: true}, Height: sql.NullInt64{Int64: int64(videoFile.Height), Valid: true},
Framerate: sql.NullFloat64{Float64: videoFile.FrameRate, Valid: true}, Framerate: sql.NullFloat64{Float64: videoFile.FrameRate, Valid: true},

View File

@@ -16,17 +16,37 @@ type GenerateTranscodeTask struct {
func (t *GenerateTranscodeTask) Start(wg *sync.WaitGroup) { func (t *GenerateTranscodeTask) Start(wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
videoCodec := t.Scene.VideoCodec.String
if ffmpeg.IsValidCodec(videoCodec) {
return
}
hasTranscode, _ := HasTranscode(&t.Scene) hasTranscode, _ := HasTranscode(&t.Scene)
if hasTranscode { if hasTranscode {
return return
} }
logger.Infof("[transcode] <%s> scene has codec %s", t.Scene.Checksum, t.Scene.VideoCodec.String) var container ffmpeg.Container
if t.Scene.Format.Valid {
container = ffmpeg.Container(t.Scene.Format.String)
} else { // container isn't in the DB
// shouldn't happen unless user hasn't scanned after updating to PR#384+ version
tmpVideoFile, err := ffmpeg.NewVideoFile(instance.FFProbePath, t.Scene.Path)
if err != nil {
logger.Errorf("[transcode] error reading video file: %s", err.Error())
return
}
container = ffmpeg.MatchContainer(tmpVideoFile.Container, t.Scene.Path)
}
videoCodec := t.Scene.VideoCodec.String
audioCodec := ffmpeg.MissingUnsupported
if t.Scene.AudioCodec.Valid {
audioCodec = ffmpeg.AudioCodec(t.Scene.AudioCodec.String)
}
if ffmpeg.IsValidCodec(videoCodec) && ffmpeg.IsValidCombo(videoCodec, container) && ffmpeg.IsValidAudioForContainer(audioCodec, container) {
return
}
videoFile, err := ffmpeg.NewVideoFile(instance.FFProbePath, t.Scene.Path) videoFile, err := ffmpeg.NewVideoFile(instance.FFProbePath, t.Scene.Path)
if err != nil { if err != nil {
@@ -41,24 +61,52 @@ func (t *GenerateTranscodeTask) Start(wg *sync.WaitGroup) {
MaxTranscodeSize: transcodeSize, MaxTranscodeSize: transcodeSize,
} }
encoder := ffmpeg.NewEncoder(instance.FFMPEGPath) encoder := ffmpeg.NewEncoder(instance.FFMPEGPath)
encoder.Transcode(*videoFile, options)
if videoCodec == ffmpeg.H264 { // for non supported h264 files stream copy the video part
if audioCodec == ffmpeg.MissingUnsupported {
encoder.CopyVideo(*videoFile, options)
} else {
encoder.TranscodeAudio(*videoFile, options)
}
} else {
if audioCodec == ffmpeg.MissingUnsupported {
//ffmpeg fails if it trys to transcode an unsupported audio codec
encoder.TranscodeVideo(*videoFile, options)
} else {
encoder.Transcode(*videoFile, options)
}
}
if err := os.Rename(outputPath, instance.Paths.Scene.GetTranscodePath(t.Scene.Checksum)); err != nil { if err := os.Rename(outputPath, instance.Paths.Scene.GetTranscodePath(t.Scene.Checksum)); err != nil {
logger.Errorf("[transcode] error generating transcode: %s", err.Error()) logger.Errorf("[transcode] error generating transcode: %s", err.Error())
return return
} }
logger.Debugf("[transcode] <%s> created transcode: %s", t.Scene.Checksum, outputPath) logger.Debugf("[transcode] <%s> created transcode: %s", t.Scene.Checksum, outputPath)
return return
} }
// return true if transcode is needed
// used only when counting files to generate, doesn't affect the actual transcode generation
// if container is missing from DB it is treated as non supported in order not to delay the user
func (t *GenerateTranscodeTask) isTranscodeNeeded() bool { func (t *GenerateTranscodeTask) isTranscodeNeeded() bool {
videoCodec := t.Scene.VideoCodec.String videoCodec := t.Scene.VideoCodec.String
hasTranscode, _ := HasTranscode(&t.Scene) container := ""
audioCodec := ffmpeg.MissingUnsupported
if t.Scene.AudioCodec.Valid {
audioCodec = ffmpeg.AudioCodec(t.Scene.AudioCodec.String)
}
if ffmpeg.IsValidCodec(videoCodec) { if t.Scene.Format.Valid {
container = t.Scene.Format.String
}
if ffmpeg.IsValidCodec(videoCodec) && ffmpeg.IsValidCombo(videoCodec, ffmpeg.Container(container)) && ffmpeg.IsValidAudioForContainer(audioCodec, ffmpeg.Container(container)) {
return false return false
} }
hasTranscode, _ := HasTranscode(&t.Scene)
if hasTranscode { if hasTranscode {
return false return false
} }

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/stashapp/stash/pkg/ffmpeg" "github.com/stashapp/stash/pkg/ffmpeg"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils" "github.com/stashapp/stash/pkg/utils"
) )
@@ -12,12 +13,30 @@ func IsStreamable(scene *models.Scene) (bool, error) {
if scene == nil { if scene == nil {
return false, fmt.Errorf("nil scene") return false, fmt.Errorf("nil scene")
} }
var container ffmpeg.Container
if scene.Format.Valid {
container = ffmpeg.Container(scene.Format.String)
} else { // container isn't in the DB
// shouldn't happen, fallback to ffprobe reading from file
tmpVideoFile, err := ffmpeg.NewVideoFile(instance.FFProbePath, scene.Path)
if err != nil {
return false, fmt.Errorf("error reading video file: %s", err.Error())
}
container = ffmpeg.MatchContainer(tmpVideoFile.Container, scene.Path)
}
videoCodec := scene.VideoCodec.String videoCodec := scene.VideoCodec.String
if ffmpeg.IsValidCodec(videoCodec) { audioCodec := ffmpeg.MissingUnsupported
if scene.AudioCodec.Valid {
audioCodec = ffmpeg.AudioCodec(scene.AudioCodec.String)
}
if ffmpeg.IsValidCodec(videoCodec) && ffmpeg.IsValidCombo(videoCodec, container) && ffmpeg.IsValidAudioForContainer(audioCodec, container) {
logger.Debugf("File is streamable %s, %s, %s\n", videoCodec, audioCodec, container)
return true, nil return true, nil
} else { } else {
hasTranscode, _ := HasTranscode(scene) hasTranscode, _ := HasTranscode(scene)
logger.Debugf("File is not streamable , transcode is needed %s, %s, %s\n", videoCodec, audioCodec, container)
return hasTranscode, nil return hasTranscode, nil
} }
} }

View File

@@ -19,6 +19,7 @@ type Scene struct {
Size sql.NullString `db:"size" json:"size"` Size sql.NullString `db:"size" json:"size"`
Duration sql.NullFloat64 `db:"duration" json:"duration"` Duration sql.NullFloat64 `db:"duration" json:"duration"`
VideoCodec sql.NullString `db:"video_codec" json:"video_codec"` VideoCodec sql.NullString `db:"video_codec" json:"video_codec"`
Format sql.NullString `db:"format" json:"format_name"`
AudioCodec sql.NullString `db:"audio_codec" json:"audio_codec"` AudioCodec sql.NullString `db:"audio_codec" json:"audio_codec"`
Width sql.NullInt64 `db:"width" json:"width"` Width sql.NullInt64 `db:"width" json:"width"`
Height sql.NullInt64 `db:"height" json:"height"` Height sql.NullInt64 `db:"height" json:"height"`

View File

@@ -50,10 +50,10 @@ func (qb *SceneQueryBuilder) Create(newScene Scene, tx *sqlx.Tx) (*Scene, error)
ensureTx(tx) ensureTx(tx)
result, err := tx.NamedExec( result, err := tx.NamedExec(
`INSERT INTO scenes (checksum, path, title, details, url, date, rating, size, duration, video_codec, `INSERT INTO scenes (checksum, path, title, details, url, date, rating, size, duration, video_codec,
audio_codec, width, height, framerate, bitrate, studio_id, cover, audio_codec, format, width, height, framerate, bitrate, studio_id, cover,
created_at, updated_at) created_at, updated_at)
VALUES (:checksum, :path, :title, :details, :url, :date, :rating, :size, :duration, :video_codec, VALUES (:checksum, :path, :title, :details, :url, :date, :rating, :size, :duration, :video_codec,
:audio_codec, :width, :height, :framerate, :bitrate, :studio_id, :cover, :audio_codec, :format, :width, :height, :framerate, :bitrate, :studio_id, :cover,
:created_at, :updated_at) :created_at, :updated_at)
`, `,
newScene, newScene,
@@ -534,3 +534,16 @@ func (qb *SceneQueryBuilder) queryScenes(query string, args []interface{}, tx *s
return scenes, nil return scenes, nil
} }
func (qb *SceneQueryBuilder) UpdateFormat(id int, format string, tx *sqlx.Tx) error {
ensureTx(tx)
_, err := tx.Exec(
`UPDATE scenes SET format = ? WHERE scenes.id = ? `,
format, id,
)
if err != nil {
return err
}
return nil
}

View File

@@ -22,6 +22,8 @@ export const SettingsConfigurationPanel: React.FC = () => {
const [maxStreamingTranscodeSize, setMaxStreamingTranscodeSize] = useState< const [maxStreamingTranscodeSize, setMaxStreamingTranscodeSize] = useState<
GQL.StreamingResolutionEnum | undefined GQL.StreamingResolutionEnum | undefined
>(undefined); >(undefined);
const [forceMkv, setForceMkv] = useState<boolean>(false);
const [forceHevc, setForceHevc] = useState<boolean>(false);
const [username, setUsername] = useState<string | undefined>(undefined); const [username, setUsername] = useState<string | undefined>(undefined);
const [password, setPassword] = useState<string | undefined>(undefined); const [password, setPassword] = useState<string | undefined>(undefined);
const [maxSessionAge, setMaxSessionAge] = useState<number>(0); const [maxSessionAge, setMaxSessionAge] = useState<number>(0);
@@ -42,6 +44,8 @@ export const SettingsConfigurationPanel: React.FC = () => {
generatedPath, generatedPath,
maxTranscodeSize, maxTranscodeSize,
maxStreamingTranscodeSize, maxStreamingTranscodeSize,
forceMkv,
forceHevc,
username, username,
password, password,
maxSessionAge, maxSessionAge,
@@ -65,6 +69,8 @@ export const SettingsConfigurationPanel: React.FC = () => {
setMaxStreamingTranscodeSize( setMaxStreamingTranscodeSize(
conf.general.maxStreamingTranscodeSize ?? undefined conf.general.maxStreamingTranscodeSize ?? undefined
); );
setForceMkv(conf.general.forceMkv);
setForceHevc(conf.general.forceHevc);
setUsername(conf.general.username); setUsername(conf.general.username);
setPassword(conf.general.password); setPassword(conf.general.password);
setMaxSessionAge(conf.general.maxSessionAge); setMaxSessionAge(conf.general.maxSessionAge);
@@ -293,6 +299,28 @@ export const SettingsConfigurationPanel: React.FC = () => {
Maximum size for transcoded streams Maximum size for transcoded streams
</Form.Text> </Form.Text>
</Form.Group> </Form.Group>
<Form.Group id="force-options-mkv">
<Form.Check
id="force-mkv"
checked={forceMkv}
label="Force Matroska as supported"
onChange={() => setForceMkv(!forceMkv)}
/>
<Form.Text className="text-muted">
Treat Matroska (MKV) as a supported container. Recommended for Chromium based browsers
</Form.Text>
</Form.Group>
<Form.Group id="force-options-hevc">
<Form.Check
id="force-hevc"
checked={forceHevc}
label="Force HEVC as supported"
onChange={() => setForceHevc(!forceHevc)}
/>
<Form.Text className="text-muted">
Treat HEVC as a supported codec. Recommended for Safari or some Android based browsers
</Form.Text>
</Form.Group>
</Form.Group> </Form.Group>
<hr /> <hr />

View File

@@ -16,7 +16,7 @@ import { ErrorUtils } from "../../utils/errors";
import { ToastUtils } from "../../utils/toasts"; import { ToastUtils } from "../../utils/toasts";
import { FolderSelect } from "../Shared/FolderSelect/FolderSelect"; import { FolderSelect } from "../Shared/FolderSelect/FolderSelect";
interface IProps {} interface IProps { }
export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IProps) => { export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IProps) => {
// Editing config state // Editing config state
@@ -25,6 +25,8 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
const [generatedPath, setGeneratedPath] = useState<string | undefined>(undefined); const [generatedPath, setGeneratedPath] = useState<string | undefined>(undefined);
const [maxTranscodeSize, setMaxTranscodeSize] = useState<GQL.StreamingResolutionEnum | undefined>(undefined); const [maxTranscodeSize, setMaxTranscodeSize] = useState<GQL.StreamingResolutionEnum | undefined>(undefined);
const [maxStreamingTranscodeSize, setMaxStreamingTranscodeSize] = useState<GQL.StreamingResolutionEnum | undefined>(undefined); const [maxStreamingTranscodeSize, setMaxStreamingTranscodeSize] = useState<GQL.StreamingResolutionEnum | undefined>(undefined);
const [forceMkv, setForceMkv] = useState<boolean>(false);
const [forceHevc, setForceHevc] = useState<boolean>(false);
const [username, setUsername] = useState<string | undefined>(undefined); const [username, setUsername] = useState<string | undefined>(undefined);
const [password, setPassword] = useState<string | undefined>(undefined); const [password, setPassword] = useState<string | undefined>(undefined);
const [logFile, setLogFile] = useState<string | undefined>(); const [logFile, setLogFile] = useState<string | undefined>();
@@ -42,6 +44,8 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
generatedPath, generatedPath,
maxTranscodeSize, maxTranscodeSize,
maxStreamingTranscodeSize, maxStreamingTranscodeSize,
forceMkv,
forceHevc,
username, username,
password, password,
logFile, logFile,
@@ -61,6 +65,8 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
setGeneratedPath(conf.general.generatedPath); setGeneratedPath(conf.general.generatedPath);
setMaxTranscodeSize(conf.general.maxTranscodeSize); setMaxTranscodeSize(conf.general.maxTranscodeSize);
setMaxStreamingTranscodeSize(conf.general.maxStreamingTranscodeSize); setMaxStreamingTranscodeSize(conf.general.maxStreamingTranscodeSize);
setForceMkv(conf.general.forceMkv);
setForceHevc(conf.general.forceHevc);
setUsername(conf.general.username); setUsername(conf.general.username);
setPassword(conf.general.password); setPassword(conf.general.password);
setLogFile(conf.general.logFile); setLogFile(conf.general.logFile);
@@ -77,15 +83,15 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
} }
function excludeRegexChanged(idx: number, value: string) { function excludeRegexChanged(idx: number, value: string) {
const newExcludes = excludes.map((regex, i)=> { const newExcludes = excludes.map((regex, i) => {
const ret = ( idx !== i ) ? regex : value ; const ret = (idx !== i) ? regex : value;
return ret return ret
}) })
setExcludes(newExcludes); setExcludes(newExcludes);
} }
function excludeRemoveRegex(idx: number) { function excludeRemoveRegex(idx: number) {
const newExcludes = excludes.filter((regex, i) => i!== idx ); const newExcludes = excludes.filter((regex, i) => i !== idx);
setExcludes(newExcludes); setExcludes(newExcludes);
} }
@@ -117,7 +123,7 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
GQL.StreamingResolutionEnum.Original GQL.StreamingResolutionEnum.Original
].map(resolutionToString); ].map(resolutionToString);
function resolutionToString(r : GQL.StreamingResolutionEnum | undefined) { function resolutionToString(r: GQL.StreamingResolutionEnum | undefined) {
switch (r) { switch (r) {
case GQL.StreamingResolutionEnum.Low: return "240p"; case GQL.StreamingResolutionEnum.Low: return "240p";
case GQL.StreamingResolutionEnum.Standard: return "480p"; case GQL.StreamingResolutionEnum.Standard: return "480p";
@@ -130,7 +136,7 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
return "Original"; return "Original";
} }
function translateQuality(quality : string) { function translateQuality(quality: string) {
switch (quality) { switch (quality) {
case "240p": return GQL.StreamingResolutionEnum.Low; case "240p": return GQL.StreamingResolutionEnum.Low;
case "480p": return GQL.StreamingResolutionEnum.Standard; case "480p": return GQL.StreamingResolutionEnum.Standard;
@@ -160,7 +166,7 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
/> />
</FormGroup> </FormGroup>
</FormGroup> </FormGroup>
<FormGroup <FormGroup
label="Database Path" label="Database Path"
helperText="File location for the SQLite database (requires restart)" helperText="File location for the SQLite database (requires restart)"
@@ -179,16 +185,16 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
label="Excluded Patterns" label="Excluded Patterns"
> >
{ (excludes) ? excludes.map((regexp, i) => { {(excludes) ? excludes.map((regexp, i) => {
return( return (
<InputGroup <InputGroup
value={regexp} value={regexp}
onChange={(e: any) => excludeRegexChanged(i, e.target.value)} onChange={(e: any) => excludeRegexChanged(i, e.target.value)}
rightElement={<Button icon="minus" minimal={true} intent="danger" onClick={(e: any) => excludeRemoveRegex(i)} />} rightElement={<Button icon="minus" minimal={true} intent="danger" onClick={(e: any) => excludeRemoveRegex(i)} />}
/> />
); );
}) : null }) : null
} }
<Button icon="plus" minimal={true} onClick={(e: any) => excludeAddRegex()} /> <Button icon="plus" minimal={true} onClick={(e: any) => excludeAddRegex()} />
<div> <div>
@@ -198,37 +204,55 @@ export const SettingsConfigurationPanel: FunctionComponent<IProps> = (props: IPr
rightIcon="help" rightIcon="help"
text="Regexps of files/paths to exclude from Scan and add to Clean" text="Regexps of files/paths to exclude from Scan and add to Clean"
minimal={true} minimal={true}
target="_blank" target="_blank"
/> />
</p> </p>
</div> </div>
</FormGroup> </FormGroup>
</FormGroup> </FormGroup>
<Divider /> <Divider />
<FormGroup> <FormGroup>
<H4>Video</H4> <H4>Video</H4>
<FormGroup <FormGroup
label="Maximum transcode size" label="Maximum transcode size"
helperText="Maximum size for generated transcodes" helperText="Maximum size for generated transcodes"
> >
<HTMLSelect <HTMLSelect
options={transcodeQualities} options={transcodeQualities}
onChange={(event) => setMaxTranscodeSize(translateQuality(event.target.value))} onChange={(event) => setMaxTranscodeSize(translateQuality(event.target.value))}
value={resolutionToString(maxTranscodeSize)} value={resolutionToString(maxTranscodeSize)}
/> />
</FormGroup>
<FormGroup
label="Maximum streaming transcode size"
helperText="Maximum size for transcoded streams"
>
<HTMLSelect
options={transcodeQualities}
onChange={(event) => setMaxStreamingTranscodeSize(translateQuality(event.target.value))}
value={resolutionToString(maxStreamingTranscodeSize)}
/>
</FormGroup>
</FormGroup> </FormGroup>
<FormGroup
label="Maximum streaming transcode size"
helperText="Maximum size for transcoded streams"
>
<HTMLSelect
options={transcodeQualities}
onChange={(event) => setMaxStreamingTranscodeSize(translateQuality(event.target.value))}
value={resolutionToString(maxStreamingTranscodeSize)}
/>
</FormGroup>
<FormGroup
helperText="Treat Matroska (MKV) as a supported container. Recommended for Chromium based browsers"
>
<Checkbox
checked={forceMkv}
label="Force Matroska as supported"
onChange={() => setForceMkv(!forceMkv)}
/>
</FormGroup>
<FormGroup
helperText="Treat HEVC as a supported codec. Recommended for Safari or some Android based browsers"
>
<Checkbox
checked={forceHevc}
label="Force HEVC as supported"
onChange={() => setForceHevc(!forceHevc)}
/>
</FormGroup>
</FormGroup>
<Divider /> <Divider />
<FormGroup> <FormGroup>