Skip to content

Commit

Permalink
Merge branch 'dev' into mengen-only
Browse files Browse the repository at this point in the history
  • Loading branch information
Kethsar committed Nov 19, 2023
2 parents 25df562 + 0f2cfc5 commit 77c2f5c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
22 changes: 21 additions & 1 deletion Info.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type Fragment struct {
XHeadSeqNum int
Data *bytes.Buffer
Slow bool
MimeType string
}

type seqChanInfo struct {
Expand Down Expand Up @@ -905,6 +906,11 @@ func (di *DownloadInfo) downloadFragment(state *fragThreadState, dataChan chan<-
headerSeqnum, _ = strconv.Atoi(headerSeqnumStr)
}

mimeType := resp.Header.Get("Content-Type")
if !strings.HasSuffix(mimeType, "/mp4") && !strings.HasSuffix(mimeType, "/webm") {
LogTrace("%s: fragment %d has unknown MIME type '%s'", state.Name, state.SeqNum, mimeType)
}

if state.ToFile {
err = os.WriteFile(fname, respData, 0644)
if err != nil {
Expand Down Expand Up @@ -936,6 +942,7 @@ func (di *DownloadInfo) downloadFragment(state *fragThreadState, dataChan chan<-
FileName: fname,
Data: data,
Slow: isSlow,
MimeType: mimeType,
}

return
Expand Down Expand Up @@ -1157,7 +1164,20 @@ func (di *DownloadInfo) DownloadStream(dataType, dataFile string, progressChan c
buf := make([]byte, BufferSize)

rc, _ := data.Data.Read(buf)
count, err := f.Write(RemoveSidx(buf[:rc]))

writeBuf := buf
// ffmpeg doesn't like certain atoms in concatenated MP4 files, so we remove those here
// If MimeType is blank, assume MP4
if strings.HasSuffix(data.MimeType, "/mp4") || data.MimeType == "" {
badAtoms := []string{"sidx"}
// ffmpeg 6.1 doesn't like multiple ftyp atoms, so only allow on the first fragment
if curFrag != startFrag {
badAtoms = append(badAtoms, "ftyp")
}
writeBuf = RemoveAtoms(buf[:rc], badAtoms...)
}

count, err := f.Write(writeBuf)
bytesWritten += count

if err != nil {
Expand Down
29 changes: 21 additions & 8 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os/signal"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -530,19 +531,31 @@ func GetAtoms(data []byte) map[string]Atom {
return atoms
}

func RemoveSidx(data []byte) []byte {
func RemoveAtoms(data []byte, atomList ...string) []byte {
atoms := GetAtoms(data)
sidx, ok := atoms["sidx"]

if !ok {
return data
var atomsToRemove []Atom
for _, atomName := range atomList {
atom, ok := atoms[atomName]
if !ok {
continue
}
atomsToRemove = append(atomsToRemove, atom)
}

ofs := sidx.Offset
rlen := sidx.Offset + sidx.Length
newData := append(data[:ofs], data[rlen:]...)
// Sort atoms by byte offset in descending order,
// this lets us remove them in order without affecting the next atom's offset
sort.Slice(atomsToRemove, func(i, j int) bool {
return atomsToRemove[i].Offset > atomsToRemove[j].Offset
})

return newData
for _, atom := range atomsToRemove {
ofs := atom.Offset
rlen := atom.Offset + atom.Length
data = append(data[:ofs], data[rlen:]...)
}

return data
}

func GetVideoIdFromWatchPage(data []byte) string {
Expand Down

0 comments on commit 77c2f5c

Please sign in to comment.