added the feature to migrate audio tracks and subtitles

This commit is contained in:
K4YT3X 2019-02-21 12:26:05 -05:00
parent 054ca44641
commit 150ab58b40

View File

@ -4,13 +4,12 @@
Name: FFMPEG Class Name: FFMPEG Class
Author: K4YT3X Author: K4YT3X
Date Created: Feb 24, 2018 Date Created: Feb 24, 2018
Last Modified: February 1, 2019 Last Modified: February 21, 2019
Description: This class handles all FFMPEG related Description: This class handles all FFMPEG related
operations. operations.
Version 2.0.6
""" """
import json
import subprocess import subprocess
@ -22,43 +21,43 @@ class Ffmpeg:
and inserting audio tracks to videos. and inserting audio tracks to videos.
""" """
def __init__(self, ffmpeg_path, outfile, ffmpeg_arguments, hardware_acc=False): def __init__(self, ffmpeg_path, ffmpeg_arguments, hardware_acc=False):
self.ffmpeg_path = '{}ffmpeg.exe'.format(ffmpeg_path) self.ffmpeg_path = ffmpeg_path
self.outfile = outfile self.ffmpeg_binary = '\"{}ffmpeg.exe\"'.format(ffmpeg_path)
self.hardware_acc = hardware_acc self.hardware_acc = hardware_acc
self.ffmpeg_arguments = ffmpeg_arguments self.ffmpeg_arguments = ffmpeg_arguments
def extract_frames(self, videoin, outpath): def get_video_info(self, input_video):
""" Gets input video information
This method reads input video information
using ffprobe in dictionary.
Arguments:
input_video {string} -- input video file path
Returns:
dictionary -- JSON text of input video information
"""
json_str = subprocess.check_output('\"{}ffprobe.exe\" -v quiet -print_format json -show_format -show_streams \"{}\"'.format(self.ffmpeg_path, input_video))
return json.loads(json_str.decode('utf-8'))
def extract_frames(self, input_video, extracted_frames):
"""Extract every frame from original videos """Extract every frame from original videos
This method extracts every frame from videoin This method extracts every frame from videoin
using ffmpeg using ffmpeg
Arguments: Arguments:
videoin {string} -- input video path input_video {string} -- input video path
outpath {string} -- video output folder extracted_frames {string} -- video output folder
""" """
execute = '\"{}\" -i \"{}\" {}\\extracted_%0d.png -y {}'.format( execute = '{} -i \"{}\" \"{}\"\\extracted_%0d.png -y {}'.format(
self.ffmpeg_path, videoin, outpath, ' '.join(self.ffmpeg_arguments)) self.ffmpeg_binary, input_video, extracted_frames, ' '.join(self.ffmpeg_arguments))
print(execute) print('Executing: {}'.format(execute))
subprocess.call(execute) subprocess.call(execute)
def extract_audio(self, videoin, outpath): def convert_video(self, framerate, resolution, upscaled_frames):
"""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 aac {}\\output-audio.aac -y {}'.format(
self.ffmpeg_path, videoin, outpath, ' '.join(self.ffmpeg_arguments))
print(execute)
subprocess.call(execute)
def convert_video(self, framerate, resolution, upscaled):
"""Converts images into videos """Converts images into videos
This method converts a set of images into a This method converts a set of images into a
@ -67,23 +66,22 @@ class Ffmpeg:
Arguments: Arguments:
framerate {float} -- target video framerate framerate {float} -- target video framerate
resolution {string} -- target video resolution resolution {string} -- target video resolution
upscaled {string} -- source images folder upscaled_frames {string} -- source images folder
""" """
execute = '\"{}\" -r {} -f image2 -s {} -i {}\\extracted_%d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p {}\\no_audio.mp4 -y {}'.format( execute = '{} -r {} -f image2 -s {} -i \"{}\"\\extracted_%d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p \"{}\"\\no_audio.mp4 -y {}'.format(
self.ffmpeg_path, framerate, resolution, upscaled, upscaled, ' '.join(self.ffmpeg_arguments)) self.ffmpeg_binary, framerate, resolution, upscaled_frames, upscaled_frames, ' '.join(self.ffmpeg_arguments))
print(execute) print('Executing: {}'.format(execute))
subprocess.call(execute) subprocess.call(execute)
def insert_audio_track(self, upscaled): def migrate_audio_tracks_subtitles(self, input_video, output_video, upscaled_frames):
"""Insert audio into video """ Migrates audio tracks and subtitles from input video to output video
Inserts the WAV audio track stripped from
the original video into final video.
Arguments: Arguments:
upscaled {string} -- upscaled image folder input_video {string} -- input video file path
output_video {string} -- output video file path
upscaled_frames {string} -- directory containing upscaled frames
""" """
execute = '\"{}\" -i {}\\no_audio.mp4 -i {}\\output-audio.aac -shortest -codec copy {} -y {}'.format( execute = '{} -i \"{}\"\\no_audio.mp4 -i \"{}\" -c:a copy -c:v copy -c:s copy -map 0:v? -map 1:a? -map 1:s? \"{}\" -y {}'.format(
self.ffmpeg_path, upscaled, upscaled, self.outfile, ' '.join(self.ffmpeg_arguments)) self.ffmpeg_binary, upscaled_frames, input_video, output_video, ' '.join(self.ffmpeg_arguments))
print(execute) print('Executing: {}'.format(execute))
subprocess.call(execute) subprocess.call(execute)