Files
cwsvJudo/homepage/redesign2018/markdownExperiment/src/galleryHelper/getFfmpegFlags.py
marko f8c8404a44 - adaptive Auflösung und Bitrate für die Videos
modified:   Makefiles/Makefiles.vidGal.d/Makefile.webmCompress
	modified:   Makefiles/Makefiles.vidGal.d/Makefile.webmCompress.cfg
	new file:   galleryHelper/getFfmpegFlags.py
2019-07-08 14:43:27 +02:00

92 lines
2.6 KiB
Python

#!/usr/bin/env python3
#
# Command line use of 'ffprobe':
#
# ffprobe -loglevel quiet -print_format json \
# -show_format -show_streams \
# video-file-name.mp4
#
# man ffprobe # for more information about ffprobe
#
import subprocess as sp
import json
import sys
import argparse
def probe(vid_file_path):
''' Give a json from ffprobe command line
@vid_file_path : The absolute (full) path of the video file, string.
'''
if type(vid_file_path) != str:
raise Exception('Give ffprobe a full file path of the video')
return
command = ["ffprobe",
"-loglevel", "quiet",
"-print_format", "json",
"-show_format",
"-show_streams",
vid_file_path
]
pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT)
out, err = pipe.communicate()
return json.loads(out.decode('utf-8'))
def duration(vid_file_path):
''' Video's duration in seconds, return a float number
'''
_json = probe(vid_file_path)
if 'format' in _json:
if 'duration' in _json['format']:
return float(_json['format']['duration'])
if 'streams' in _json:
# commonly stream 0 is the video
for s in _json['streams']:
if 'duration' in s:
return float(s['duration'])
# if everything didn't happen,
# we got here because no single 'return' in the above happen.
raise Exception('I found no duration')
#return None
def getPixelSize(bitrate):
if(bitrate >= 500):
return 480
if(bitrate >= 250):
return 360
return 240
if __name__ == "__main__":
argParser = argparse.ArgumentParser(description='Calculate Bitrate and Pixelsize (height) for a encoding a video with limited (byte-)size')
argParser.add_argument('inFile', help="file to be analysed")
argParser.add_argument('flagToGet', help="Zielparameter wählen", choices=['pixelSize', 'videoBitrate'])
argParser.add_argument('--audioBitrate', help="audio bitrate die abgezogen werden soll (in kbit/s)", default=0)
argParser.add_argument('--targetSize', help="dateigröße der zieldatei in MB", default=10)
argParser.add_argument('--overhead', help="prozentualer overhead in %", default=95)
argParser.add_argument('--maxVidBitrate', help="Videobitrate, bei der gedeckelt werden soll", default=750)
argv = argParser.parse_args()
video_file_path = argv.inFile
durationInSec = duration(video_file_path)
videoBitrateInKbPerSec = argv.overhead/100 * (argv.targetSize * 1000 * 8)/durationInSec - argv.audioBitrate
videoBitrateInKbPerSec = int(min(argv.maxVidBitrate, videoBitrateInKbPerSec))
pixelSize = getPixelSize(videoBitrateInKbPerSec)
if(argv.flagToGet == "pixelSize"):
print(pixelSize)
if(argv.flagToGet == "videoBitrate"):
print(videoBitrateInKbPerSec)