-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Flowtter/ffmpeg
Ffmpeg
- Loading branch information
Showing
3 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ ffmpeg-python==0.2.0 | |
yapf==0.32.0 | ||
mypy==0.961 | ||
pylint==2.14.3 | ||
|
||
# Tests | ||
pytube==12.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import os | ||
import cv2 | ||
from pytube import YouTube | ||
from ffmpeg_utils import scale_video, _split_video | ||
|
||
|
||
def test_basic() -> None: | ||
os.mkdir('./test_mp4') | ||
yt = YouTube('https://www.youtube.com/watch?v=6A-hTKYBkC4') | ||
yt.streams.order_by('resolution').desc().first().download( | ||
filename='./test_mp4/test_basic.mp4') | ||
vid = cv2.VideoCapture('./test_mp4/test_basic.mp4') | ||
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH) | ||
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) | ||
|
||
os.remove('./test_mp4/test_basic.mp4') | ||
assert width == 1920 and height == 1080 | ||
|
||
|
||
def test_upscale() -> None: | ||
yt = YouTube('https://www.youtube.com/watch?v=6A-hTKYBkC4') | ||
yt.streams.order_by('resolution').asc().first().download( | ||
filename='./test_mp4/test_upscale.mp4') | ||
|
||
scale_video('./test_mp4/test_upscale.mp4') | ||
vid = cv2.VideoCapture('./test_mp4/test_upscale.mp4') | ||
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH) | ||
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) | ||
|
||
os.remove('./test_mp4/test_upscale.mp4') | ||
assert width == 1920 and height == 1080 | ||
|
||
|
||
def test_downscale() -> None: | ||
yt = YouTube('https://www.youtube.com/watch?v=wZI9is9Ix90') | ||
yt.streams.order_by('resolution').desc().first().download( | ||
filename='./test_mp4/test_downscale.mp4') | ||
|
||
scale_video('./test_mp4/test_downscale.mp4') | ||
vid = cv2.VideoCapture('./test_mp4/test_downscale.mp4') | ||
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH) | ||
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) | ||
|
||
os.remove('./test_mp4/test_downscale.mp4') | ||
os.rmdir('./test_mp4') | ||
assert width == 1920 and height == 1080 | ||
|
||
|
||
def abort(video_path: str) -> None: | ||
top = os.path.split(video_path)[0] | ||
print(top) | ||
for root, dirs, files in os.walk(top, topdown=False): | ||
for name in files: | ||
os.remove(os.path.join(root, name)) | ||
for name in dirs: | ||
os.rmdir(os.path.join(root, name)) | ||
os.rmdir(top) | ||
|
||
|
||
def split_check(video_path: str, frames: int) -> bool: | ||
drive = os.path.split(video_path)[0] | ||
for f in os.listdir(): | ||
path = os.path.join(drive, f) | ||
if os.path.isfile(os.path.join(drive, f)): | ||
vid = cv2.VideoCapture(path) | ||
fps = vid.get(cv2.CAP_PROP_FPS) | ||
frame_count = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) | ||
duration = frame_count // fps | ||
|
||
print(duration) | ||
if duration != frames: | ||
abort(video_path) | ||
return False | ||
return True | ||
|
||
|
||
def cut_1_100(video_path: str) -> bool: | ||
_split_video(video_path, [(0, 100)]) | ||
return split_check(video_path, 100) | ||
|
||
|
||
def cut_10_100(video_path: str) -> bool: | ||
_split_video(video_path, [(0, 100), (100, 200), (200, 300), (300, 400), | ||
(400, 500), (500, 600), (600, 700), (700, 800), | ||
(800, 900), (900, 1000)]) | ||
return split_check(video_path, 100) | ||
|
||
|
||
def test_split() -> None: | ||
os.mkdir('test_split') | ||
yt = YouTube('https://www.youtube.com/watch?v=6A-hTKYBkC4') | ||
yt.streams.order_by('resolution').desc().first().download( | ||
filename='./test_split/test_basic.mp4') | ||
|
||
assert cut_1_100('./test_split/test_basic.mp4') | ||
|
||
yt.streams.order_by('resolution').desc().first().download( | ||
filename='./test_split/test_basic.mp4') | ||
assert cut_10_100('./test_split/test_basic.mp4') | ||
|
||
abort('./test_split/test_basic.mp4') |