Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal]: -vcodec copy HLS #396

Open
4 tasks done
juniorcines opened this issue May 9, 2024 · 12 comments · Fixed by #401
Open
4 tasks done

[Proposal]: -vcodec copy HLS #396

juniorcines opened this issue May 9, 2024 · 12 comments · Fixed by #401
Assignees
Labels
ENHANCEMENT ⚡ New Feature/Addition/Improvement PROPOSAL 📩 A proposal/proposition WAITING TO TEST ⏲️ Asked user to test the suggested example/binary/solution WORK IN PROGRESS 🚧 currently been worked on.
Milestone

Comments

@juniorcines
Copy link

Issue guidelines

Issue Checklist

  • I have searched open or closed issues and found nothing related to my idea.
  • I have read the Documentation and it doesn't mention anything about my idea.
  • To my best knowledge, my idea wouldn't break something for other users.

Describe your Idea

Hello, I would like the option that if the original quality is used, one can specify if they want to do:

-vcodec: copy

I say this because most of us could have the video in H264 and we just want to use the original quality and have it in HLS.

So with this option it would be faster to convert the videos to HLS.

Use Cases

-vcodec: copy

transfer the video to HLS faster if the original quality is used without the option of multiple qualities

Any other Relevant Information?

No response

@juniorcines juniorcines added the PROPOSAL 📩 A proposal/proposition label May 9, 2024
Copy link

welcome bot commented May 9, 2024

Thanks for opening this issue, a maintainer will get back to you shortly!

In the meantime:

  • Read our Issue Guidelines, and update your issue accordingly. Please note that your issue will be fixed much faster if you spend about half an hour preparing it, including the exact reproduction steps and a demo.
  • Go comprehensively through our dedicated FAQ & Troubleshooting section.
  • For any quick questions and typos, please refrain from opening an issue, as you can reach us on Gitter community channel.

@abhiTronix
Copy link
Owner

abhiTronix commented May 20, 2024

@juniorcines Thank you for the suggestion. Adding an option to copy the existing video codec (vcodec copy) when transcoding to HLS (or DASH) can be a useful optimization, especially when the input video is already encoded in a already compatible format like H.264. It's a great quality-of-life improvement that can make the transcoding workflow in StreamGear more efficient for many users. 👍🏼

@abhiTronix abhiTronix added this to To do in VidGear v0.3.3 via automation May 20, 2024
@abhiTronix abhiTronix added this to the v0.3.3 milestone May 20, 2024
@abhiTronix abhiTronix added ENHANCEMENT ⚡ New Feature/Addition/Improvement WORK IN PROGRESS 🚧 currently been worked on. labels May 20, 2024
@juniorcines
Copy link
Author

Thanks to you, and if when we only want to use the default quality to pass it to HLS, if it is already in H264, using copy is good to pass it quickly without consuming a lot of resources.

I use it in Nodejs, but I would like to have this in Python with your library.

abhiTronix added a commit that referenced this issue May 30, 2024
💬 When the output codec is set to "copy" (stream copy mode), certain video
processing parameters like "-vf" (video filters) and "-aspect" (aspect ratio)
are not supported and can lead to errors and invalid output files.

♻️ This commit refactors the internal `PreProcess` method in StreamGear API to handle
the stream copy mode correctly:

- 🥅 Moved the existing code for setting "-vf" and "-aspect" inside conditional block that checks if
  the output stream codec is not "copy".
