Skip to content

Commit

Permalink
integrating audio and video functions in one
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierOramas committed Sep 14, 2020
1 parent 82407c3 commit 4d94dc4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
*.pyc
.todo
*.mp4
*.egg-info
*.egg-info
*.mp3
*.w4a
*.aac
2 changes: 1 addition & 1 deletion video_diet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__version__ = '0.1.0'


def convert_video(source: str, dest: str):
def convert_file(source: str, dest: str):
stream = ffmpeg.input(source)
stream = ffmpeg.output(stream, dest, vcodec='libx265', crf='28')
ffmpeg.run(stream)
37 changes: 16 additions & 21 deletions video_diet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import enlighten
import ffmpeg

from .utils import convertion_path,convertion_path_audio, get_codec, get_codec_audio
from . import convert_video
from .utils import convertion_path, get_codec, check_ignore
from . import convert_file

app = typer.Typer()

Expand Down Expand Up @@ -39,7 +39,7 @@ def folder(path: Path = typer.Argument(
resolve_path=True
)):
"""
Convert all videos in a folder
Convert all videos and audios in a folder
"""

videos = []
Expand All @@ -52,32 +52,27 @@ def folder(path: Path = typer.Argument(
file = base_dir / file
guess = filetype.guess(str(file))

if guess and 'video' in guess.mime:

if (not (ignore_extension == None) and str(file).lower().endswith(ignore_extension)) or (not (ignore_path == None) and str(ignore_path) in str(file)) :
typer.secho(f'ignoring: {file}')
continue
if check_ignore(file, ignore_extension, ignore_path):
continue

if guess and 'video' in guess.mime :

videos.append(file)

if guess and 'audio' in guess.mime:

if (not (ignore_extension == None) and str(file).lower().endswith(ignore_extension)) or (not (ignore_path == None) and str(ignore_path) in str(file)) :
typer.secho(f'ignoring: {file}')
continue

audios.append(file)

manager = enlighten.get_manager()
errors_files = []
pbar = manager.counter(total=len(videos), desc='Video', unit='videos')
pbar = manager.counter(total=len(videos)+len(audios), desc='Files', unit='files')
for video in videos:
typer.secho(f'Processing: {video}')
if get_codec(str(video)) != 'hevc':
new_path = convertion_path(video)
new_path = convertion_path(video, False)

try:
convert_video(str(video),str(new_path))
convert_file(str(video),str(new_path))
os.remove(str(video))
if video.suffix == new_path.suffix:
shutil.move(new_path, str(video))
Expand All @@ -91,11 +86,11 @@ def folder(path: Path = typer.Argument(

for audio in audios:
typer.secho(f'Processing: {audio}')
if get_codec_audio(str(audio)) != 'hevc':
new_path = convertion_path_audio(audio)
if get_codec(str(audio)) != 'hevc':
new_path = convertion_path(audio, True)

try:
convert_video(str(audio),str(new_path))
convert_file(str(audio),str(new_path))
os.remove(str(audio))
if audio.suffix == new_path.suffix:
shutil.move(new_path, str(audio))
Expand All @@ -109,7 +104,7 @@ def folder(path: Path = typer.Argument(


if errors_files:
typer.secho(f'This videos could not be processed : {str(errors_files)}', fg=RED)
typer.secho(f'This videos and audios could not be processed : {str(errors_files)}', fg=RED)


@app.command()
Expand All @@ -126,7 +121,7 @@ def file(path: Path = typer.Argument(
"""

if path is None:
typer.secho('Please write the video path', fg=RED)
typer.secho('Please write the video or audio path', fg=RED)
return


Expand All @@ -136,4 +131,4 @@ def file(path: Path = typer.Argument(
typer.secho('The destination file already exist, please delete it', fg=RED)
return

convert_video(str(path),str(conv_path))
convert_file(str(path),str(conv_path))
46 changes: 25 additions & 21 deletions video_diet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,24 @@ def get_codec(path: str):
metadata = FFProbe(path)
except:
return None
return metadata.video[0].codec_name

def get_codec_audio(path: str):
try:
metadata = FFProbe(path)
except:
return None
return metadata.audio[0].codec_name


def convertion_path(path: Path):

if path.suffix.lower() not in ['.mkv', '.mp4']:
return path.parent / (path.stem + '.mkv')

return path.parent / ('conv-' + path.name)

def convertion_path_audio(path: Path):

if path.suffix.lower() not in ['.aac', '.m4a']:
return path.parent / (path.stem + '.aac')

if 'video' in metadata.streams:
return metadata.video[0].codec_name

return metadata.audio[0].codec_name


def convertion_path(path: Path, audio:bool ):
print(audio)
if not audio:
if path.suffix.lower() not in ['.mkv', '.mp4']:

return path.parent / (path.stem + '.mkv')
else:
if path.suffix.lower() not in ['.aac', '.m4a']:

return path.parent / (path.stem + '.aac')

return path.parent / ('conv-' + path.name)


Expand All @@ -40,3 +36,11 @@ def check_if_video(path: str):
guess = filetype.guess(path)

return guess and 'video' in guess

def check_ignore(file, ignore_extension:str, ignore_path:str):

if (not (ignore_extension == None) and str(file).lower().endswith(ignore_extension)) or (not (ignore_path == None) and str(ignore_path) in str(file)) :
typer.secho(f'ignoring: {file}')
return True

return False

0 comments on commit 4d94dc4

Please sign in to comment.