added possibility to convert (not complete)

This commit is contained in:
hendrik 2024-05-25 18:24:17 +02:00
parent 99409f913e
commit 3f651fd204
10 changed files with 185 additions and 5 deletions

36
convert_button.js Normal file
View File

@ -0,0 +1,36 @@
function create_convert_button(path_parts, side) {
let btn = document.createElement("button");
btn.type = "button";
btn.classList.add("btn", "btn-light", side+ "-convert-btn");
btn.textContent = path_parts[0] + " to mkv convertieren";
btn.dataset.path=path_parts;
let btn_div = document.getElementById("convert-button-"+ side);
btn_div.innerHTML ="";
btn.onclick = function () {
let endpoint = "converter.py";
let url = '/script/' + endpoint+ '?'
+ path_parts
.reverse()
.map((par) => 'path=' + encodeURIComponent(par))
.join('&');
let btn_div = document.getElementById("convert-button-"+ side);
btn_div.innerHTML ="";
fetch(url)
.then(response => response.json())
.then(data => {
if(Object.keys( data).length === 0) {
alert("something went wrong");
} else {
alert("look for file at "+ data["path"]);
}
})
.catch(error => console.error('Error fetching table data:', error));
};
btn_div.appendChild(btn);
}

View File

@ -36,7 +36,7 @@
.scrollable {
overflow: scroll;
height: 800px;
height: 650px;
}
.info {

View File

@ -15,6 +15,12 @@ function add_file_tree_clickhanlder(side) {
let root_dir = document.getElementById(side + "_dir").value;
path.push(root_dir);
fetchmkvdata(path,side);
} else if ( elem.textContent.endsWith('.mp3') || elem.textContent.endsWith('.mp4')) {
let path = get_path_var(elem);
path.unshift(elem.textContent);
let root_dir = document.getElementById(side + "_dir").value;
path.push(root_dir);
create_convert_button(path, side);
}
}
if (type == "folder") {
@ -27,6 +33,7 @@ function add_file_tree_clickhanlder(side) {
path.push(root_dir);
fetchTable(elem.parentElement,path,side );
fetchmkvdata(path.reverse(),side);
}

View File

@ -65,9 +65,11 @@
<tr>
<td>
<div class="beside info col-md4 bg-light p-4 m-2" id="info-left"></div>
<div id="convert-button-left"></div>
</td>
<td>
<div class="beside info col-md4 bg-light p-4 m-2" id="info-right"></div>
<div id="convert-button-right"></div>
</td>
</tr>
</table>
@ -76,6 +78,7 @@
<script src="mkvdatefetcher.js"></script>
<script src="media_info_presenter.js"></script>
<script src="filter.js"></script>
<script src="convert_button.js"></script>
<script>
function set_base_dir(side) {
let text = document.getElementById(side+"_filter");

View File

@ -44,8 +44,18 @@ function create_info_table(obj, fields) {
}
function create_media_info(data) {
if(Object.keys( data).length < 2) {
return;
}
let info_ct = document.createElement("div");
info_ct.classList.add("container","text-center");
if (data["title"] ) {
let title = document.createElement("div");
title.innerHTML="<b>title:: </b>" + data["title"] ;
info_ct.appendChild(title);
}
let video = document.createElement("div");
video.classList.add("row");
@ -67,6 +77,9 @@ function create_media_info(data) {
info_ct.id=data["side"]+"_info";
info_ct.classList.add(data["side"]+"_info");
let info_obj = document.getElementById("info-"+data["side"]);
// New selection so rm convert btn
let btn_div = document.getElementById("convert-button-"+ side);
btn_div.innerHTML ="";
info_obj.innerHTML = "";
info_obj.appendChild(info_ct);
}

2
merge_button.js Normal file
View File

@ -0,0 +1,2 @@
function() {
}

63
script/analyzer.py Executable file
View File

@ -0,0 +1,63 @@
import itertools
import sys
import os
import subprocess
import json
def prpr(js):
print(json.dumps(js, indent=4), file=sys.stderr)
RELEVANT_PROPERTIES = {
"video" : [ "display_dimensions", "language"],
"audio": ["audio_channels", "language","track_name"],
"subtitles": ["language", "track_name"]
}
class MkvAnalyzer:
@staticmethod
def get_short_mkv_info(path):
if not os.path.exists(path):
print(f"path does not exists")
return
info = json.loads(subprocess.check_output(["mkvmerge", "-J", path]).decode("utf-8"))
tracks = info["tracks"]
grouped_tracks = { x: [ v for v in tracks if v["type"] == x ] for x in [ typ for typ in set( [ x["type"] for x in tracks ])] }
for k, u in grouped_tracks.items():
[v.update({r: v["properties"][r] for r in v["properties"].keys() if r in RELEVANT_PROPERTIES[k]}) for v in u]
[ v.pop("properties") for v in u]
grouped_tracks["title"] = info["container"]["properties"].get("title", "NA ( "+ os.path.basename(os.path.normpath(path)) + ")")
grouped_tracks["chapters"] = info["chapters"]
return grouped_tracks
@staticmethod
def get_short_mkv_info_general_path(path):
if os.path.isfile(path):
return get_short_mkv_info(path)
matroskas = [ os.path.join(path, l) for l in os.listdir(path) if os.path.isfile(os.path.join(path, l)) and l.endswith(".mkv")]
results = [ MkvAnalyzer.get_short_mkv_info(file) for file in matroskas ]
num_of_results = len(results)
if len(results) == 0:
return {}
for res in results:
for key in ['video', 'audio', 'subtitles']:
res[key] = len(res.get(key, []))
muster = results[0]
matroskas = MkvAnalyzer.get_short_mkv_info(matroskas[0])
if len([r for r in results if r['video'] == muster['video'] and r['subtitles'] == muster['subtitles'] and r['audio'] == muster['audio'] ] )== num_of_results:
prpr(matroskas)
matroskas['title'] =os.path.split(os.path.split(path)[0])[1] +"/"+ os.path.basename(os.path.normpath(path))
matroskas.pop('chapter', None)
return matroskas
else:
return {}

50
script/converter.py Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env python3
import os
import json
import cgi
import sys
import subprocess
form = cgi.FieldStorage()
param = form.getvalue('path', [])
print(param, file=sys.stderr);
def ret(response = []):
print("Content-Type: application/json\n")
print(json.dumps(response))
base_dir="/data"
path=base_dir
if isinstance(param, str):
param = [param]
for el in param:
path = os.path.join(path, el)
print(path, file=sys.stderr)
if not os.path.isfile(path) or ( not path.endswith(".mp3") and not path.endswith(".mp4")):
print("Use first exit - whatever", file=sys.stderr)
print(os.path.isfile(path), file=sys.stderr)
print(path.endswith(".mp3"), file=sys.stderr)
print(path.endswith(".mp4"), file=sys.stderr)
ret()
elif path.endswith(".mp4"):
command = ["ffmpeg", "-i", path, "-vcodec", "copy", "-acodec", "copy", path+".mkv", ";"]
foo = subprocess.run(command)
ret([{"path" : path+".mkv"}])
else:
command = ["ffmpeg", "-i", path, "-acodec", "copy", path+".mkv", ";"]
print("command", file=sys.stderr)
print(command, file=sys.stderr)
foo = subprocess.run(command)
ret([{"path" : path+".mkv"}])
# todo: handle dirs? (just convert all in there?^

View File

@ -30,12 +30,17 @@ for el in param:
print(path, file=sys.stderr);
if not os.path.isfile(path) or not path.endswith(".mkv"):
if ( not os.path.isfile(path) or not path.endswith(".mkv")) and not os.path.isdir(path):
ret()
data = MkvAnalyzer.get_short_mkv_info(path)
elif os.path.isdir(path):
data = MkvAnalyzer.get_short_mkv_info_general_path(path)
data["side"] = side;
ret(data)
else:
data = MkvAnalyzer.get_short_mkv_info(path)
data["side"] = side;
ret(data)
data["side"] = side;
ret(data)

View File

@ -8,6 +8,7 @@ function fetchTable(folder_container, path, side) {
.map((par) => 'path=' + encodeURIComponent(par))
.join('&');
let folder_el = folder_container.children[0];
folder_container.innerHTML = "";
folder_container.appendChild(folder_el);