Skip to content

Commit

Permalink
add youtube-dl
Browse files Browse the repository at this point in the history
now it's an all-in-one-solution
  • Loading branch information
sturmen committed Mar 2, 2019
1 parent c601708 commit 3b47fb8
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions double_speed_HEVC.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import ffmpeg
import sys
import os
import youtube_dl

MAX_HEIGHT = 1440
MAX_WIDTH = 2960
FILE_NAME_TEMPLATE = "%(uploader)s - %(title)s - %(id)s"

def get_height(filename):
probe = ffmpeg.probe(filename)
Expand All @@ -21,25 +23,43 @@ def get_frame_rate(filename):
return float(fps)

def get_crf(height):
if (height >= 1440):
if (height > 1080):
return 23
elif (height >= 1080):
elif (height > 720):
return 25
else:
return 28

def add_file_name(extracted_info):
constructed_file_name = FILE_NAME_TEMPLATE % {"uploader": extracted_info["uploader"], "title": extracted_info["title"], "id": extracted_info["id"]}
constructed_file_name += ".mkv"
print(constructed_file_name)
return constructed_file_name

def main():
in_file_name = sys.argv[1]
new_name = os.path.splitext(in_file_name)[0] + "2XHEVC.mp4"
new_height = get_height(in_file_name)
downloaded_videos = []
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': FILE_NAME_TEMPLATE,
'merge_output_format': 'mkv'
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
for url in sys.argv[1:]:
extracted_info = ydl.extract_info(url)
downloaded_videos.append(add_file_name(extracted_info))

for in_file_name in downloaded_videos:
new_name = os.path.splitext(in_file_name)[0] + " [2XHEVC].mp4"
new_height = get_height(in_file_name)

inputObject = ffmpeg.input(in_file_name)
v1 = inputObject['v'].setpts("0.5*PTS")
if (new_height > MAX_HEIGHT):
v1 = v1.filter('scale', -1, MAX_HEIGHT, force_original_aspect_ratio='decrease')
a1 = inputObject['a'].filter('atempo', 2.0)
inputObject = ffmpeg.input(in_file_name)
v1 = inputObject['v'].setpts("0.5*PTS")
if (new_height > MAX_HEIGHT):
v1 = v1.filter('scale', -1, MAX_HEIGHT, force_original_aspect_ratio='decrease')
a1 = inputObject['a'].filter('atempo', 2.0)

ffmpeg.output(v1, a1, new_name, format='mp4', pix_fmt='yuv420p', vcodec='libx265', preset='slow', crf=get_crf(new_height), acodec='aac', r=(2.0*get_frame_rate(in_file_name))).run(overwrite_output=True)
ffmpeg.output(v1, a1, new_name, format='mp4', pix_fmt='yuv420p', vcodec='libx265', preset='slow', crf=get_crf(new_height), acodec='aac', r=(2.0*get_frame_rate(in_file_name))).run(overwrite_output=True)

if __name__== "__main__":
main()

0 comments on commit 3b47fb8

Please sign in to comment.