-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressionAssignment.py
More file actions
51 lines (41 loc) · 2.21 KB
/
CompressionAssignment.py
File metadata and controls
51 lines (41 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import subprocess
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as ndi
from ffmpeg_quality_metrics import FfmpegQualityMetrics
bit1280 = [512, 1024, 2048, 3072]
bit640 = [96, 128, 256,384, 612, 1024, 2048]
bit320 = [64, 96, 128, 256, 512, 1024]
bitrates = [bit320, bit640, bit1280]
resolutionNames = ["320x138","640x274","1280x548"]
def convertToMP4Lossy(resolution: str, rate: int):
subprocess.run(f'''ffmpeg -s {resolution} -i SourceVideo\\dancing{resolution}.yuv -b:v {rate}k
-c:v libx264 -preset slow OutputVideo\\compressed_{resolution}_{rate}.mp4''',shell = True)
def convertMP4ToYUVLossless(resolution: str, rate: int):
subprocess.run(f'''ffmpeg -i OutputVideo\\compressed_{resolution}_{rate}.mp4 -c:v libx264 OutputVideo\\compressed_{resolution}_{rate}.yuv''',shell=True)
def upscale(resolution: str, UpDimensions: str, rate: int):
subprocess.run(f'''ffmpeg -i OutputVideo\\compressed_{resolution}_{rate}.yuv -vf scale={UpDimensions}:flags=lanczos
-c:v libx264 -preset slow -crf 21 OutputVideo\\upscaled_{resolution}_{rate}_{UpDimensions}.yuv''',shell=True)
def convertToMP4Lossless(resolution: str):
subprocess.run(f'''ffmpeg -s {resolution} -i SourceVideo\\dancing{resolution}.yuv SourceVideo\\compressed_lossless.mp4''',shell = True)
def runProcess(resolution, rates):
for _rate in rates:
print(_rate)
convertToMP4Lossy(resolution,_rate)
convertMP4ToYUVLossless(resolution,_rate)
upscale(resolution, "1280x548",_rate)
ffqm = FfmpegQualityMetrics(f"OutputVideo\\upscaled_{resolution}_{_rate}_1280x548.yuv ", "SourceVideo\\compressed_lossless.mp4")
metrics = ffqm.calculate(["psnr"])
#print(ffqm.get_global_stats()["ssim"]["ssim_y"]["average"])
print(ffqm.get_global_stats()["psnr"]["psnr_avg"]["average"])
psnrs.append(ffqm.get_global_stats()["psnr"]["psnr_avg"]["average"])
convertToMP4Lossless("1280x548")
for index in range(len(bitrates)):
psnrs = []
runProcess(resolutionNames[index], bitrates[index])
plt.plot(bitrates[index], psnrs, label = resolutionNames[index], marker = 'o')
plt.title('Bitrate against PSNR')
plt.xlabel('Bitrate')
plt.ylabel('PSNR')
plt.legend()
plt.show()