Improve deletification

It was meant to run once and not as a service...
So it's bursting at the seams
But it will work
I will make it work
This commit is contained in:
2024-11-05 15:33:21 +01:00
parent 60dc43fd9b
commit 9b41f9114c

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"io"
"log/slog"
"os"
"os/exec"
"path/filepath"
@@ -85,26 +86,28 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string,
if err != nil {
return err
}
defer os.Remove(videoFile.Name())
defer Delete(videoFile.Name(), log)
// Create temporary audio file
audioFile, err := os.CreateTemp(outputDir, "youtube_*.m4a")
if err != nil {
return err
}
defer os.Remove(audioFile.Name())
defer Delete(audioFile.Name(), log)
log.Debug("Downloading video file...")
err = dl.videoDLWorker(ctx, videoFile, v, videoFormat)
if err != nil {
return err
}
videoFile.Close()
log.Debug("Downloading audio file...")
err = dl.videoDLWorker(ctx, audioFile, v, audioFormat)
if err != nil {
return err
}
audioFile.Close()
//nolint:gosec
ffmpegVersionCmd := exec.Command("ffmpeg", "-y",
@@ -122,6 +125,13 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string,
return ffmpegVersionCmd.Run()
}
func Delete(path string, log *slog.Logger) {
err := os.Remove(path)
if err != nil {
log.Error("Failed deleting file", "path", path, "error", err)
}
}
func getVideoAudioFormats(v *youtube.Video, quality string, mimetype, language string) (*youtube.Format, *youtube.Format, error) {
var videoFormats, audioFormats youtube.FormatList