diff --git a/server/actions/encode_mp3.go b/server/actions/encode_mp3.go index 9dfbed22385b32acb225b2a9439fa05ae9384968..ebde77e806a3d524c65804dec01d7206f5293120 100644 --- a/server/actions/encode_mp3.go +++ b/server/actions/encode_mp3.go @@ -4,11 +4,11 @@ import ( "errors" "fmt" "io" + "io/ioutil" "os" "git.autistici.org/ale/djrandom/api" "git.autistici.org/ale/djrandom/services" - "git.autistici.org/ale/djrandom/util" "git.autistici.org/ale/djrandom/util/ffmpeg" "git.autistici.org/ale/djrandom/util/metadata_extraction" ) @@ -48,8 +48,12 @@ func (a *EncodeMp3Action) HandleSong(session services.Session, song *api.Song, l } // Generate a temporary file to save the encoded mp3 locally. - tmpf := util.TempName("ffmpeg-mp3-", ".mp3") - defer os.Remove(tmpf) + tmpf, err := ioutil.TempFile("", "djrandom-mp3-") + if err != nil { + return err + } + tmpf.Close() + defer os.Remove(tmpf.Name()) // Download file and pipe it directly into ffmpeg. file, err := a.Storage.Open(audioFileId) @@ -65,7 +69,7 @@ func (a *EncodeMp3Action) HandleSong(session services.Session, song *api.Song, l "-acodec", "libmp3lame", "-b:a", "320k", "-f", "mp3", - tmpf, + tmpf.Name(), } fmt.Fprintf(log, "running ffmpeg: %v\n", ffmpegArgs) output, err := ffmpeg.Run(file, ffmpegArgs) @@ -77,16 +81,16 @@ func (a *EncodeMp3Action) HandleSong(session services.Session, song *api.Song, l } // Update the db with the new file! - af, _, err := metadata_extraction.GetMetadata(tmpf, api.MIMETYPE_MP3) + af, _, err := metadata_extraction.GetMetadata(tmpf.Name(), api.MIMETYPE_MP3) if err != nil { fmt.Fprintf(log, "error reading meta: %s\n", err) return err } // Add the file to the song, and upload it to remote storage. - outfile, err := os.Open(tmpf) + outfile, err := os.Open(tmpf.Name()) if err != nil { - fmt.Fprintf(log, "unexpected error re-opening file %s: %s\n", tmpf, err) + fmt.Fprintf(log, "unexpected error re-opening file %s: %s\n", tmpf.Name(), err) return err } defer outfile.Close() diff --git a/util/fingerprinting/fingerprint.go b/util/fingerprinting/fingerprint.go index b91ef2129370ca1fdcbbe7e3a3b9418d057cf928..4b093beffc434727022074e3168765cb90a17025 100644 --- a/util/fingerprinting/fingerprint.go +++ b/util/fingerprinting/fingerprint.go @@ -11,10 +11,11 @@ import ( "log" "os" "os/exec" + "path/filepath" + "strconv" "git.autistici.org/ale/djrandom/api" "git.autistici.org/ale/djrandom/services" - "git.autistici.org/ale/djrandom/util" "git.autistici.org/ale/djrandom/util/ffmpeg" ) @@ -33,7 +34,7 @@ var ( func RunCodegen(tempName string) (string, string, error) { cmd := exec.Command( *codegenBinary, tempName, "0", - fmt.Sprintf("%d", fingerprintSampleTime)) + strconv.Itoa(fingerprintSampleTime)) log.Printf("Exec: %v", cmd) stdout, err := cmd.StdoutPipe() if err != nil { @@ -105,14 +106,21 @@ func GetSongFingerprint(song *api.Song, db services.Database, storage services.S defer file.Close() // Pipe it to ffmpeg to extract a WAV sample of the audio. - tmpName := util.TempName("djrandom-fingerprint-", ".wav") - defer os.Remove(tmpName) + // This is just so that hopefully we don't have to download + // the entire file, as ffmpeg should close stdin as soon as it + // has decoded the time slice it needs. + tmpDir, err := ioutil.TempDir("", "djrandom-fingerprint-") + if err != nil { + return "", "", err + } + defer os.RemoveAll(tmpDir) + tmpName := filepath.Join(tmpDir, "fp.wav") ffmpegArgs := []string{ "-f", api.GetFfmpegFormatForMimeType(af.MimeType), "-i", "-", - "-t", fmt.Sprintf("%d", fingerprintSampleTime), - "-ss", fmt.Sprintf("%d", fingerprintSampleOffset), + "-t", strconv.Itoa(fingerprintSampleTime), + "-ss", strconv.Itoa(fingerprintSampleOffset), tmpName, } output, err := ffmpeg.Run(file, ffmpegArgs)