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

Stream resize error #244

Closed
ioangatop opened this issue Sep 1, 2021 · 12 comments · Fixed by #274
Closed

Stream resize error #244

ioangatop opened this issue Sep 1, 2021 · 12 comments · Fixed by #274
Labels
BUG 🐛 Vidgear api's error, flaw or fault EXTERNAL BUG 👾 Bug caused by external dependent library SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Milestone

Comments

@ioangatop
Copy link

ioangatop commented Sep 1, 2021

Hi! There is an issue with frame fetching from a youtube video when using different resolutions.

Code to reproduce:

from vidgear.gears.helper import dimensions_to_resolutions
from vidgear.gears import VideoGear

import pafy
import cv2

source = "https://youtu.be/j1GLs_fMn1s"

video = pafy.new(source) 
valid_streams = dimensions_to_resolutions(
    [stream.resolution for stream in video.allstreams]
)   # ['144p', '240p', '360p', '480p', '720p', '1080p']

options = {
    "STREAM_RESOLUTION": valid_streams[1],  # '240p'
    "STREAM_PARAMS": {"nocheckcertificate": True}
}

stream = VideoGear(
    source=source,
    stream_mode=True,
    **options
).start()

while True:
    frame = stream.read()

    if frame is None:
        break

    cv2.imwrite('frame.jpg', frame)

    break

stream.stop()

Fetched image w/ 240p resolution (happens with 480p as well):
240

Fetched image w/ 144p resolution:
144p

Logs

10:17:21 :: CamGear :: INFO :: Using Youtube-dl Backend
[ WARN:0] global /opt/opencv-4.5.1/modules/videoio/src/cap_gstreamer.cpp (924) open OpenCV | GStreamer warning: unable to query duration of stream
[ WARN:0] global /opt/opencv-4.5.1/modules/videoio/src/cap_gstreamer.cpp (961) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=449, duration=-1
10:17:22 :: CamGear :: DEBUG :: Setting backend `1800` for this source.
@welcome
Copy link

welcome bot commented Sep 1, 2021

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 Sep 1, 2021

@ioangatop Yes bug is reproducible. But bug is with OpenCV's GSTREAMER backend and not vidgear. You can test this by:

  1. First clone vidgear:
  # clone the repository and get inside
  git clone https://github.com/abhiTronix/vidgear.git
  1. Then, commenting this line, so as to disable enforced cv2.CAP_GSTREAMER backend.

backend = cv2.CAP_GSTREAMER

  1. Then install vidgear as:
  # install normally
  cd vidgear
  pip install .
  1. Running your code again with CAP_FFMPEG backend:
from vidgear.gears.helper import dimensions_to_resolutions
from vidgear.gears import VideoGear

import pafy
import cv2

source = "https://youtu.be/j1GLs_fMn1s"

video = pafy.new(source)
valid_streams = dimensions_to_resolutions(
    [stream.resolution for stream in video.allstreams]
)  # ['144p', '240p', '360p', '480p', '720p', '1080p']

options = {
    "STREAM_RESOLUTION": valid_streams[1],  # '240p'
    "STREAM_PARAMS": {"nocheckcertificate": True},
}

stream = VideoGear(
    source=source, stream_mode=True, backend=cv2.CAP_FFMPEG, **options
).start()

while True:
    frame = stream.read()

    if frame is None:
        break

    cv2.imwrite("frame.jpg", frame)

    break

stream.stop()

@abhiTronix abhiTronix added BUG CONFIRMED ✅ Bug is confirmed! BUG 🐛 Vidgear api's error, flaw or fault EXTERNAL BUG 👾 Bug caused by external dependent library labels Sep 1, 2021
@abhiTronix
Copy link
Owner

@ioangatop Can you confirm your OpenCV version by excuting this command:

python3 -c "import cv2; print(cv2.__version__)"

@abhiTronix abhiTronix added the WAITING FOR RESPONSE ⏳ Waiting for the user response. label Sep 1, 2021
@ioangatop
Copy link
Author

ioangatop commented Sep 1, 2021

@abhiTronix thanks for the quick response. My OpenCV version is 4.5.1.

