(inputFile, outputFile, outputFormat string, deleteSource bool)
| 48 | } |
| 49 | |
| 50 | func MediaFile(inputFile, outputFile, outputFormat string, deleteSource bool) (state string, err error) { |
| 51 | status := "FAILED" |
| 52 | msg := "" |
| 53 | ffmpegPath, flag := hasFfmpeg() |
| 54 | if !flag { |
| 55 | return status, fmt.Errorf("ffmpeg not found, cannot convert file") |
| 56 | } |
| 57 | logFile, err := os.OpenFile(filepath.Join(global.Dir.ConvertLogDir, "convert.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 58 | if err != nil { |
| 59 | return status, fmt.Errorf("cannot open convert.log: %w", err) |
| 60 | } |
| 61 | defer logFile.Close() |
| 62 | |
| 63 | allLogFile, err := os.OpenFile(filepath.Join(global.Dir.ConvertLogDir, "convert-all.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 64 | if err != nil { |
| 65 | return status, fmt.Errorf("cannot open convert-all.log: %w", err) |
| 66 | } |
| 67 | defer allLogFile.Close() |
| 68 | args, fileType, err := buildFFmpegArgs(inputFile, outputFile, outputFormat) |
| 69 | if err != nil { |
| 70 | return status, fmt.Errorf("FFmpeg args failed: %w", err) |
| 71 | } |
| 72 | ctx := context.Background() |
| 73 | var cancel context.CancelFunc |
| 74 | ctx, cancel = context.WithTimeout(ctx, 5*time.Minute) |
| 75 | defer cancel() |
| 76 | cmdr := exec.CommandContext(ctx, ffmpegPath, args...) |
| 77 | cmdr.Env = append(os.Environ(), |
| 78 | "PATH="+filepath.Dir(ffmpegPath)+":"+os.Getenv("PATH"), |
| 79 | "LD_LIBRARY_PATH=/usr/local/lib:"+os.Getenv("LD_LIBRARY_PATH"), |
| 80 | ) |
| 81 | var buf bytes.Buffer |
| 82 | cmdr.Stdout = &buf |
| 83 | cmdr.Stderr = &buf |
| 84 | err = cmdr.Run() |
| 85 | logStr := buf.String() |
| 86 | |
| 87 | stat, statErr := os.Stat(outputFile) |
| 88 | if err != nil || statErr != nil || stat.Size() == 0 { |
| 89 | status = "FAILED" |
| 90 | msg = extractFFmpegError(logStr) |
| 91 | _ = os.Remove(outputFile) |
| 92 | } else { |
| 93 | status = "SUCCESS" |
| 94 | msg = "SUCCESS" |
| 95 | } |
| 96 | |
| 97 | entry := response.FileConvertLog{ |
| 98 | Date: time.Now().Format("2006-01-02 15:04:05"), |
| 99 | Type: fileType, |
| 100 | Log: fmt.Sprintf("%s -> %s", inputFile, outputFile), |
| 101 | Status: status, |
| 102 | Message: msg, |
| 103 | } |
| 104 | _ = appendJSONLog(logFile, entry) |
| 105 | |
| 106 | allLogEntry := fmt.Sprintf("[%s] %s %s -> %s [%s]: %s\n", |
| 107 | time.Now().Format("2006-01-02 15:04:05"), |
nothing calls this directly
no test coverage detected