video2x/ffmpeg.py

90 lines
2.9 KiB
Python
Raw Normal View History

2018-02-24 18:34:00 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Name: FFMPEG Class
Author: K4YT3X
Date Created: Feb 24, 2018
2018-11-03 01:49:19 +00:00
Last Modified: November 2, 2018
2018-02-24 18:34:00 +00:00
Description: This class handles all FFMPEG related
operations.
2018-11-03 01:49:19 +00:00
Version 2.0.5
2018-02-24 18:34:00 +00:00
"""
import subprocess
2018-02-24 18:34:00 +00:00
2018-10-22 19:00:09 +00:00
class Ffmpeg:
2018-02-25 03:52:04 +00:00
"""This class communicates with ffmpeg
This class deals with ffmpeg. It handles extracitng
frames, stripping audio, converting images into videos
and inserting audio tracks to videos.
"""
2018-02-24 18:34:00 +00:00
2018-11-03 01:49:19 +00:00
def __init__(self, ffmpeg_path, outfile, ffmpeg_arguments, hardware_acc=False):
self.ffmpeg_path = '{}ffmpeg.exe'.format(ffmpeg_path)
2018-02-24 21:13:27 +00:00
self.outfile = outfile
2018-11-03 01:49:19 +00:00
self.hardware_acc = hardware_acc
self.ffmpeg_arguments = ffmpeg_arguments
2018-02-24 18:34:00 +00:00
2018-02-25 03:52:04 +00:00
def extract_frames(self, videoin, outpath):
"""Extract every frame from original videos
This method extracts every frame from videoin
using ffmpeg
Arguments:
videoin {string} -- input video path
outpath {string} -- video output folder
"""
execute = '\"{}\" -i \"{}\" {}\\extracted_%0d.png -y {}'.format(
2018-11-03 01:49:19 +00:00
self.ffmpeg_path, videoin, outpath, ' '.join(self.ffmpeg_arguments))
print(execute)
subprocess.call(execute)
2018-02-24 18:34:00 +00:00
def extract_audio(self, videoin, outpath):
2018-02-25 03:52:04 +00:00
"""Strips audio tracks from videos
This method strips audio tracks from videos
into the output folder in aac format.
Arguments:
videoin {string} -- input video path
outpath {string} -- video output folder
"""
execute = '\"{}\" -i \"{}\" -vn -acodec copy {}\\output-audio.aac -y {}'.format(
2018-11-03 01:49:19 +00:00
self.ffmpeg_path, videoin, outpath, ' '.join(self.ffmpeg_arguments))
print(execute)
subprocess.call(execute)
2018-02-24 18:34:00 +00:00
def convert_video(self, framerate, resolution, upscaled, ):
2018-02-25 03:52:04 +00:00
"""Converts images into videos
This method converts a set of images into a
video.
Arguments:
framerate {float} -- target video framerate
resolution {string} -- target video resolution
upscaled {string} -- source images folder
2018-02-25 03:52:04 +00:00
"""
execute = '\"{}\" -r {} -f image2 -s {} -i {}\\extracted_%d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p {}\\no_audio.mp4 -y {}'.format(
2018-11-03 01:49:19 +00:00
self.ffmpeg_path, framerate, resolution, upscaled, upscaled, ' '.join(self.ffmpeg_arguments))
print(execute)
subprocess.call(execute)
2018-02-25 03:52:04 +00:00
def insert_audio_track(self, upscaled):
2018-02-25 03:52:04 +00:00
"""Insert audio into video
2018-05-19 22:24:31 +00:00
Inserts the WAV audio track stripped from
2018-02-25 03:52:04 +00:00
the original video into final video.
2018-02-24 18:34:00 +00:00
2018-02-25 03:52:04 +00:00
Arguments:
upscaled {string} -- upscaled image folder
2018-02-25 03:52:04 +00:00
"""
execute = '\"{}\" -i {}\\no_audio.mp4 -i {}\\output-audio.aac -shortest -codec copy {} -y {}'.format(
2018-11-03 01:49:19 +00:00
self.ffmpeg_path, upscaled, upscaled, self.outfile, ' '.join(self.ffmpeg_arguments))
print(execute)
subprocess.call(execute)