Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use Last-Modified header to set the file mtime by default #589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ var (
RetryTimes int
// YouTubeStream2 will use data in `url_encoded_fmt_stream_map`
YouTubeStream2 bool
// NoMTime Do not use the Last-Modified header to set the file modification time
NoMTime bool
)

// FakeHeaders fake http headers
Expand Down
15 changes: 15 additions & 0 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ func Save(
}
}()

// try to update mtime before renaming file
defer func() {
if err != nil || config.NoMTime {
return
}
resHeaders, reqError := request.Headers(urlData.URL, refer)
if reqError != nil {
return
}
t, parseError := time.Parse("Mon, 02 Jan 2006 15:04:05 GMT", resHeaders.Get("Last-Modified"))
if parseError == nil {
os.Chtimes(tempFilePath, time.Now(), t)
}
}()

if chunkSizeMB > 0 {
var start, end, chunkSize int64
chunkSize = int64(chunkSizeMB) * 1024 * 1024
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func init() {
flag.IntVar(
&config.RetryTimes, "retry", 10, "How many times to retry when the download failed",
)
flag.BoolVar(&config.NoMTime, "no-mtime", false, "Do not use the Last-Modified header to set the file modification time")
// youku
flag.StringVar(&config.YoukuCcode, "ccode", "0590", "Youku ccode")
flag.StringVar(
Expand Down
15 changes: 12 additions & 3 deletions utils/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"fmt"
"os"
"os/exec"
"time"

"github.com/iawia002/annie/config"
)

func runMergeCmd(cmd *exec.Cmd, paths []string, mergeFilePath string) error {
func runMergeCmd(cmd *exec.Cmd, paths []string, mergeFilePath, mergedFilePath string) error {
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
Expand All @@ -18,6 +21,12 @@ func runMergeCmd(cmd *exec.Cmd, paths []string, mergeFilePath string) error {
if mergeFilePath != "" {
os.Remove(mergeFilePath)
}
if !config.NoMTime {
info, err := os.Stat(paths[0])
if err == nil {
os.Chtimes(mergedFilePath, time.Now(), info.ModTime())
}
}
// remove parts
for _, path := range paths {
os.Remove(path)
Expand All @@ -37,7 +46,7 @@ func MergeAudioAndVideo(paths []string, mergedFilePath string) error {
cmds, "-c:v", "copy", "-c:a", "aac", "-strict", "experimental",
mergedFilePath,
)
return runMergeCmd(exec.Command("ffmpeg", cmds...), paths, "")
return runMergeCmd(exec.Command("ffmpeg", cmds...), paths, "", mergedFilePath)
}

// MergeToMP4 merge video parts to MP4
Expand All @@ -55,5 +64,5 @@ func MergeToMP4(paths []string, mergedFilePath string, filename string) error {
"ffmpeg", "-y", "-f", "concat", "-safe", "-1",
"-i", mergeFilePath, "-c", "copy", "-bsf:a", "aac_adtstoasc", mergedFilePath,
)
return runMergeCmd(cmd, paths, mergeFilePath)
return runMergeCmd(cmd, paths, mergeFilePath, mergedFilePath)
}