Export multi-frame dicom into multiple individual frame dicoms

Did anybody successfully export multi-frame dicom (in Lua) into multiple individual frame dicoms?
These big files are problematic for some older dicom viewers and I would like to export/extract these dicoms.
Right now I am exporting each dicom into a PatientID folder like so:

  • for i, instance in pairs(instances) do
    – Retrieve the DICOM file from Orthanc
    local dicom = RestApiGet(‘/instances/’ .. instance[‘ID’] .. ‘/file’)
    – Write to the file
    local target = assert(io.open(receivepath1 .. ‘/’ .. instance[‘ID’] .. ‘.dcm’, ‘wb’))
    target:write(dicom)
    target:close()
    end *
    Please let me know if anyone did it and how.
    Thank you,
    Emil