2021-02-23 16:01:29 +00:00
|
|
|
package videoparser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"openmediacenter/apiGo/api/types"
|
2021-09-23 17:45:58 +00:00
|
|
|
"openmediacenter/apiGo/config"
|
2021-02-23 16:01:29 +00:00
|
|
|
"openmediacenter/apiGo/database"
|
2021-09-25 18:49:47 +00:00
|
|
|
"openmediacenter/apiGo/videoparser/thumbnail"
|
2021-02-23 16:01:29 +00:00
|
|
|
"openmediacenter/apiGo/videoparser/tmdb"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2021-03-22 16:22:34 +00:00
|
|
|
"strings"
|
2021-02-23 16:01:29 +00:00
|
|
|
)
|
|
|
|
|
2021-09-23 15:38:20 +00:00
|
|
|
var mSettings *types.SettingsType
|
2021-02-23 16:01:29 +00:00
|
|
|
|
|
|
|
// default Tag ids
|
|
|
|
const (
|
|
|
|
FullHd = 2
|
|
|
|
Hd = 4
|
|
|
|
LowQuality = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
type VideoAttributes struct {
|
|
|
|
Duration float32
|
|
|
|
FileSize uint
|
|
|
|
Width uint
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:38:20 +00:00
|
|
|
func InitDeps(sett *types.SettingsType) {
|
2021-02-23 16:01:29 +00:00
|
|
|
mSettings = sett
|
2021-09-23 15:38:20 +00:00
|
|
|
}
|
2021-02-23 16:01:29 +00:00
|
|
|
|
2021-09-23 15:38:20 +00:00
|
|
|
func ReIndexVideos(path []string) {
|
2021-03-22 16:22:34 +00:00
|
|
|
// filter out those urls which are already existing in db
|
|
|
|
nonExisting := filterExisting(path)
|
|
|
|
|
|
|
|
fmt.Printf("There are %d videos not existing in db.\n", len(*nonExisting))
|
|
|
|
|
|
|
|
for _, s := range *nonExisting {
|
2021-09-23 15:38:20 +00:00
|
|
|
ProcessVideo(s)
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 19:16:38 +00:00
|
|
|
AppendMessage("reindex finished successfully!")
|
|
|
|
SendEvent("stop")
|
2021-02-23 16:01:29 +00:00
|
|
|
fmt.Println("Reindexing finished!")
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:22:34 +00:00
|
|
|
// filter those entries from array which are already existing!
|
|
|
|
func filterExisting(paths []string) *[]string {
|
|
|
|
var nameStr string
|
|
|
|
|
|
|
|
// build the query string with files on disk
|
|
|
|
for i, s := range paths {
|
|
|
|
// escape ' in url name
|
|
|
|
s = strings.Replace(s, "'", "\\'", -1)
|
|
|
|
nameStr += "SELECT '" + s + "' "
|
|
|
|
|
|
|
|
// if first index add as url
|
|
|
|
if i == 0 {
|
|
|
|
nameStr += "AS url "
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not last index add union all
|
|
|
|
if i != len(paths)-1 {
|
|
|
|
nameStr += "UNION ALL "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf("SELECT * FROM (%s) urls WHERE urls.url NOT IN(SELECT movie_url FROM videos)", nameStr)
|
|
|
|
rows := database.Query(query)
|
|
|
|
|
|
|
|
var resultarr []string
|
|
|
|
// parse the result rows into a array
|
|
|
|
for rows.Next() {
|
|
|
|
var url string
|
|
|
|
err := rows.Scan(&url)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resultarr = append(resultarr, url)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
return &resultarr
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:38:20 +00:00
|
|
|
func ProcessVideo(fileNameOrig string) {
|
|
|
|
fmt.Printf("Processing %s video\n", fileNameOrig)
|
2021-02-23 16:01:29 +00:00
|
|
|
|
|
|
|
// match the file extension
|
2021-04-16 20:48:41 +00:00
|
|
|
r := regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
|
2021-02-23 16:01:29 +00:00
|
|
|
fileName := r.ReplaceAllString(fileNameOrig, "")
|
|
|
|
|
2021-03-22 16:22:34 +00:00
|
|
|
// match the year and cut year from name
|
2021-02-23 16:01:29 +00:00
|
|
|
year, fileName := matchYear(fileName)
|
|
|
|
|
2021-03-22 16:22:34 +00:00
|
|
|
fmt.Printf("The Video %s doesn't exist! Adding it to database.\n", fileName)
|
|
|
|
addVideo(fileName, fileNameOrig, year)
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a video to the database
|
|
|
|
func addVideo(videoName string, fileName string, year int) {
|
|
|
|
var ppic *string
|
|
|
|
var tmdbData *tmdb.VideoTMDB
|
|
|
|
var err error
|
2021-09-25 18:49:47 +00:00
|
|
|
var insertid int64
|
2021-02-23 16:01:29 +00:00
|
|
|
|
2021-09-23 17:45:58 +00:00
|
|
|
vidFolder := config.GetConfig().General.ReindexPrefix + mSettings.VideoPath
|
|
|
|
|
2021-02-23 16:01:29 +00:00
|
|
|
// if TMDB grabbing is enabled serach in api for video...
|
|
|
|
if mSettings.TMDBGrabbing {
|
|
|
|
tmdbData = tmdb.SearchVideo(videoName, year)
|
|
|
|
}
|
|
|
|
|
2021-09-25 18:49:47 +00:00
|
|
|
// parse pic from 4min frame
|
2021-09-28 08:52:18 +00:00
|
|
|
ppic, vinfo, ffmpegErr := thumbnail.Parse(vidFolder+fileName, 240)
|
|
|
|
|
|
|
|
if ffmpegErr == nil {
|
|
|
|
if mSettings.TMDBGrabbing && tmdbData != nil {
|
2021-10-24 13:22:14 +00:00
|
|
|
// inesert fixed pic ratio what we get from tmdb
|
|
|
|
previewRatio := 2 / 3
|
|
|
|
query := `INSERT INTO videos(movie_name,movie_url,poster,thumbnail,quality,previewratio,length,release_date) VALUES (?,?,?,?,?,?,?,?)`
|
|
|
|
err, insertid = database.Insert(query, videoName, fileName, ppic, tmdbData.Thumbnail, vinfo.Width, previewRatio, vinfo.Length, tmdbData.ReleaseDate)
|
2021-09-28 08:52:18 +00:00
|
|
|
} else {
|
2021-10-24 13:22:14 +00:00
|
|
|
previewRatio := float32(vinfo.Height) / float32(vinfo.Width)
|
2021-09-28 08:52:18 +00:00
|
|
|
// insert without tmdb info
|
2021-10-24 13:22:14 +00:00
|
|
|
query := `INSERT INTO videos(movie_name,movie_url,poster,thumbnail,quality,previewratio,length) VALUES (?,?,?,?,?,?,?)`
|
|
|
|
err, insertid = database.Insert(query, videoName, fileName, ppic, ppic, vinfo.Width, previewRatio, vinfo.Length)
|
2021-09-28 08:52:18 +00:00
|
|
|
}
|
2021-09-25 18:49:47 +00:00
|
|
|
} else {
|
2021-09-28 08:52:18 +00:00
|
|
|
fmt.Printf("FFmpeg error occured: %s\n", ffmpegErr.Error())
|
|
|
|
|
|
|
|
if mSettings.TMDBGrabbing && tmdbData != nil {
|
|
|
|
query := `INSERT INTO videos(movie_name,movie_url,thumbnail,release_date) VALUES (?,?,?,?)`
|
|
|
|
err, insertid = database.Insert(query, videoName, fileName, tmdbData.Thumbnail, tmdbData.ReleaseDate)
|
|
|
|
} else {
|
|
|
|
query := `INSERT INTO videos(movie_name,movie_url) VALUES (?,?)`
|
|
|
|
err, insertid = database.Insert(query, videoName, fileName)
|
2021-09-25 18:49:47 +00:00
|
|
|
}
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 18:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to insert video into db: %s\n", err.Error())
|
|
|
|
return
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:52:18 +00:00
|
|
|
if ffmpegErr == nil {
|
|
|
|
// add default tags
|
|
|
|
if vinfo.Width != 0 {
|
|
|
|
insertSizeTag(uint(vinfo.Width), uint(insertid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 16:01:29 +00:00
|
|
|
// add tmdb tags
|
|
|
|
if mSettings.TMDBGrabbing && tmdbData != nil {
|
2021-09-25 18:49:47 +00:00
|
|
|
insertTMDBTags(tmdbData.GenreIds, insertid)
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 19:16:38 +00:00
|
|
|
AppendMessage(fmt.Sprintf("%s - added!", videoName))
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func matchYear(fileName string) (int, string) {
|
2021-04-16 20:48:41 +00:00
|
|
|
r := regexp.MustCompile(`\([0-9]{4}?\)`)
|
2021-02-23 16:01:29 +00:00
|
|
|
years := r.FindAllString(fileName, -1)
|
|
|
|
if len(years) == 0 {
|
|
|
|
return -1, fileName
|
|
|
|
}
|
2021-03-14 18:01:40 +00:00
|
|
|
yearStr := years[len(years)-1]
|
2021-03-14 16:29:33 +00:00
|
|
|
// get last year occurance and cut first and last char
|
2021-03-14 18:01:40 +00:00
|
|
|
year, err := strconv.Atoi(yearStr[1 : len(yearStr)-1])
|
2021-02-23 16:01:29 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return -1, fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut out year from filename
|
|
|
|
return year, r.ReplaceAllString(fileName, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert the default size tags to corresponding video
|
|
|
|
func insertSizeTag(width uint, videoId uint) {
|
|
|
|
var tagType uint
|
|
|
|
|
|
|
|
if width >= 1080 {
|
|
|
|
tagType = FullHd
|
|
|
|
} else if width >= 720 {
|
|
|
|
tagType = Hd
|
|
|
|
} else {
|
|
|
|
tagType = LowQuality
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf("INSERT INTO video_tags(video_id,tag_id) VALUES (%d,%d)", videoId, tagType)
|
|
|
|
err := database.Edit(query)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Eror occured while adding default Tag: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert id array of tmdb geners to database
|
|
|
|
func insertTMDBTags(ids []int, videoId int64) {
|
|
|
|
genres := tmdb.GetGenres()
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
var idGenre *tmdb.TMDBGenre
|
|
|
|
for _, genre := range *genres {
|
|
|
|
if genre.Id == id {
|
|
|
|
idGenre = &genre
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// skip tag if name couldn't be found
|
|
|
|
if idGenre == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we check if the tag we want to add already exists
|
|
|
|
tagId := createTagToDB(idGenre.Name)
|
2021-09-27 17:30:18 +00:00
|
|
|
if tagId != -1 {
|
|
|
|
// now we add the tag
|
|
|
|
query := fmt.Sprintf("INSERT INTO video_tags(video_id,tag_id) VALUES (%d,%d)", videoId, tagId)
|
|
|
|
_ = database.Edit(query)
|
|
|
|
}
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns id of tag or creates it if not existing
|
|
|
|
func createTagToDB(tagName string) int64 {
|
2021-09-27 17:30:18 +00:00
|
|
|
query := "SELECT tag_id FROM tags WHERE tag_name = ?"
|
|
|
|
var id int64 = -1
|
|
|
|
err := database.QueryRow(query, tagName).Scan(&id)
|
2021-02-23 16:01:29 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// tag doesn't exist -- add it
|
2021-09-27 17:30:18 +00:00
|
|
|
query = "INSERT INTO tags (tag_name) VALUES (?)"
|
|
|
|
err, id = database.Insert(query, tagName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|