Skip to content

Commit

Permalink
Fix composite download deletion (#332)
Browse files Browse the repository at this point in the history
Fix temporary file deletion issue during composite video download
  • Loading branch information
frin1 authored Dec 20, 2024
1 parent 9f98965 commit 207c91b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"io"
"log/slog"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -85,14 +86,14 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string,
if err != nil {
return err
}
defer os.Remove(videoFile.Name())
defer closeAndRemoveFile(videoFile, log)

// Create temporary audio file
audioFile, err := os.CreateTemp(outputDir, "youtube_*.m4a")
if err != nil {
return err
}
defer os.Remove(audioFile.Name())
defer closeAndRemoveFile(audioFile, log)

log.Debug("Downloading video file...")
err = dl.videoDLWorker(ctx, videoFile, v, videoFormat)
Expand Down Expand Up @@ -122,6 +123,17 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string,
return ffmpegVersionCmd.Run()
}

func closeAndRemoveFile(file *os.File, log *slog.Logger) {
if err := file.Close(); err != nil {
log.Error("Failed to close file", "error", err)
return
}
err := os.Remove(file.Name())
if err != nil {
log.Error("Failed to delete file", "error", err)
}
}

func getVideoAudioFormats(v *youtube.Video, quality string, mimetype, language string) (*youtube.Format, *youtube.Format, error) {
var videoFormats, audioFormats youtube.FormatList

Expand Down

0 comments on commit 207c91b

Please sign in to comment.