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

Video and Audio Frame Lengths Not Matching #2

Open
EpicCodeWizard opened this issue Jul 20, 2022 · 0 comments
Open

Video and Audio Frame Lengths Not Matching #2

EpicCodeWizard opened this issue Jul 20, 2022 · 0 comments

Comments

@EpicCodeWizard
Copy link

EpicCodeWizard commented Jul 20, 2022

I can successfully save audio using pyaudio and save video using cv2. For some odd reason, the 2 files are different lengths. How can I make the 2 files the same length? Files:

test.mp4

Audio
Code:

# Importing kinect
from libkinect2.utils import merge_audio_frames
from libkinect2 import Kinect2

# Connecting kinect
kinect = Kinect2(use_sensors=['color', 'audio'])
kinect.connect()
kinect.wait_for_worker()

# Saving audio and video
audio = []
video = []
print("recording")
for frame, color_img, audio_frames in kinect.iter_frames():
    if audio_frames:
        full_frame = merge_audio_frames(audio_frames)
        audio.append(full_frame.data)
    video.append(color_img)
    if frame == 500:
        print("recording done")
        break

# Disconnecting kinect
kinect.disconnect()

# Save audio
import wave
wavefile = wave.open("test.wav", "wb")
wavefile.setnchannels(1)
wavefile.setsampwidth(4)
wavefile.setframerate(16000)
wavefile.writeframes(b"".join(audio))
wavefile.close()

# Save video
import cv2
height, width, layers = stream2[0].shape
video = cv2.VideoWriter("test2.mp4", 0, 30, (width,height))
for image in stream2:
    video.write(image)
cv2.destroyAllWindows()
video.release()

# Use ffmpeg to speed up video (original video is 0.5x speed for some reason)
import os
os.system("ffmpeg -i test2.mp4 -filter:v \"setpts=PTS/2\" test.mp4")
@EpicCodeWizard EpicCodeWizard changed the title Video and Audio Frames Not Matching Video and Audio Frame Lengths Not Matching Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant