Replaces find720 script with Python version.

This commit is contained in:
Markus Birth 2019-09-22 02:27:44 +02:00
parent f51c766a32
commit 6c59d7cf61
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
2 changed files with 83 additions and 15 deletions

83
find720.py Executable file
View File

@ -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)

View File

@ -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