在Firebase上使用python将音频添加到视频

rfbsl7qr  于 2023-06-20  发布在  Python
关注(0)|答案(1)|浏览(89)

简单和容易,或者我认为。Firebase似乎很难编辑视频,它在模拟器中工作,但在部署时不工作,反之亦然。我一直在尝试简单地添加一个音频文件到视频使用的功能。现在的输出只有0个字节。
现在,这是我对这个特定示例的测试函数:
它会给予你一个链接,假设你在testfiles文件夹中有video.mp4和audio.mp3文件,它会将它们上传到输入桶,然后到触发桶运行函数来添加这两个文件并将它们输出到输出桶。但是ffmpeg并不总是工作,moviepy也不总是工作,加上它太慢了。

def addTestBuckets2(req: https_fn.Request) -> https_fn.Response:

    # create the buckets
    testBucketInput = storage.bucket("testbucketinput")
    testBucketOutput = storage.bucket("testbucketoutput")
    testBucketTrigger = storage.bucket("testbuckettrigger")

    # upload the test files
    testBucketInput.blob("audio.mp3").upload_from_filename(
        "D:/backendfunctions/functions/testfiles/audio.mp3")
    testBucketInput.blob("video.mp4").upload_from_filename(
        "D:/backendfunctions/functions/testfiles/video.mp4")

    open("trigger.txt", "w").close()
    testBucketTrigger.blob("trigger.txt").upload_from_filename(
        "trigger.txt")

    # try to download the output.mp4 from the bucket on a loop
    keepTrying = 0
    while keepTrying < 100:
        try:
            testBucketOutput.blob("output.mp4").download_to_filename(
                "output.mp4")
            break
        except:
            sleep(.1)
            keepTrying += 1

    # os.remove("output.mp4")

    # return
    return https_fn.Response("Bucket tested")

@storage_fn.on_object_finalized(bucket="testbuckettrigger", region=region)
def editvidtestfunction(event: storage_fn.CloudEvent[storage_fn.StorageObjectData | None],) -> None:
    """ Test functions editing limit """

    # get the file path from the event
    file_path = event.data.name

    # get the file name
    file_name = file_path.split("/")[-1]
    # just_path = file_path.split("/")[:-1][0]
    # just_name = '.'.join(file_name.split('.')[:-1])

    # donwload the file from the bucket to local function
    storage.bucket("testbucketinput").blob(
        "video.mp4").download_to_filename("video.mp4")
    storage.bucket("testbucketinput").blob(
        "audio.mp3").download_to_filename("audio.mp3")

    # add the audio to the video using ffmpeg
    cmd = f"ffmpeg -i video.mp4 -i audio.mp3 -c copy -map 0:v:0 -map 1:a:0 output.mp4"
    subprocess.run(cmd, shell=True)

    # upload the final video to the output bucket
    storage.bucket("testbucketoutput").blob(
        "output.mp4").upload_from_filename("output.mp4")```
m1m5dgzv

m1m5dgzv1#

首先,你需要在Python中安装所需的包:

pip install firebase-admin
pip install google-cloud-storage

接下来设置您的Firebase Admin SDK,如果您没有现有的项目,请创建一个新项目,并为该项目生成一个私钥,该私钥将用于您的python代码然后将您的媒体上传到云接下来使用Python库处理媒体,您可以使用Moviepy

pip install moviepy

使用选定的库对视频和音频文件执行所需的编辑操作。例如,您可以连接音频文件,将音频叠加到视频上,或根据需要调整时间戳。将您编辑的媒体文件保存到云端,不要忘记在此过程后进行清理。

import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
from moviepy.editor import VideoFileClip, AudioFileClip

# Step 2: Set up the Firebase Admin SDK
cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
    'storageBucket': 'your-storage-bucket.appspot.com'
})

# Step 3: Upload your media files to Cloud Storage
bucket = storage.bucket()
video_blob = bucket.blob("videos/sample_video.mp4")
audio_blob = bucket.blob("audios/sample_audio.wav")
video_blob.upload_from_filename("path/to/sample_video.mp4")
audio_blob.upload_from_filename("path/to/sample_audio.wav")

# Step 4: Process the video and audio files
video_path = video_blob.generate_signed_url(datetime.timedelta(seconds=1), method='GET')
audio_path = audio_blob.generate_signed_url(datetime.timedelta(seconds=3), method='GET')
video_clip = VideoFileClip(video_path)
audio_clip = AudioFileClip(audio_path)
final_clip = video_clip.set_audio(audio_clip)

# Step 5: Save the edited media file
final_clip.write_videofile("path/to/edited_video.mp4", codec="libx264")

# Step 6: Clean up (optional)
video_blob.delete()
audio_blob.delete()

在代码中执行以下替换"path/to/serviceAccountKey.json":Firebase服务帐户密钥JSON文件的路径。"your-storage-bucket.appspot.com":您的Firebase存储桶名称。"videos/sample_video.mp4":您的视频文件在Cloud Storage中的所需位置和文件名。"audios/sample_audio.wav":您的音频文件在Cloud Storage中的所需位置和文件名。"path/to/sample_video.mp4":视频文件的本地路径。"path/to/sample_audio.wav":音频文件的本地路径。"path/to/edited_video.mp4":本地计算机上编辑的视频文件所需的位置和文件名。

相关问题