- 🔊 Added an else block to log warnings and discard "-vf" and "-aspect" in stream copy mode.
@abhiTronix abhiTronix self-assigned this May 31, 2024
abhiTronix added a commit that referenced this issue Jun 7, 2024
…PI [#401]

💬 This commit includes significant refactoring, documentation updates, and enhancements across StreamGear, WriteGear, and NetGear APIs. It introduces safer process termination methods, improved parameter handling, and enhanced documentation for better user guidance. Additionally, it addresses several bugs and improves the overall maintainability and clarity of the codebase.

- Refactored stream copy handling to address errors with unsupported parameters (Fixes #396).
  - Moved settings for "-vf" and "-aspect" inside conditional blocks.
  - Added warnings and discarded these parameters in stream copy mode.
  - Fixed incompatibility of stream copy with Real-time Frames Mode.
    - Added warnings and discarded `-vcodec copy` when using this mode.
- Deprecated `terminate()` method, introducing `close()` for safer process termination.
  - Updated documentation and tests to reflect these changes.
- Introduced new `-enable_force_termination` attribute for immediate FFmpeg process termination.
- Restricted `-livestream` parameter to Real-time Frames Mode only.
  - Disabled live streaming for video files and updated relevant logging.
- Removed non-essential aspect ratio parameter to prevent unwanted distortions (Fixes #385).
  - Enhanced warning messages and clarified documentation.
- Updated `close()` methods for handling gracefully signal interruptions on different systems with device audio streams.
  - Adjusted signals for graceful termination based on the operating system.

#### WriteGear API:
- Simplified the logic for formatting output parameters.


#### NetGear API:
- Fixed Secure Mode failing to work on conflicting ZMQ Contexts:
  - Handled "Address in use" error more gracefully and disabled secure mode if errors occur.
  - Improved handling of ZMQ Authenticator and Certificates.
- Enhanced logging and error handling for secure mode.
  - Logged Authenticator start/stop events.
- Handled socket session expiration more gracefully in `recv_handler`.
- Fixed `msg_json` undefined when terminating context in the `recv_handler` method.
- Ensured proper termination of the ZMQ context and socket when closing the NetGear instance.
- Added Admonition for warning users about the Client's end must run before the Server's end to establish a secure connection in Secure Mode.

#### Helper Class:
- Added a custom deprecated decorator to mark deprecated functions and parameters.
  - It displays a warning message when a deprecated function or parameter is used.
- Updated `extract_time` helper function regex to handle milliseconds.

#### Setup
- Updated setup.py to use the latest `pyzmq` version to address installation issues (Fixes #399).

#### Documentation:
- StreamGear Updates:
  - Improved the overview section's description and wording.
  - Updated usage examples for both Single-Source Mode and Real-time Frames Mode.
    - Updated StreamGear usage examples for device audio input.
    - Refactored sections for Live Streaming usage.
      - Added warning for unsupported `-livestream` parameter in Single-Source Mode.
    - Updated usage example for device video source.
  - Addressed deprecation of the `terminate()` method in favor of the new `close()` method.
    - Updated respective notices for the deprecated `terminate()` method and `rgb_mode` parameter.
  - Added the use of new `-enable_force_termination` parameter.
  - Added a new FAQ entry about the deprecated `rgb_mode` parameter.
  - Added a deprecation warning admonition for the `rgb_mode` parameter in the `stream()` method.
    - Removed the obsolete usage example for deprecation RGB mode with StreamGear.
  - Added documentation and usage of the new `-enable_force_termination` parameter.
    - Modified the warning message to mention that forced termination can cause corrupted output in certain scenarios.
  - Updated the docstring for the `stream()` method and `transcode_source()` method.
  - Refactored the StreamGear API Parameters documentation to enhance clarity and readability.
    - Refined the description of the `-streams` attribute of the StreamGear API.
- Updated the documentation for the `-disable_force_termination` parameter in WriteGear API.
- Improved overall documentation quality, including enhancing clarity, readability, consistency, and precision.
- Added detailed explanations, practical examples, best practices, and clearer usage patterns.
- Fixed various issues like typos, formatting errors, code highlighting issues, and grammar inconsistencies.
- Updated sections, code examples, admonitions, and comments for better clarity and accuracy.
- Fixed minor typo in `js_hook.py`.

#### CI
- Improved parameterized test cases to support floating point values.
- Fixed various typos and code issues in tests.
- Updated StreamGear tests to use the new `close()` method instead of the deprecated terminate() method.
- Updated tests of various APIs for better coverage and reliability.
- Enabled `kill=True` in `close()` in NetGear Tests.
- Fixed expected duration value in parameterized test case from `8` to `8.44` since `test_extract_time` function now supports floating point values.
- Fixed `test_secure_mode` NetGear test:
  - Added `"127.0.0.1"` address to allow common endpoint for connection.
  - Added `"jpeg_compression":False` to disable frame compression, allowing frame to be the same while assertion.


#### Maintenance:
- Applied short-circuiting to simplify code across various APIs and tests.
- Improved logging, parameter validation, and added descriptive dialogs across various APIs.
@abhiTronix
Copy link
Owner

Stream copy successfully introduced for Single Source mode in StreamGear API in commit d4243ab

VidGear v0.3.3 automation moved this from To do to Done Jun 7, 2024
@abhiTronix abhiTronix added SOLVED 🏁 This issue/PR is resolved now. Goal Achieved! and removed WORK IN PROGRESS 🚧 currently been worked on. labels Jun 7, 2024
@abhiTronix
Copy link
Owner

abhiTronix commented Jun 7, 2024

⚠️ It is incompatible with Real-time Frames Mode as it requires re-encoding of incoming frames.

@juniorcines
Copy link
Author

⚠️ It is incompatible with Real-time Frames Mode as it requires re-encoding of incoming frames.

In this code could you tell me how it could be used? What about copy?

`# import required libraries
from vidgear.gears import StreamGear

activate Single-Source Mode with valid video input

stream_params = {"-video_source": "foo.mp4"}

describe a suitable master playlist location/name and assign params

streamer = StreamGear(output="hls_out.m3u8", format = "hls", **stream_params)

trancode source

streamer.transcode_source()

terminate

streamer.terminate()
`

@juniorcines
Copy link
Author

code python

My code is in Spanish.

But what my code does is create an array with the list of multiple qualities downgrade + the original quality since your system always adds the original quality.

so I made it so that if there are multiple qualities it is used:

stream_params["-vcodec"] = "h264_nvenc"
stream_params["-streams"] = streams

and if not multiple qualities, then only the original quality that is used:

stream_params["-vcodec"] = "copy"

which would be to move to HLS faster, the video is in H264.

but I get the following error with the video regardless of whether it is original quality or multiple quality

error python

@abhiTronix
Copy link
Owner

⚠️ It is incompatible with Real-time Frames Mode as it requires re-encoding of incoming frames.

In this code could you tell me how it could be used? What about copy?

`# import required libraries from vidgear.gears import StreamGear

activate Single-Source Mode with valid video input

stream_params = {"-video_source": "foo.mp4"}

describe a suitable master playlist location/name and assign params

streamer = StreamGear(output="hls_out.m3u8", format = "hls", **stream_params)

trancode source

streamer.transcode_source()

terminate

streamer.terminate() `

@juniorcines Yeah sure. Please wait.

@abhiTronix
Copy link
Owner

abhiTronix commented Jun 7, 2024

My code is in Spanish.

But what my code does is create an array with the list of multiple qualities downgrade + the original quality since your system always adds the original quality.

so I made it so that if there are multiple qualities it is used:

stream_params["-vcodec"] = "h264_nvenc"
stream_params["-streams"] = streams
and if not multiple qualities, then only the original quality that is used:

stream_params["-vcodec"] = "copy"

which would be to move to HLS faster, the video is in H264.

but I get the following error with the video regardless of whether it is original quality or multiple quality

@juniorcines The changes hasn't been released yet, and you're using old version of vidgear. The v0.3.3 will be released in few day, for using it right now, you need to clone and install our testing branch from source as follows:

# clone the repository and get inside
git clone https://github.com/abhiTronix/vidgear.git && cd vidgear

# checkout the latest testing branch
git checkout testing

# Install latest stable release with all Core dependencies
pip install -U .[core]

# Or Install latest stable release with all Core & Asyncio dependencies
pip install -U .[asyncio]

Then you could use stream copy as follows:

# import required libraries
from vidgear.gears import StreamGear

# activate Single-Source Mode
stream_params = {
    "-video_source": "foo.mp4",
    "-vcodec": "copy",
}
# describe a suitable master playlist location/name and assign params
streamer = StreamGear(output="hls_out.m3u8", format = "hls", **stream_params)
# trancode source
streamer.transcode_source()
# terminate
streamer.terminate()

Or with multiple streams:

# import required libraries
from vidgear.gears import StreamGear

# activate Single-Source Mode and also define various streams
stream_params = {
    "-video_source": "foo.mp4",
    "-vcodec": "copy",
    "-streams": [
        {"-resolution": "1280x720", "-framerate": 30.0},  # Stream2: 1280x720 at 30fps framerate
        {"-resolution": "640x360", "-framerate": 60.0},  # Stream3: 640x360 at 60fps framerate
        {"-resolution": "320x240", "-video_bitrate": "500k"},  # Stream3: 320x240 at 500kbs bitrate
    ],
}
# describe a suitable master playlist location/name and assign params
streamer = StreamGear(output="hls_out.m3u8", format = "hls", **stream_params)
# trancode source
streamer.transcode_source()
# terminate
streamer.terminate()

@abhiTronix
Copy link
Owner

@juniorcines If you still encounter any errors, do let me know. goodluck!

@abhiTronix abhiTronix added the WAITING TO TEST ⏲️ Asked user to test the suggested example/binary/solution label Jun 7, 2024
@juniorcines
Copy link
Author

juniorcines commented Jun 7, 2024

I have a question, when using:

"-vcodec": "copy",

When using it in multiple streams, in the end the video would have the same quality, and video bitrate wouldn't it?

since it is being used:

"-vcodec": "copy",

I thought that "-vcodec": "copy", It was only used without multiple streams.

@abhiTronix
Copy link
Owner

I thought that "-vcodec": "copy", It was only used without multiple streams.

@juniorcines True, I'll update that.

@abhiTronix abhiTronix reopened this Jun 8, 2024
VidGear v0.3.3 automation moved this from Done to In progress Jun 8, 2024
@abhiTronix abhiTronix removed the SOLVED 🏁 This issue/PR is resolved now. Goal Achieved! label Jun 8, 2024
@abhiTronix abhiTronix added the WORK IN PROGRESS 🚧 currently been worked on. label Jun 8, 2024
abhiTronix added a commit that referenced this issue Jun 9, 2024
…ixes #396)

- ♻️ Ignore the stream copy parameter if Real-time Frames Mode or Custom Streams are enabled, and log appropriate warnings.
- ⚡️Updated the handling of the `-acodec` parameter:
     - Use the default `aac` codec for Custom Streams.
     - Use stream copy (`-acodec copy`) for the input video's audio stream if Custom Streams are not enabled.
- ♻️ Refactor the handling of the `-livestream` parameter to ensure it is only enabled for the Real-time Frames Mode.
- ♻️ Refactor the video and audio bitrate assignment to skip the assignment when stream copy is enabled.
- 🔊 Updated log message for `-clear_prev_assets` parameter.
- ✏️ Fix a typo in comments.

Docs:
- 📝 Add a new tip box explaining the benefits of using stream copy (`-vcodec copy`) in the Single Source Mode for faster transcoding of HLS/DASH streams.
- 💬 Highlight the limitations of stream copy, such as incompatibility with Real-time Frames Mode and Custom Streams, which require re-encoding of frames.
- 💬 Clarify that the audio stream copy (`-acodec copy`) is automatically applied when using the input video's audio stream.
- 🎨 Fixed various issues like typos, formatting errors, code highlighting issues, and grammar inconsistencies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ENHANCEMENT ⚡ New Feature/Addition/Improvement PROPOSAL 📩 A proposal/proposition WAITING TO TEST ⏲️ Asked user to test the suggested example/binary/solution WORK IN PROGRESS 🚧 currently been worked on.
Projects
VidGear v0.3.3
In progress
Development

Successfully merging a pull request may close this issue.

2 participants