Is it safe to defragment a file that's being used?

Solution 1:

Unless there is an error on the drive, it is unlikely that defragmenting will cause any problems while files are being read and written.

My personal preference is to close open apps & programs and let defrag run overnight.

If that will not work for you, you can try: https://docs.microsoft.com/en-us/windows/win32/fileio/defragmenting-files which is a more hands-on approach.