-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dan Menzies
committed
Mar 25, 2024
1 parent
226f572
commit fa53600
Showing
3 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# IDE and Editor files | ||
.vscode/ | ||
.idea/ | ||
*.sublime-workspace | ||
*.sublime-project | ||
*.swp | ||
*.swo | ||
*.swn | ||
*.swm | ||
*.swl | ||
*.swk | ||
*.sw* | ||
*~ | ||
.\#* | ||
\#*\# | ||
/.emacs.desktop | ||
/.emacs.desktop.lock | ||
.elc | ||
auto-save-list | ||
tramp | ||
.\#* | ||
.project | ||
.pydevproject | ||
.settings/ | ||
.loadpath | ||
.externalToolBuilders/ | ||
*.launch | ||
.cproject | ||
.classpath | ||
.factorypath | ||
.buildpath | ||
.target | ||
.tern-project | ||
.texlipse | ||
.springBeans | ||
.recommenders/ | ||
.apt_generated/ | ||
.apt_generated_tests/ | ||
.cache-main | ||
.scala_dependencies | ||
.worksheet | ||
.sts4-cache/ | ||
.flattened-pom.xml | ||
.gradletasknamecache | ||
.m2/ | ||
.recommenders/ | ||
.idea_modules/ | ||
*.iml | ||
*.ipr | ||
cmake-build-*/ | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
.vscode/ | ||
.history/ | ||
|
||
# Python | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
*.so | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
*.manifest | ||
*.spec | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
*.mo | ||
*.pot | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
instance/ | ||
.webassets-cache | ||
.scrapy | ||
docs/_build/ | ||
.pybuilder/ | ||
target/ | ||
.ipynb_checkpoints | ||
profile_default/ | ||
ipython_config.py | ||
__pypackages__/ | ||
celerybeat-schedule | ||
celerybeat.pid | ||
*.sage.py | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
.spyderproject | ||
.spyproject | ||
.ropeproject | ||
/site | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
.pyre/ | ||
.pytype/ | ||
cython_debug/ | ||
|
||
# Converted video files | ||
converted/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Video Conversion Script for Upload | ||
|
||
This Python script is designed to convert video files in the current directory to formats suitable for uploading to various platforms. It provides options to adjust the resolution, audio settings, and bitrate of the output videos. | ||
|
||
## Features | ||
|
||
- Converts all `.mp4` files in the current directory to the specified format | ||
- Supports optional low resolution output (720p) | ||
- Allows encoding without audio | ||
- Provides an option for low bitrate audio | ||
- Offers a high video bitrate setting for YouTube uploads | ||
- Resizes the video to the specified resolution and sets the frame rate to 30 fps | ||
- Saves the converted files in a `converted` directory | ||
|
||
## Requirements | ||
|
||
- Python 3.x | ||
- MoviePy library | ||
|
||
## Installation | ||
|
||
1. Clone the repository or download the script file. | ||
2. Install the required dependencies by running the following command: | ||
``` | ||
pip install moviepy | ||
``` | ||
|
||
|
||
Sure! Here's a brief installation guide for FFmpeg that you can add to the README: | ||
|
||
### FFmpeg Installation | ||
|
||
The script relies on FFmpeg for video processing. Make sure you have FFmpeg installed on your system before running the script. | ||
|
||
#### Windows | ||
|
||
1. Download the latest FFmpeg binaries from the [official website](https://ffmpeg.org/download.html#build-windows). | ||
2. Extract the downloaded archive to a directory of your choice. | ||
3. Add the `bin` directory of the extracted FFmpeg directory to your system's PATH environment variable. | ||
|
||
#### macOS | ||
|
||
1. Install FFmpeg using Homebrew by running the following command in the terminal: | ||
``` | ||
brew install ffmpeg | ||
``` | ||
|
||
#### Linux | ||
|
||
1. Install FFmpeg using your distribution's package manager. For example: | ||
- Ubuntu or Debian: | ||
``` | ||
sudo apt-get update | ||
sudo apt-get install ffmpeg | ||
``` | ||
- Fedora or CentOS: | ||
``` | ||
sudo dnf install ffmpeg | ||
``` | ||
- Arch Linux: | ||
``` | ||
sudo pacman -S ffmpeg | ||
``` | ||
## Usage | ||
To use the script, open a terminal or command prompt and navigate to the directory containing the script and the video files you want to convert. Then, run the script with the desired options: | ||
``` | ||
python convert_for_upload.py [--lowres] [--noaudio] [--lowaudio] [--yt] | ||
``` | ||
### Options | ||
- `--lowres`: Enable low resolution output (720p). | ||
- `--noaudio`: Encode without audio, only video. | ||
- `--lowaudio`: Use a low bitrate for audio. | ||
- `--yt`: Use a high video bitrate, ready for YouTube. | ||
## Examples | ||
1. Convert videos to low resolution (720p): | ||
``` | ||
python convert_for_upload.py --lowres | ||
``` | ||
2. Convert videos without audio: | ||
``` | ||
python convert_for_upload.py --noaudio | ||
``` | ||
3. Convert videos with low bitrate audio: | ||
``` | ||
python convert_for_upload.py --lowaudio | ||
``` | ||
4. Convert videos with high video bitrate for YouTube: | ||
``` | ||
python convert_for_upload.py --yt | ||
``` | ||
## Output | ||
The converted video files will be saved in the `converted` directory within the current directory. The script will create the `converted` directory if it doesn't already exist. | ||
The output file names will be modified based on the selected options: | ||
- `--lowres`: The file name will have ` - low res` appended before the `.mp4` extension. | ||
- `--noaudio`: The file name will have ` - no audio` appended before the `.mp4` extension. | ||
- `--lowaudio`: The file name will have ` - low bitrate audio` appended before the `.mp4` extension. | ||
- `--yt`: The file name will have ` - yt` appended before the `.mp4` extension. | ||
## Contributing | ||
I welcome contributions to enhance the functionality and usability of this script. If you have any ideas, suggestions, or bug reports, please feel free to open an issue or submit a pull request on the GitHub repository. | ||
I appreciate your feedback and look forward to collaborating with the community to make this script even better! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import os | ||
import argparse | ||
from moviepy.editor import VideoFileClip | ||
|
||
def convert_for_upload(lowres, noaudio, lowaudio, yt): | ||
# Source and destination directories | ||
source_dir = './' | ||
dest_dir = './converted' | ||
|
||
# Check if destination directory exists, if not, create it | ||
if not os.path.exists(dest_dir): | ||
os.makedirs(dest_dir) | ||
|
||
# Resolution settings based on lowres flag | ||
resolution = 720 if lowres else 1080 | ||
video_bitrate = "1000k" if lowres else "2000k" | ||
video_bitrate = video_bitrate if not yt else '8000k' | ||
|
||
# Audio settings based on noaudio flag | ||
audio_codec = 'aac' if not noaudio else None | ||
audio_bitrate = '320k' if not lowaudio else '192k' | ||
audio_bitrate = audio_bitrate if not noaudio else '0k' | ||
|
||
print("Building clips") | ||
print(f'resolution: {resolution}') | ||
print(f'video_bitrate: {video_bitrate}') | ||
print(f'audio_codec: {audio_codec}') | ||
print(f'audio_bitrate: {audio_bitrate}') | ||
print(f'--------------------------------------') | ||
|
||
# Iterate through each file in the source directory | ||
for filename in os.listdir(source_dir): | ||
# Check if file is an mp4 | ||
if filename.endswith('.mp4'): | ||
# Create video clip | ||
clip = VideoFileClip(os.path.join(source_dir, filename)) | ||
|
||
# Conditionally modify the clip for no audio | ||
if noaudio: | ||
clip = clip.without_audio() | ||
|
||
# Resize the clip based on the lowres flag and set frame rate to 30 fps | ||
resized_clip = clip.resize(height=resolution).set_fps(30) | ||
|
||
# Tweak the name of the file if needed | ||
destination_filename = filename | ||
|
||
if yt: | ||
destination_filename = filename.replace('.mp4', ' - yt.mp4') | ||
|
||
elif lowres: | ||
destination_filename = filename.replace('.mp4', ' - low res.mp4') | ||
|
||
elif lowaudio: | ||
destination_filename = filename.replace('.mp4', ' - low bitrate audio.mp4') | ||
|
||
elif noaudio: | ||
destination_filename = filename.replace('.mp4', ' - no audio.mp4') | ||
|
||
# Write to file with specified settings, adjusting for audio | ||
resized_clip.write_videofile( | ||
os.path.join(dest_dir, destination_filename), | ||
audio_codec=audio_codec, | ||
audio_bitrate=audio_bitrate, | ||
bitrate=video_bitrate | ||
) | ||
|
||
def main(): | ||
# Setup argparse for command line flags | ||
parser = argparse.ArgumentParser(description='Convert videos for upload, with optional settings for low resolution and no audio.') | ||
parser.add_argument('--lowres', action='store_true', help='Enable low resolution output.') | ||
parser.add_argument('--noaudio', action='store_true', help='Encode without audio, only video.') | ||
parser.add_argument('--lowaudio', action='store_true', help='Use a low bitrate for audio.') | ||
parser.add_argument('--yt', action='store_true', help='Use a high video bit rate, ready for YouTube.') | ||
args = parser.parse_args() | ||
|
||
# Call the conversion function with the flags | ||
convert_for_upload(args.lowres, args.noaudio, args.lowaudio, args.yt) | ||
|
||
if __name__ == '__main__': | ||
main() |