I'm trying to create and save a video file in python via opencv
video_name = os.path.join(DIR, "test.mp4")
images = [img for img in os.listdir(DIR) if img.startswith(f'{test_')]
images = sorted(images, key=graph_utils_py.numericalSort)
frame = cv2.imread(os.path.join(DIR, images[0]))
height, width, layers = frame.shape
video = cv2.VideoWriter(video_name, 0, 1, (width, height))
for image in images:
fpath = os.path.join(DIR, image)
video.write(cv2.imread(fpath))
os.remove(fpath) # remove the image file
cv2.destroyAllWindows()
video.release()
The above generates the video file without any issues but I get a warning
OpenCV: FFMPEG: tag 0x00000000/'????' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
[mp4 # 000001d0378cdec0] Could not find tag for codec rawvideo in stream #0, codec not currently supported in container
This creates an issue when I try to embed this video file in a pdf generated using latex.
From what I understand [The video data inside the file must be encoded with the H.264 codec.][1] to successfully view the embedded video in pdf.
Any suggestion on how to generate the mp4 file with H.264 codec in python will be greatly appreciated.
EDIT:
I tried what has been suggested below and the following error occurs
OpenCV: FFMPEG: tag 0x31435641/'AVC1' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'
Failed to load OpenH264 library: openh264-1.8.0-win64.dll
Please check environment and/or download library: https://github.com/cisco/openh264/releases
[libopenh264 # 000002833670e500] Incorrect library version loaded
Could not open codec 'libopenh264': Unspecified error
You need to pass correct codec:
fourcc = cv2.VideoWriter_fourcc(*'AVC1')
fps = 1
video = cv2.VideoWriter(video_name, fourcc, fps, (width, height))
Related
I am converting video files using an elastic transcoder. AWS Lambda function get video file from s3 bucket and convert it according to PresetId.
But, I need to compare video file resolution with PresetId. If the video file resolution is higher than the PresetId video resolution, then convert this video file, otherwise do not need to convert all video files.
Do you have access to ffmpeg/ffprobe/ffplay from AWS - is it possible to call them and take their console output? I'm not sure about what's allowed in AWS, but on Desktop you could call ffprobe etc. - it could return text or even JSON.
Many ways are suggested here: Getting video dimension / resolution / width x height from ffmpeg
One of the suggested ways:
ffprobe -v error -show_entries stream=width,height -of csv=p=0:s=x input.m4v
1280x720
I have base64 audio string like data:audio/mpeg;base64,//OAxAAAAANIAAAAABhqZ3f4StN3gOAaB4NAUBYZLv......, and I was trying to convert the base64 to wav file using base64 module in Python:
decode_bytes = base64.b64decode(encoding_str)
with open(file_name + '.wav', "wb") as wav_file:
wav_file.write(decode_bytes)
Then I was trying to convert the audio to text using speech_recognition module, and it gives error below:
ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format
Is there a solution for this problem?
Seems like your audio file is mp3 from mime-type - audio/mpeg. You will need to save it as mp3
decode_bytes = base64.b64decode(encoding_str)
with open(file_name + '.mp3', "wb") as wav_file:
wav_file.write(decode_bytes)
And convert the mp3 to wav format either using pydub or FFmpeg and then give this wav file to speech_recognition module.
I want to use video Capturing in Opencv with providing video path or camera path from a text file,
is their any possibility to Provide Video Source From a Text File in Open CV Python
cap = cv2.VideoCapture(open('file.txt').read())
In text file just write your : -
Video file name with extn. : - Video.mp4
or
Camera path : - rtsp://User_Name:Password#127.0.0.1/Streaming/Channels/501
I am uploading videos to Youtube, and on my Android phone I am downloading them using Youtube Red. I am playing these downloaded videos in the background, when the screen is off.
This works with the wast majority of the videos, except the ones that I am uploading.
I did read the recommended upload formats, I tried several codecs, but no luck. My audios stop the second I am shutting off the screen.
What I finally found using youtube-dl -F is that my videos do not have audio-only tracks with webm extension, only as m4a (after Youtube processed them).
So my question is: what makes Youtube create webm audio files for some videos, but not for the others? Is there a way to force this (I suppose not). Is there a way to suggest it? As I mentioned, I tried a wide variety of codecs - video and audio, and the combinations - when generating my files to be uplaoded.
A sample output for a file which works:
format code extension resolution note
249 webm audio only DASH audio 52k , opus # 50k, 73.58KiB
250 webm audio only DASH audio 66k , opus # 70k, 92.62KiB
251 webm audio only DASH audio 114k , opus #160k, 161.14KiB
171 webm audio only DASH audio 115k , vorbis#128k, 161.27KiB
140 m4a audio only DASH audio 127k , m4a_dash container, mp4a.40.2#128k, 180.79KiB
and the output for one file which does not:
format code extension resolution note
139 m4a audio only DASH audio 49k , m4a_dash container, mp4a.40.5# 48k (22050Hz), 1.20MiB
140 m4a audio only DASH audio 129k , m4a_dash container, mp4a.40.2#128k (44100Hz), 3.20MiB
I've been making video flipbooks by putting together individual frames of PNG files. At first I was using FFMPEG. I just got OpenCV working.
However, every time I make one, I have to manually choose the encoder to use.
The window has the title: Video Compression. The about box says it's Intel Indeo Video iYUV 2.0
Can I specify this somewhere in a Python call to OpenCV so I don't have to choose every time?
Here is the code that is making the video. Note I'm resizing the frames as they were different sized source frames.
video = cv.VideoWriter(outfile, -1, 30, (width, height), False)
for image in images:
cvimage = cv.imread(os.path.join(png_working_folder, image))
resized = cv.resize(cvimage, (800,800))
video.write(resized)
I found one solution.
I wasn't using fourcc previously. Adding it to my call got me over this hump.
fourcc = cv.VideoWriter_fourcc(*'XVID')
video = cv.VideoWriter(outfile, fourcc, 30, (width, height))
Adding the fourcc to the call was the key.