0

Is it possible to write video or audio file in current directory using moviepy in Azure Functions as azure functions have read only access?

I want to use moviepy to concatenate videos in azure functions, even though it is running locally but after it is deployed to azure functions its giving error that functions have read only access.

I used below code:

final_clip.write_videofile('finalvideo.mp4',codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a')

And when I used the following codes:

final_clip.write_videofile('/tmp/finalvideo.mp4',codec='libx264', audio_codec='aac', temp_audiofile='/tmp/temp-audio.m4a')

console logs for above codes

Expended Logs

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15
  • You might be able to use Azure Files to provide a read-write file system: https://learn.microsoft.com/en-us/azure/azure-functions/storage-considerations#mount-file-shares – John Hanley Nov 24 '21 at 17:21

1 Answers1

0

The current directory is indeed read-only. Use /tmp for temporary files (for example, open('/tmp/test.json', ...))

Below is the sample code for creating temporary files in temporary directory.

import logging
import azure.functions as func
import tempfile
from os import listdir

#---
   tempFilePath = tempfile.gettempdir()
   fp = tempfile.NamedTemporaryFile()
   fp.write(b'Hello world!')
   filesDirListInTemp = listdir(tempFilePath)

enter image description here

For complete information over temporary files refer this Document.

Here is the example code for importing Audio File in HTTP triggered function using Movie py.

import azure.functions as func
import azure.cognitiveservices.speech as speechsdk
import tempfile
import imageio
imageio.plugins.ffmpeg.download()
from moviepy.editor import AudioFileClip



speech_key="<speech service key>"
service_region="<speech service region>"
temp_file_path = tempfile.gettempdir() + "/result.wav"
text = 'hello, this is a test'

def main(req: func.HttpRequest) -> func.HttpResponse:
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

    auto_detect_source_language_config = speechsdk.languageconfig.AutoDetectSourceLanguageConfig()

    speech_synthesizer = speechsdk.SpeechSynthesizer(
        speech_config=speech_config, auto_detect_source_language_config=auto_detect_source_language_config,audio_config=None)

    result = speech_synthesizer.speak_text_async(text).get();
    if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
            stream = speechsdk.AudioDataStream(result)
            stream.save_to_wav_file(temp_file_path)
    
    myclip = AudioFileClip(temp_file_path)

    return func.HttpResponse(str(myclip.duration))

Refer this SO thread for further details.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15
  • i am getting problem in line: final_clip.write_videofile('/tmp/finalvideo.mp4',codec='libx264', audio_codec='aac', temp_audiofile='/tmp/temp-audio.m4a') – Harish Gavel Nov 25 '21 at 13:47
  • I am getting this Error Exception while executing function: Functions.videoStitch Result: Failure Exception: OSError: [Errno 32] Broken pipe MoviePy error: FFMPEG encountered the following error while writing file /tmp/e34e8650558448ed8d2f1ee45f838c3b.mp4: File "/home/site/wwwroot/.python_packages/lib/site-packages/moviepy/video/io/ffmpeg_writer.py", line 228, in ffmpeg_write_video writer.write_frame(frame) File "/home/site/wwwroot/.python_packages/lib/site-packages/moviepy/video/io/ffmpeg_writer.py", line 180, in write_frame raise IOError(error) – Harish Gavel Nov 25 '21 at 16:50