Skip to main content

A Python library for video style transfer and utility functions

Project description

bgral

PyPI - Version Status - Alpha License - MIT

bgral is a Python library currently under active development. The PyPI package has been published to reserve the name.

At the moment, it contains only a couple of basic utility functions — but more features are on the way!


✨ Features (So Far)

made_by()

Returns the author's name.

from bgral.core import made_by

print(made_by())  # Output: Bhavya Gujral

🎨 Video Style Transfer

This project applies neural style transfer on videos by extracting individual frames, applying style transfer to each frame, and then reassembling the frames into a stylized video with the original audio.

🚀 Features

  • Extracts frames from a video.
  • Applies style transfer on each frame using a pre-trained TensorFlow model.
  • Recompiles the stylized frames back into a video.
  • Adds original audio to the final video.
  • Supports both local (offline) and online (TensorFlow Hub) style transfer models.

🛠 Prerequisites

To run this project, you will need:

  • Python 3.x
  • TensorFlow
  • OpenCV
  • TensorFlow Hub
  • PIL (Python Imaging Library)
  • ffmpeg (for adding audio back to the video)

📦 Install Dependencies

Install Python libraries:

pip install tensorflow opencv-python tensorflow-hub pillow

Install ffmpeg (Linux):

sudo apt install ffmpeg

🧪 Usage

Running the Script

To apply style transfer on a video, use the style_transfer() function:

from bgral.video_style_transfer import style_transfer

style_transfer(
    input_video="input.mp4",
    style_image_path="style.jpg",
    working_dir="output",
    fps=30
)

Arguments:

  • input_video (str): Path to the input video file.
  • style_image_path (str): Path to the image to be used for style transfer.
  • working_dir (str): Directory where the frames, styled frames, and final video will be saved.
  • fps (int): Frames per second for the output video (default is 30).

How It Works

  1. Extract Frames: The video is broken down into individual frames.
  2. Apply Style Transfer: The style image is applied to each frame using a pre-trained model from TensorFlow Hub.
  3. Recompile Video: The stylized frames are then compiled into a new video.
  4. Add Audio: The original audio from the input video is added back to the final stylized video.

Understanding FPS Calculation Based on Video Duration

In video processing, FPS (Frames Per Second) refers to the number of frames displayed or captured per second of video. To calculate the FPS of a video, you can use the formula:

fps = len(frame_paths) / video_duration_in_seconds

Where:

  • len(frame_paths) is the total number of frames in the video.
  • video_duration_in_seconds is the total duration of the video in seconds.

Example:

Let's say the video has a total duration of 11 seconds, and there are 250 frames extracted from the video. To calculate the FPS, we use the formula:

fps = 250 / 11

This results in:

fps ≈ 22.73

So, the video has approximately 22.73 FPS, meaning it displays about 22.73 frames per second. This FPS determines the smoothness of the video, with a higher FPS providing smoother video playback and a lower FPS resulting in choppier motion, especially during fast scenes.

Conclusion:

For this example, the FPS is approximately 22.73 FPS for a video that is 11 seconds long and has 250 frames. By using this simple formula, you can calculate the FPS for any video by knowing the total number of frames and the video duration.

🧪 Example

Run the following command to execute the style transfer process on a sample video:

python check.py

This will read the video test.mp4, apply the style from test.jpg, and save the output to the output/ folder.


📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bgral-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bgral-0.2.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file bgral-0.2.0.tar.gz.

File metadata

  • Download URL: bgral-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for bgral-0.2.0.tar.gz
Algorithm Hash digest
SHA256 241f2ec9c60502140898c29e87a6dc8a5d7a90b9b25be29d933e68e32d730f16
MD5 c6e9cef1289889c8828d0165fe5406fb
BLAKE2b-256 bef2b1bf96a1d74ca346bcd2964f0c3e2f8af97ce041bbbb3ddebac84da26779

See more details on using hashes here.

File details

Details for the file bgral-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: bgral-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for bgral-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c232b0afeafe50e2b3e9c43545d1ff21558e28d3f6ee8c0dd56959b5fdeba0c8
MD5 6ca6a4977fda7931521de7fc7dc88729
BLAKE2b-256 313238ca524079c22d75d1b68680702818fdf3696ac9cd0c7f1d85233ba639be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page