Compare commits
No commits in common. "076947ebf93e18f933fbd42d7bb01db695e959e5" and "674327e0cfcf9282b59471d2018914b092655b38" have entirely different histories.
076947ebf9
...
674327e0cf
35
adept.go
35
adept.go
@ -1,10 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"git.metaunix.net/BitGoblin/adept/config"
|
||||
"git.metaunix.net/BitGoblin/adept/transcoder"
|
||||
"git.metaunix.net/BitGoblin/adept/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -20,7 +25,31 @@ func main() {
|
||||
// initialize the video repository
|
||||
r := transcoder.NewRepository(viper.GetString("transcoder.repository"))
|
||||
|
||||
// create transcoder object and start the main loop
|
||||
t := transcoder.NewTranscoder(*r)
|
||||
t.Start()
|
||||
// main program loop - runs infinitely until externally terminated
|
||||
for {
|
||||
ingestFiles := r.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(viper.GetString("transcoder.repository"), "ingest", i.Name())) {
|
||||
log.Printf("%s appears to be open in another program; skipping it for this run.", i.Name())
|
||||
continue
|
||||
}
|
||||
|
||||
// archive file
|
||||
r.ArchiveFile(i.Name())
|
||||
// transcode file
|
||||
transcoder.Transcode(i.Name())
|
||||
// clean up source file
|
||||
r.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)
|
||||
}
|
||||
}
|
||||
|
@ -8,41 +8,31 @@ import (
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
basePath string
|
||||
ingestPath string
|
||||
archivePath string
|
||||
outputPath string
|
||||
basePath string
|
||||
}
|
||||
|
||||
func NewRepository(path string) *Repository {
|
||||
// make sure repository base directory exists
|
||||
create_repo_dir(path)
|
||||
|
||||
// make sure the ingest, archive, and output directories exist
|
||||
subDirs := []string{"ingest", "archive", "output"}
|
||||
for _, d := range subDirs {
|
||||
subPath := filepath.Join(path, d)
|
||||
create_repo_dir(subPath)
|
||||
}
|
||||
|
||||
r := new(Repository)
|
||||
r.basePath = path
|
||||
r.ingestPath = filepath.Join(path, "ingest")
|
||||
r.archivePath = filepath.Join(path, "archive")
|
||||
r.outputPath = filepath.Join(path, "output")
|
||||
|
||||
// make sure repository base directory exists
|
||||
create_repo_dir(r.basePath)
|
||||
// make sure the folder structure is setup
|
||||
create_repo_dir(r.ingestPath)
|
||||
create_repo_dir(r.archivePath)
|
||||
create_repo_dir(r.archivePath)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// Repository getters
|
||||
func (r *Repository) GetIngestPath() string {
|
||||
return r.ingestPath
|
||||
}
|
||||
func (r *Repository) GetOutputPath() string {
|
||||
return r.outputPath
|
||||
}
|
||||
|
||||
func (r *Repository) SearchIngest() []os.FileInfo {
|
||||
ingestPath := filepath.Join(r.basePath, "ingest")
|
||||
|
||||
log.Printf("Searching ingest directory for files to transcode...")
|
||||
|
||||
ingestDir, err := os.Open(r.ingestPath)
|
||||
ingestDir, err := os.Open(ingestPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening ingest directory: %s", err)
|
||||
os.Exit(1)
|
||||
@ -58,13 +48,13 @@ func (r *Repository) SearchIngest() []os.FileInfo {
|
||||
|
||||
func (r *Repository) ArchiveFile(inFile string) {
|
||||
// create ingest and archive paths
|
||||
ingestFilePath := filepath.Join(r.ingestPath, inFile)
|
||||
archiveFilePath := filepath.Join(r.archivePath, inFile)
|
||||
ingestPath := filepath.Join(r.basePath, "ingest", inFile)
|
||||
archivePath := filepath.Join(r.basePath, "archive", inFile)
|
||||
|
||||
log.Printf("Copying %s to the archive.", ingestFilePath)
|
||||
log.Printf("Copying %s to the archive.", ingestPath)
|
||||
|
||||
// open ingest file
|
||||
source, err := os.Open(ingestFilePath)
|
||||
source, err := os.Open(ingestPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening file in ingest: %s.", err)
|
||||
os.Exit(1)
|
||||
@ -72,7 +62,7 @@ func (r *Repository) ArchiveFile(inFile string) {
|
||||
defer source.Close()
|
||||
|
||||
// attempt to create destination file
|
||||
destination, err := os.Create(archiveFilePath)
|
||||
destination, err := os.Create(archivePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening archive file: %s.", err)
|
||||
os.Exit(1)
|
||||
@ -90,10 +80,10 @@ func (r *Repository) ArchiveFile(inFile string) {
|
||||
|
||||
func (r *Repository) CleanupFile(inFile string) {
|
||||
// create ingest path
|
||||
ingestFilePath := filepath.Join(r.ingestPath, inFile)
|
||||
ingestPath := filepath.Join(r.basePath, "ingest", inFile)
|
||||
|
||||
// remove the file
|
||||
os.Remove(ingestFilePath)
|
||||
os.Remove(ingestPath)
|
||||
}
|
||||
|
||||
func create_repo_dir(path string) {
|
||||
|
@ -6,61 +6,14 @@ import (
|
||||
"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) {
|
||||
func 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)
|
||||
ingestPath := filepath.Join(viper.GetString("transcoder.repository"), "ingest", inFile)
|
||||
outputPath := filepath.Join(viper.GetString("transcoder.repository"), "output", inFile)
|
||||
|
||||
log.Printf("Transcoding video file %s.", ingestPath)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user