Skip to content

Video and Audio Frame Lengths Not Matching #2

@EpicCodeWizard

Description

@EpicCodeWizard

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")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions