package transcoder import ( "fmt" "log" "os" "os/exec" "path/filepath" "strings" "time" "github.com/spf13/viper" "git.metaunix.net/BitGoblin/adept/util" ) type Transcoder struct { repo Repository } // Transcoder object constructor func NewTranscoder(r Repository) *Transcoder { t := new(Transcoder) t.repo = r return t } // Start the main transcoder loop func (t *Transcoder) Start() { // main program loop - runs infinitely until externally terminated for { ingestFiles := t.repo.SearchIngest() if len(ingestFiles) < 1 { log.Println("There were no files found in ingest to transcode; skipping run.") } else { for _, i := range ingestFiles { // check if the file is open in another program (e.g. still being written to) if util.IsFileLocked(filepath.Join(t.repo.GetIngestPath(), i.Name())) { log.Printf("%s appears to be open in another program; skipping it for this run.", i.Name()) continue } // archive file t.repo.ArchiveFile(i.Name()) // transcode file t.Transcode(i.Name()) // clean up source file t.repo.CleanupFile(i.Name()) } } // sleep for X minutes - specified in the adept.toml config file interval := viper.GetInt("transcoder.interval") time.Sleep(time.Duration(interval) * time.Minute) } } func (t *Transcoder) Transcode(inFile string) { // create ingest and archive paths ingestPath := filepath.Join(t.repo.GetIngestPath(), inFile) outputName := strings.Join([]string{util.FilenameWithoutExtension(inFile), viper.GetString("transcoder.video_format")}, ".") outputPath := filepath.Join(t.repo.GetOutputPath(), outputName) log.Printf("Transcoding video file %s.", ingestPath) cmd := exec.Command("/usr/bin/ffmpeg", "-i", ingestPath, "-y", "-f", viper.GetString("transcoder.video_format"), "-c:v", viper.GetString("transcoder.video_codec"), "-s", viper.GetString("transcoder.video_resolution"), "-r", viper.GetString("transcoder.video_framerate"), "-vf", fmt.Sprintf("format=%s", viper.GetString("transcoder.video_color")), "-profile:v", viper.GetString("transcoder.video_profile"), "-c:a", viper.GetString("transcoder.audio_codec"), outputPath) if err := cmd.Run(); err != nil { log.Fatalf("Error while transcoding: %s.", err) os.Exit(1) } }