You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.8 KiB
Julia

12 months ago
# То же самое, но на julia
# Версия 1.0 (рабочая)
12 months ago
using Logging
12 months ago
using Printf
using Dates
12 months ago
using CRC32c
using JSON
using Base.Threads
using MySQL
using Printf, Dates# using DBInterface
# debuglogger = ConsoleLogger(stderr, Logging.Debug)
# global_logger(debuglogger)
12 months ago
# include( "./Humanize.jl")
12 months ago
# import humanbytes
11 months ago
include("Humanize.jl")
12 months ago
function valid_file(filename)
11 months ago
valids = [".avi", ".mkv", ".mp4", ".webm", ".mpeg"]
12 months ago
_, ext = splitext(filename)
return ext in valids
end
12 months ago
# file_info function returns information from file
function file_info(filename)
_, ext = splitext(filename)
info = stat(filename)
12 months ago
crc = CRC32c.crc32c(open(filename, "r"))
12 months ago
m = Dict(
"filename" => filename,
"size" => info.size,
12 months ago
"hsize" => humanbytes(info.size),
12 months ago
"mtime" => info.mtime,
"ctime" => info.ctime,
"hmtime" => humandate(info.mtime),
"hctime" => humandate(info.ctime),
12 months ago
"ext" => ext,
12 months ago
"crc"=> Int(crc),
12 months ago
)
return m
end
11 months ago
# возвращает индекс видео-потока
function get_video_stream(stream)
for i in eachindex(stream)
if stream[i]["codec_type"] == "video"
return i
end
end
return -1
end
# возвращает индекс аудио-потока, -1 при отсутствии
function get_audio_stream(stream)
for i in eachindex(stream)
if stream[i]["codec_type"] == "audio"
return i
end
end
return -1
end
11 months ago
# Потоки могут быть перепутаны, поэтму нужно сначала
# определить индекс видео-потока
12 months ago
function file_meta(filename)
cmd = `ffprobe -v quiet -print_format json -show_format -show_streams "$filename"`
data = read(cmd, String) |> JSON.parse
11 months ago
n = get_video_stream(data["streams"])
audio = get_audio_stream(data["streams"]) != -1
11 months ago
dur = parse(Float64, data["format"]["duration"])
hdur = Dates.unix2datetime(dur)
hduration = Dates.format((hdur), "HH:MM:SS")
11 months ago
res = Dict(
11 months ago
"duration" => hdur,
"hduration" => hduration,
11 months ago
"width" => data["streams"][n]["width"],
"height" => data["streams"][n]["height"],
"bit_rate" => humanbitrate(data["streams"][n]["avg_frame_rate"]),
"codec" => data["streams"][n]["codec_name"],
"audio" => audio,
"tags" => "",
"type" => hduration < "00:10:00" ? "clip" : "film",
"newname" => "",
"poster" => "",
11 months ago
# "aspect_ratio" => data["streams"][n]["display_aspect_ratio"],
)
return res
12 months ago
end
12 months ago
function check_file(filename)
12 months ago
# println(filename)
v2 = file_meta(filename)
11 months ago
v1 = file_info(filename)
result = merge!(v1, v2)
12 months ago
# println(info)
return result
12 months ago
end
function main(host_id::Integer)
dir = "D:\\vids\\weed"
12 months ago
@info "Обработка каталога: $dir"
files = readdir(dir, join=true)
n = length(files)
println("всего: $n files")
conn = DBInterface.connect(MySQL.Connection, "xigmanas", "itman", "X753951x", db="vid")
12 months ago
@time begin
# @sync begin
11 months ago
for file in filter(valid_file, files)
# Threads.@spawn begin
meta = nothing
try
@elapsed meta = check_file(file)
catch e
@error "Ошибка обработки файла $file: $e"
continue
end
meta["host_id"] = host_id
# println(meta)
save_db(conn, meta)
# end
11 months ago
end
# end
12 months ago
end
println("обработано")
close(conn)
end
function exist(conn, crc::Integer)
cmd = "select count(*) as cnt from filemeta where crc = $crc"
result = DBInterface.execute(conn, cmd)
data = first(result)
# println("data = $data")
return data.cnt > 0
end
# сохраняет meta в БД
function save_db(conn, m)
filename = m["filename"]
if exist(conn, m["crc"])
@info "уже имеется: $filename"
else
# println("сохраняю: $filename")
host_id = m["host_id"]
filename = replace(m["filename"], "\\" => "\\\\")
newname = m["newname"]
size = m["size"]
hsize = m["hsize"]
hduration = m["hduration"]
width = m["width"]
height = m["height"]
bitrate = m["bit_rate"]
ext = m["ext"]
crc = m["crc"]
hctime = m["hctime"]
hmtime = m["hmtime"]
poster = m["poster"]
audio = m["audio"]
codec = m["codec"]
tags = m["tags"]
type = m["type"]
fields = """
host_id, filename, newname, size,
hsize, duration, width,
height, bitrate, ext, crc,
ctime, mtime,
poster, audio, codec, tags,
type
"""
values = """
$host_id, '$filename', '$newname', $size,
'$hsize', '$hduration', $width,
$height, '$bitrate', '$ext', $crc,
'$hctime', '$hmtime',
'$poster', $audio, '$codec', '$tags',
'$type'
"""
cmd = "insert into filemeta ($fields) values ($values)"
println("file: ", filename)
result = DBInterface.execute(conn, cmd)
# println("result: ", result)
end
12 months ago
end
function openDB()
conn = DBInterface.connect(MySQL.Connection, "xigmanas", "itman", "X753951x", db="sea")
limit = 2
ext = ".webp"
cmd = "select ext, newurl from filemeta where ext = '$ext' limit $limit"
result = DBInterface.execute(conn, cmd)
# println(typeof(result))
for s in result
println(s.ext, ' ', s.newurl)
end
end
11 months ago
#=
-------- основная программа --------------
=#
12 months ago
# parallel_execution()
# openDB()
main(1)