forked from localhorst/media_management_scripts
		
	add new convert script that uses solar power metric
This commit is contained in:
		@ -21,7 +21,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## Usage 
 | 
					## Usage 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
`python ./check_metadata.py path`
 | 
					`python ./check_metadata.py path codec_filter`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Features
 | 
					## Features
 | 
				
			||||||
- find all video files in path
 | 
					- find all video files in path
 | 
				
			||||||
 | 
				
			|||||||
@ -109,7 +109,7 @@ def print_all(media_files, filter_mode):
 | 
				
			|||||||
    if(filter_mode != " "):        
 | 
					    if(filter_mode != " "):        
 | 
				
			||||||
        print("\nFound files with selected filter: " + filter_mode + "\n")
 | 
					        print("\nFound files with selected filter: " + filter_mode + "\n")
 | 
				
			||||||
        for media_file_filtered in media_files_filtered:
 | 
					        for media_file_filtered in media_files_filtered:
 | 
				
			||||||
            print(media_file_filtered)
 | 
					            print ('"'+media_file_filtered+'", ')
 | 
				
			||||||
        print("\n")
 | 
					        print("\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def print_codecs(media_files):
 | 
					def print_codecs(media_files):
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										112
									
								
								convert/convert.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								convert/convert.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,112 @@
 | 
				
			|||||||
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					import subprocess
 | 
				
			||||||
 | 
					import time
 | 
				
			||||||
 | 
					import requests
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					from pathlib import Path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROM_URL = "http://127.0.0.1:9104/metrics"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def load_config(path):
 | 
				
			||||||
 | 
					    """Load JSON configuration file."""
 | 
				
			||||||
 | 
					    with open(path, "r", encoding="utf-8") as f:
 | 
				
			||||||
 | 
					        return json.load(f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def wait_for_solar_power():
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Wait until the Prometheus metric `fronius_site_autonomy_ratio` equals 1.0.
 | 
				
			||||||
 | 
					    Check every 5 minutes if not available.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    while True:
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            r = requests.get(PROM_URL, timeout=5)
 | 
				
			||||||
 | 
					            if r.status_code == 200:
 | 
				
			||||||
 | 
					                for line in r.text.splitlines():
 | 
				
			||||||
 | 
					                    if line.startswith("fronius_site_autonomy_ratio"):
 | 
				
			||||||
 | 
					                        try:
 | 
				
			||||||
 | 
					                            value = float(line.split()[-1])
 | 
				
			||||||
 | 
					                            if value == 1.0:
 | 
				
			||||||
 | 
					                                print("[INFO] Solar power available – starting conversion.")
 | 
				
			||||||
 | 
					                                return
 | 
				
			||||||
 | 
					                        except ValueError:
 | 
				
			||||||
 | 
					                            pass
 | 
				
			||||||
 | 
					            print("[INFO] No solar power – waiting 5 minutes...")
 | 
				
			||||||
 | 
					        except requests.RequestException as e:
 | 
				
			||||||
 | 
					            print(f"[WARN] Could not reach Prometheus: {e}")
 | 
				
			||||||
 | 
					        time.sleep(300)  # Wait 5 minutes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def analyze_codecs(oldfile, newfile, dst_folder):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Run codecVis to compare old and new files.
 | 
				
			||||||
 | 
					    Then rename output.png to newfile.png.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    cmd = ["codecVis", oldfile, newfile]
 | 
				
			||||||
 | 
					    print(f"[CMD] {' '.join(cmd)}")
 | 
				
			||||||
 | 
					    subprocess.run(cmd, cwd=dst_folder, check=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Rename output.png to match the new file
 | 
				
			||||||
 | 
					    output_png = Path(dst_folder) / "output.png"
 | 
				
			||||||
 | 
					    new_png = Path(dst_folder) / (Path(newfile).name + ".png")
 | 
				
			||||||
 | 
					    if output_png.exists():
 | 
				
			||||||
 | 
					        output_png.rename(new_png)
 | 
				
			||||||
 | 
					        print(f"[INFO] Analysis image saved as {new_png}")
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        print("[WARN] output.png not found!")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def main():
 | 
				
			||||||
 | 
					    if len(sys.argv) != 2:
 | 
				
			||||||
 | 
					        print("Give path to config file as argument.")
 | 
				
			||||||
 | 
					        sys.exit(1)
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					    cfg = load_config(sys.argv[1])
 | 
				
			||||||
 | 
					    dst_folder = Path(cfg["dst_folder"])
 | 
				
			||||||
 | 
					    src_folder = Path(cfg["src_folder"])
 | 
				
			||||||
 | 
					    dst_folder.mkdir(parents=True, exist_ok=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for job in cfg["jobs"]:
 | 
				
			||||||
 | 
					        src_file = src_folder / job
 | 
				
			||||||
 | 
					        tmp_movie_name = "tmp_" + Path(job).stem + ".mkv"
 | 
				
			||||||
 | 
					        movie_name = Path(job).stem + ".mkv"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        tmp_dst_file = dst_folder / tmp_movie_name
 | 
				
			||||||
 | 
					        dst_file = dst_folder / movie_name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        print(f"Source: {src_file}")
 | 
				
			||||||
 | 
					        print(f"Temp name: {tmp_movie_name}")
 | 
				
			||||||
 | 
					        print(f"Final name: {movie_name}")
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        # Remove leftover temporary file
 | 
				
			||||||
 | 
					        if tmp_dst_file.exists():
 | 
				
			||||||
 | 
					            print(f"[INFO] File {tmp_dst_file} already exists. --> Delete!")
 | 
				
			||||||
 | 
					            tmp_dst_file.unlink()
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					        # Skip if final file already exists
 | 
				
			||||||
 | 
					        if dst_file.exists():
 | 
				
			||||||
 | 
					            print(f"[INFO] Skip {dst_file}, already exists. --> Convert already done!")
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        wait_for_solar_power()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            cmd = [
 | 
				
			||||||
 | 
					                "ffmpeg", "-i", str(src_file),
 | 
				
			||||||
 | 
					                "-vcodec", "libx264", "-acodec", "aac",
 | 
				
			||||||
 | 
					                str(tmp_dst_file)
 | 
				
			||||||
 | 
					            ]
 | 
				
			||||||
 | 
					            print(f"[CMD] {' '.join(cmd)}")
 | 
				
			||||||
 | 
					            subprocess.run(cmd, check=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # Rename temp file to final name
 | 
				
			||||||
 | 
					            tmp_dst_file.rename(dst_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # Run codec analysis
 | 
				
			||||||
 | 
					            analyze_codecs(str(src_file), str(dst_file), dst_folder)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        except subprocess.CalledProcessError as e:
 | 
				
			||||||
 | 
					            print(f"[ERROR] Processing failed for {job}: {e}")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					    main()
 | 
				
			||||||
							
								
								
									
										8
									
								
								convert/convert_jobs.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								convert/convert_jobs.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "dst_folder": "/mnt/mainstorage/media/converted",
 | 
				
			||||||
 | 
					  "src_folder": "/mnt/mainstorage/media/movies",
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  "jobs": [
 | 
				
			||||||
 | 
					    "BigBuckBunny_320x180.mp4",
 | 
				
			||||||
 | 
					  ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user