I guess there isn't an easier way to change / choose the backend, right?

@abhiTronix
Copy link
Owner

@abhiTronix thanks for the quick response. My OpenCV version is 4.5.1.

@ioangatop Thanks, I'm looking into OpenCV issues for clue.

I guess there isn't an easier way to change / choose the backend, right?

I can make that simpler, but there's a bug with FFmpeg, which made me to enforce Gstreamer backend in the first place.

@abhiTronix
Copy link
Owner

@ioangatop OpenCV is crappy in doing VideoIO even with backends. I'm thinking to switch FFmpeg decoding completely with issue #148. But unfortunately, It will going to happen next year due to busy 2021.

I think the best resolution to this problem for now is to use 360p or 720p and then reduce size manually. You can use reducer() for that purpose.

@abhiTronix abhiTronix added WORKAROUND PRESENT 🆗 A workaround is present for this issue, but not resolved! and removed BUG CONFIRMED ✅ Bug is confirmed! WAITING FOR RESPONSE ⏳ Waiting for the user response. labels Sep 1, 2021
@abhiTronix
Copy link
Owner

I'm closing this issue for now. I'll look into it in later versions.

@ioangatop
Copy link
Author

@abhiTronix thanks! I believe that worse and best in general don't have a problem, but it's good to have an option to fetch frames with a specific resolution, as it is a nice trade-off of speed and quality.

I'm not sure but I hope that in general, only these resolutions have problems, and its not for this specific stream (meaning if I put another stream url, 480p will act properly but not the 720p)

@abhiTronix
Copy link
Owner

@ioangatop I'm thinking this is to do with GStreamer opencv/opencv#19025 (comment). As we can see errors in logs:

[ WARN:0] global /home/abhishek/Downloads/opencv-master/modules/videoio/src/cap_gstreamer.cpp (898) open OpenCV | GStreamer warning: unable to query duration of stream
[ WARN:0] global /home/abhishek/Downloads/opencv-master/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=450, duration=-1

@abhiTronix
Copy link
Owner

abhiTronix commented Sep 1, 2021

and its not for this specific stream (meaning if I put another stream url, 480p will act properly but not the 720p)

@ioangatop These errors/anomalies are only for Youtube livestreams and not general videos. General videos will works out of the box for any resolution.

@abhiTronix abhiTronix pinned this issue Sep 3, 2021
@abhiTronix abhiTronix unpinned this issue Oct 27, 2021
@abhiTronix abhiTronix added WORK IN PROGRESS 🚧 currently been worked on. and removed WORKAROUND PRESENT 🆗 A workaround is present for this issue, but not resolved! labels Dec 4, 2021
@abhiTronix
Copy link
Owner

abhiTronix commented Dec 4, 2021

@ioangatop I think #274 resolves this issue too.

  # install normally
  pip install -U vidgear[core]

Test your code:

from vidgear.gears.helper import dimensions_to_resolutions
from vidgear.gears import VideoGear

import pafy
import cv2

source = "https://youtu.be/j1GLs_fMn1s"

options = {
    "STREAM_RESOLUTION": "240p",
    "STREAM_PARAMS": {"nocheckcertificate": True},
}

stream = VideoGear(
    source=source, stream_mode=True, logging=True, **options
).start()

while True:
    frame = stream.read()

    if frame is None:
        break

    cv2.imwrite("frame.jpg", frame)

    break

stream.stop()

@abhiTronix abhiTronix reopened this Dec 4, 2021
@abhiTronix
Copy link
Owner

abhiTronix commented Dec 4, 2021

Successfully resolved and merged in commit: dc26c00

Usage docs are available at: https://abhitronix.github.io/vidgear/latest/gears/camgear/usage/#using-camgear-with-youtube-videos

@abhiTronix abhiTronix added SOLVED 🏁 This issue/PR is resolved now. Goal Achieved! and removed WORK IN PROGRESS 🚧 currently been worked on. labels Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BUG 🐛 Vidgear api's error, flaw or fault EXTERNAL BUG 👾 Bug caused by external dependent library SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants