Video Playback: Difference between revisions
Jump to navigation
Jump to search
| Line 101: | Line 101: | ||
win.close() | win.close() | ||
core.quit() | core.quit() | ||
</syntaxhighlight> | |||
Example demonstrating if video and audio encoding are correct: | |||
<syntaxhighlight lang="python" line> | |||
import subprocess | |||
import json | |||
file_path = "C_dyad1_video2_241123.mp4" | |||
def check_video_file(file_path): | |||
try: | |||
# Run ffprobe to get file metadata in JSON format | |||
result = subprocess.run( | |||
[ | |||
"ffprobe", | |||
"-v", "error", | |||
"-show_streams", | |||
"-show_format", | |||
"-print_format", "json", | |||
file_path | |||
], | |||
stdout=subprocess.PIPE, | |||
stderr=subprocess.PIPE, | |||
text=True | |||
) | |||
metadata = json.loads(result.stdout) | |||
except Exception as e: | |||
print(f"Error running ffprobe: {e}") | |||
return | |||
# Check for video stream | |||
video_stream = next((stream for stream in metadata['streams'] if stream['codec_type'] == 'video'), None) | |||
if video_stream: | |||
# Check video codec | |||
video_codec = video_stream.get('codec_name') | |||
if video_codec == 'h264': | |||
print("Video codec: H.264") | |||
else: | |||
print(f"ERROR: Video codec is NOT H.264 (Found: {video_codec})") | |||
# Extract and report frame rate | |||
if 'r_frame_rate' in video_stream: | |||
raw_frame_rate = video_stream['r_frame_rate'] | |||
calculated_frame_rate = eval(raw_frame_rate) # Convert string like "30/1" to float | |||
print(f"Frame rate: {calculated_frame_rate:.2f} FPS (raw: {raw_frame_rate})") | |||
else: | |||
print("ERROR: Could not determine raw frame rate from metadata.") | |||
# Check for constant frame rate | |||
if video_stream.get('avg_frame_rate'): | |||
avg_frame_rate = eval(video_stream['avg_frame_rate']) | |||
if abs(avg_frame_rate - calculated_frame_rate) < 0.01: | |||
print("Frame rate: Constant") | |||
else: | |||
print(f"ERROR: Frame rate is NOT constant (avg_frame_rate: {avg_frame_rate:.2f} FPS)") | |||
else: | |||
print("ERROR: Could not determine average frame rate consistency.") | |||
# Check for frame drops | |||
try: | |||
frame_info_result = subprocess.run( | |||
[ | |||
"ffprobe", | |||
"-v", "error", | |||
"-select_streams", "v:0", | |||
"-show_entries", "frame=pkt_pts_time", | |||
"-of", "csv=p=0", | |||
file_path | |||
], | |||
stdout=subprocess.PIPE, | |||
stderr=subprocess.PIPE, | |||
text=True | |||
) | |||
# Filter out empty or invalid lines | |||
frame_times = [ | |||
float(line.strip()) for line in frame_info_result.stdout.splitlines() | |||
if line.strip() # Exclude empty lines | |||
] | |||
expected_interval = 1.0 / calculated_frame_rate # Expected time between frames | |||
frame_drops = [ | |||
i for i, (t1, t2) in enumerate(zip(frame_times, frame_times[1:])) | |||
if abs(t2 - t1 - expected_interval) > 0.01 # Tolerance for irregularity | |||
] | |||
if frame_drops: | |||
print(f"ERROR: Detected frame drops at frames: {frame_drops}") | |||
else: | |||
print("No frame drops detected.") | |||
except Exception as e: | |||
print(f"Error analyzing frames for drops: {e}") | |||
else: | |||
print("ERROR: No video stream found") | |||
# Check for audio stream | |||
audio_stream = next((stream for stream in metadata['streams'] if stream['codec_type'] == 'audio'), None) | |||
if audio_stream: | |||
# Check audio codec | |||
audio_codec = audio_stream.get('codec_name') | |||
if audio_codec == 'pcm_s16le': | |||
print("Audio codec: WAV (PCM)") | |||
else: | |||
print(f"ERROR: Audio codec is NOT WAV (PCM) (Found: {audio_codec})") | |||
# Check sample rate | |||
sample_rate = audio_stream.get('sample_rate') | |||
if sample_rate == "44100": | |||
print("Audio sample rate: 44.1 kHz") | |||
else: | |||
print(f"ERROR: Audio sample rate is NOT 44.1 kHz (Found: {sample_rate} Hz)") | |||
else: | |||
print("ERROR: No audio stream found") | |||
# Check synchronization | |||
if video_stream and audio_stream: | |||
video_start_pts = float(video_stream.get('start_time', 0)) | |||
audio_start_pts = float(audio_stream.get('start_time', 0)) | |||
if abs(video_start_pts - audio_start_pts) < 0.01: # Tolerance for synchronization | |||
print("Video and audio are synchronized.") | |||
else: | |||
print(f"ERROR: Video and audio are NOT synchronized. Start difference: {abs(video_start_pts - audio_start_pts):.3f} seconds") | |||
else: | |||
print("ERROR: Could not determine synchronization (missing video or audio streams).") | |||
# Example usage | |||
if __name__ == "__main__": | |||
check_video_file(file_path) | |||
</syntaxhighlight> | </syntaxhighlight> | ||