Created
December 30, 2015 04:50
-
-
Save lordsutch/702c242c5a417e34fd5c to your computer and use it in GitHub Desktop.
Convert videos to Chromebook-compatible format, if necessary
This file contains hidden or 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
#!/usr/bin/env python3 | |
import sys | |
import subprocess | |
import json | |
import os | |
# Supported by Chrome OS according to https://www.chromium.org/audio-video | |
ACCEPTABLE_VIDEO=('theora', 'vp8', 'vp9', 'h264', 'mpeg4') | |
ACCEPTABLE_AUDIO=('aac', 'mp3', 'vorbis', 'opus', 'pcm_mulaw', 'pcm_f32le', | |
'pcm_s16le', 'pcm_u8', 'flac', 'amr_nb', 'amr_wb') | |
def check_codec(filename): | |
video_ok = audio_ok = False | |
output = subprocess.run(['ffprobe', '-i', filename, | |
'-of', 'json', '-show_streams'], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.DEVNULL) | |
dat = json.loads(output.stdout.decode('utf-8')) | |
for stream in dat.get('streams', []): | |
#print(repr(stream)) | |
if stream.get('codec_type', '') == 'video' and \ | |
stream.get('codec_name', '') in ACCEPTABLE_VIDEO: | |
video_ok = True | |
if stream.get('codec_type', '') == 'audio' and \ | |
stream.get('codec_name', '') in ACCEPTABLE_AUDIO: | |
audio_ok = True | |
return (video_ok, audio_ok) | |
for filename in sys.argv[1:]: | |
video_ok, audio_ok = check_codec(filename) | |
if (not video_ok) or (not audio_ok): | |
print(filename, 'needs reencoding') | |
basename, ext = os.path.splitext(filename) | |
acodec = 'copy' if audio_ok else 'libvo_aacenc' | |
if video_ok: | |
vparams = ['-codec:v', 'copy'] | |
else: | |
vparams = ['-codec:v', 'libx264', | |
'-preset', 'slower', | |
'-tune', 'film', | |
'-crf', '20', | |
'-profile:v', 'high', # TiVo compatibility | |
'-level', '4.1', # TiVo compatibility | |
'-movflags', '+faststart'] | |
ext = '.m4v' | |
outfile = basename+'-fixed'+ext | |
#print(outfile, acodec, vparams) | |
subprocess.run(['ffmpeg', '-threads', '0', '-i', filename] + | |
vparams + | |
['-codec:a', acodec, '-b:a', '192k', '-y', outfile]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment