From 6c59d7cf61e6a7b7426f7e680a381ac1923bdee0 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 22 Sep 2019 02:27:44 +0200 Subject: [PATCH] Replaces find720 script with Python version. --- find720.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ find720.sh | 15 ---------- 2 files changed, 83 insertions(+), 15 deletions(-) create mode 100755 find720.py delete mode 100755 find720.sh diff --git a/find720.py b/find720.py new file mode 100755 index 0000000..77ae109 --- /dev/null +++ b/find720.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import json +import os +from pprint import pprint +import subprocess +import sys + +VIDEO_EXTS = ["avi", "mp4", "mkv", "wmv", "mov", "m4v", "flv", "webm"] +MAX_HEIGHT = 720 # Only list videos with height (=shortest side) of more than this + +# https://stackoverflow.com/questions/3844430/how-to-get-the-duration-of-a-video-in-python +def ffprobe(filepath): + command = [ + "ffprobe", + "-loglevel", "quiet", + "-print_format", "json", + "-show_format", + "-show_streams", + filepath + ] + + pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + out, err = pipe.communicate() + return json.loads(out) + +def nice_size(bytesize): + units = ["B", "K", "M", "G", "T"] + bs = float(bytesize) + u = 0 + while bs > 1200: + bs = bs / 1024 + u += 1 + return "{:.2f}{}".format(bs, units[u]) + +large_vids = [] + +for root, dirs, files in os.walk("."): + for f in files: + ext = f.split(".")[-1] + if not ext in VIDEO_EXTS: + continue + filepath = "{}/{}".format(root, f) + meta = ffprobe(filepath) + #pprint(meta) + vmeta = None + if not "streams" in meta: + print("ERROR reading {}.".format(filepath), file=sys.stderr) + continue + for s in meta["streams"]: + if s["codec_type"] == "video": + vmeta = s + if vmeta is None: + print("No video stream found in {}.".format(filepath), file=sys.stderr) + continue + #pprint(vmeta) + smaller = vmeta["height"] + if vmeta["width"] < smaller: + smaller = vmeta["width"] + + if smaller > MAX_HEIGHT: + record = { + "filepath": filepath, + "width": vmeta["width"], + "height": vmeta["height"], + "filesize": meta["format"]["size"], + } + large_vids.append(record) + print("+", end="", file=sys.stderr, flush=True) + else: + print(".", end="", file=sys.stderr, flush=True) + +print(" done.", file=sys.stderr) + +# Sort in descending order with largest file first +large_vids.sort(key=lambda x: int(x["filesize"]), reverse=True) + +#pprint(large_vids) + +for v in large_vids: + print(v["filepath"]) + print("{:=4}x{:=4} {} {}".format(v["width"], v["height"], nice_size(v["filesize"]), v["filepath"]), file=sys.stderr) diff --git a/find720.sh b/find720.sh deleted file mode 100755 index 3452ba7..0000000 --- a/find720.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash -# Find video files with >720 height -find . -type f \( -iname "*.wmv" -or -iname "*.mp4" -or -iname "*.avi" -or -iname "*.mkv" -or -iname "*.flv" -or -iname "*.mov" -or -iname "*.m4v" -or -iname "*.webm" \) -print0 | while read -d $'\0' i; do - DIM=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$i") - DIMS=($(echo $DIM | tr "x" " ")) - NARROW=${DIMS[0]} - if [ ${DIMS[1]} -lt ${DIMS[0]} ]; then - NARROW=${DIMS[1]} - fi - if [ $NARROW -gt 720 ]; then - echo "$i" - else - echo -n "." > /dev/stderr - fi -done