Skip to content

Commit

Permalink
Changed to using pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashton Doane authored and Ashton Doane committed Aug 20, 2024
1 parent ff80659 commit e27fc50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/ilabs_streamsync/example_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
if __name__ == "__main__":
# load an MNE raw file
raw = None
# cam1 = "/Users/ashtondoane/VideoSync_NonSubject/sinclair_alexis_audiosync_240110_CAM3.mp4"
cam1 = "/Users/user/VideoSync_NonSubject/sinclair_alexis_audiosync_240110_CAM3.mp4"
flux1 = None
my_events = []

# extract_audio_from_video(cam1, "/Users/ashtondoane/VideoSync_NonSubject/output")
ss = StreamSync(None, None)
ss.add_stream("/Users/ashtondoane/VideoSync_NonSubject/output/sinclair_alexis_audiosync_240110_CAM3_16bit.wav", channel=1)
extract_audio_from_video(cam1, "/Users/user/VideoSync_NonSubject/output")
ss = StreamSync(None, None) #Raw type not supported yet
ss.add_stream("/Users/user/VideoSync_NonSubject/output/sinclair_alexis_audiosync_240110_CAM3_16bit.wav", channel=1)
ss.plot_sync_pulses(tmin=0.998,tmax=1)

# subjects = ["146a", "222b"]
Expand Down
20 changes: 11 additions & 9 deletions src/ilabs_streamsync/streamsync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import pathlib
import subprocess

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -40,7 +41,7 @@ def add_stream(self, stream, channel=None, events=None):

def _extract_data_from_stream(self, stream, channel):
"""Extracts pulses and raw data from stream provided."""
ext = os.path.splitext(stream)[1]
ext = pathlib.Path(stream).suffix
if ext == ".fif":
return self._extract_data__from_raw(stream, channel)
if ext == ".wav":
Expand Down Expand Up @@ -69,7 +70,7 @@ def plot_sync_pulses(self, tmin=0, tmax=float('inf')):
tt = np.arange(npts) / stream[1]
idx = np.where((tt>=tmin) & (tt<tmax))
axset[i+1].plot(tt[idx], stream[2][idx].T)
axset[i+1].set_title(os.path.basename(stream[0]))
axset[i+1].set_title(pathlib.Path(stream[0]).name)
# Make label equal to simply the cam number
plt.show()

Expand All @@ -92,11 +93,12 @@ def extract_audio_from_video(path_to_video, output_dir):
"""
audio_codecout = 'pcm_s16le'
audio_suffix = '_16bit'
audio_file = os.path.basename(os.path.splitext(path_to_video)[0]) + audio_suffix + '.wav'
if not os.path.exists(path_to_video):
p = pathlib.Path(path_to_video)
audio_file = p.stem + audio_suffix + '.wav'
if not p.exists():
raise ValueError('Path provided cannot be found.')
if os.path.exists(os.path.join(output_dir, audio_file)):
raise Exception("Audio already exists for " + path_to_video + " in output directory " + output_dir)
if pathlib.PurePath.joinpath(pathlib.Path(output_dir), pathlib.Path(audio_file)).exists():
raise Exception(f"Audio already exists for {path_to_video} in output directory.")

command = ['ffmpeg',
'-acodec', 'pcm_s24le', # force little-endian format (req'd for Linux)
Expand All @@ -111,8 +113,8 @@ def extract_audio_from_video(path_to_video, output_dir):
pipe = subprocess.run(command, timeout=50, check=False)

if pipe.returncode==0:
print('Audio extraction was successful for ' + path_to_video)
output_path = os.path.join(output_dir, audio_file)
print(f'Audio extraction was successful for {path_to_video}')
output_path = pathlib.PurePath.joinpath(pathlib.Path(output_dir), pathlib.Path(audio_file))
os.renames(audio_file, output_path)
else:
print("Audio extraction unsuccessful for " + path_to_video)
print(f"Audio extraction unsuccessful for {path_to_video}")

0 comments on commit e27fc50

Please sign in to comment.