Skip to content

Commit

Permalink
Merge branch 'audio-diet'
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierOramas committed Sep 14, 2020
2 parents 2cc4e81 + aec2b68 commit 852541f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
.todo
*.mp4
*.egg-info
*.mp3
*.w4a
*.aac
*.vscode
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg?label=license)](https://www.gnu.org/licenses/gpl-3.0) [![Last commit](https://img.shields.io/github/last-commit/hiancdtrsnm/video-diet.svg?style=flat)](https://github.com/hiancdtrsnm/video-diet/commits) [![GitHub commit activity](https://img.shields.io/github/commit-activity/m/hiancdtrsnm/video-diet)](https://github.com/hiancdtrsnm/video-diet/commits) [![Github Stars](https://img.shields.io/github/stars/hiancdtrsnm/video-diet?style=flat&logo=github)](https://github.com/hiancdtrsnm/video-diet) [![Github Forks](https://img.shields.io/github/forks/hiancdtrsnm/video-diet?style=flat&logo=github)](https://github.com/hiancdtrsnm/video-diet) [![Github Watchers](https://img.shields.io/github/watchers/hiancdtrsnm/video-diet?style=flat&logo=github)](https://github.com/hiancdtrsnm/video-diet) [![GitHub contributors](https://img.shields.io/github/contributors/hiancdtrsnm/video-diet)](https://github.com/hiancdtrsnm/video-diet/graphs/contributors)

This project aims to reduce the spaces of your videos encoding it on `hevc`.
This project aims to reduce the spaces of your videos and audios encoding it on `hevc`.

## Why video-diet?
The answer is easy. I have a lot old-movies/videos taking a lot of space in the hard-drive.
The answer is easy. I have a lot old-movies/videos and music/audios taking a lot of space in the hard-drive.
So I'm always short on disk space, the by accident discover de `hevc` codec. when i need to shrink a video of `3GB`
to upload it to `Telegram`, the convertion take my 3GB movie and returned a 300 MB with the same quality 😱. So I
decided that I would convert all my video files, but they are a lot, so I build this tool for it.
decided that I would convert all my video and audio files , but they are a lot, so I build this tool for it.

More info about `hevc`:

Expand Down
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.7'


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)
62 changes: 41 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, get_codec
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,10 +39,11 @@ def folder(path: Path = typer.Argument(
resolve_path=True
)):
"""
Convert all videos in a folder
Convert all videos and audios in a folder
"""

videos = []
audios = []

for dir, folders, files in os.walk(path):
base_dir = Path(dir)
Expand All @@ -51,30 +52,29 @@ def folder(path: Path = typer.Argument(
file_path = base_dir / item
guess = filetype.guess(str(file_path))

if guess and 'video' in guess.mime:

ignored_by_extension = ignore_extension is not None \
and str(file_path).lower().endswith(ignore_extension)

ignored_by_path = ignore_path is not None \
and str(ignore_path) in str(file_path)

if ignored_by_extension or ignored_by_path:
typer.secho(f'Ignoring: {file_path}')
continue

if check_ignore(file, ignore_extension, ignore_path):
continue

if guess and 'video' in guess.mime :

videos.append(file_path)


if guess and 'audio' in guess.mime:

audios.append(file_path)

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 @@ -85,6 +85,25 @@ def folder(path: Path = typer.Argument(

pbar.update()

for audio in audios:
typer.secho(f'Processing: {audio}')
if get_codec(str(audio)) != 'hevc':

new_path = convertion_path(audio, True)

try:
convert_file(str(audio),str(new_path))
os.remove(str(audio))
if audio.suffix == new_path.suffix:
shutil.move(new_path, str(audio))

except ffmpeg._run.Error:
typer.secho(f'ffmpeg could not process this file: {str(audio)}', fg=RED)
errors_files.append(video)


pbar.update()

if errors_files:
typer.secho('This videos could not be processed:', fg=RED)
typer.secho(str(errors_files), fg=RED)
Expand All @@ -104,7 +123,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

conv_path = convertion_path(path)
Expand All @@ -114,12 +133,13 @@ def file(path: Path = typer.Argument(
please delete it', fg=RED)
return


if get_codec(str(path)) == 'hevc':
typer.secho('This video codec is already \'hevc\'', fg=GREEN)
return

try:
convert_video(str(path), str(conv_path))
convert_file(str(path), str(conv_path))

except FileNotFoundError as error:
if error.filename == 'ffmpeg':
Expand Down
43 changes: 35 additions & 8 deletions video_diet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,46 @@ def get_codec(path: str):
metadata = FFProbe(path)
except:
return None
return metadata.video[0].codec_name


def convertion_path(path: Path):

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


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

return metadata.audio[0].codec_name


def convertion_path(path: Path, audio: bool ):

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)



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):

ignored_by_extension = ignore_extension is not None \
and str(file_path).lower().endswith(ignore_extension)
ignored_by_path = ignore_path is not None \
and str(ignore_path) in str(file_path)

if ignored_by_extension or ignored_by_path:
typer.secho(f'Ignoring: {file_path}')
return True

return False

0 comments on commit 852541f

Please sign in to comment.