Skip to content
Snippets Groups Projects
Commit 6842a7e9 authored by ale's avatar ale
Browse files

use ioutil.TempFile, not our obsolete replacement

parent 736912e2
Branches
No related tags found
No related merge requests found
......@@ -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()
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment