Skip to content

Commit

Permalink
enhancement #48 : add automatic mux of mp4 file
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienDanieau committed Aug 8, 2024
1 parent cd5aea9 commit 90c19b8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

*.gdp
*.mp4
54 changes: 43 additions & 11 deletions src/examples/simple_recorder.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import argparse
import os
import subprocess
from typing import Optional

import gi

gi.require_version("Gst", "1.0")
from gi.repository import Gst
from gi.repository import Gst # noqa : E402

from gst_signalling.utils import find_producer_peer_id_by_name
from gst_signalling.utils import find_producer_peer_id_by_name # noqa : E402


class GstRecorder:
Expand Down Expand Up @@ -115,6 +117,41 @@ def process_msg(bus: Gst.Bus) -> bool:
return True


def save_file(file_name: str) -> None:
"""
mux the gdp streams with a command similar to
gst-launch-1.0 \
mp4mux name=mux ! filesink location=recording.mp4 \
filesrc location=video_0.gdp ! gdpdepay ! h264parse ! queue ! mux. \
filesrc location=video_1.gdp ! gdpdepay ! h264parse ! queue ! mux. \
filesrc location=audio_0.gdp ! gdpdepay ! opusparse ! queue ! mux.
"""

base_command = f"gst-launch-1.0 mp4mux name=mux ! filesink location={file_name} "
files = [f for f in os.listdir() if f.endswith(".gdp")]

if len(files) == 0:
print("no gdp file found")
return
else:
print(f"gdp files to combine: {files}")

file_commands = []

for file in files:
if os.path.isfile(file):
if "video" in file:
file_commands.append(f"filesrc location={file} ! gdpdepay ! h264parse ! queue ! mux.")
elif "audio" in file:
file_commands.append(f"filesrc location={file} ! gdpdepay ! opusparse ! queue ! mux.")

command = base_command + " ".join(file_commands)

print(f"running : {command}")

subprocess.run(command, shell=True)


def main() -> None:
parser = argparse.ArgumentParser(description="webrtc gstreamer simple recorder")
parser.add_argument("--signaling-host", default="127.0.0.1", help="Gstreamer signaling host")
Expand All @@ -130,6 +167,8 @@ def main() -> None:
help="producer name",
)

parser.add_argument("--output", type=str, help="output mp4 file", default="recording.mp4")

args = parser.parse_args()

if args.remote_producer_peer_id is None and args.remote_producer_peer_name is None:
Expand All @@ -153,15 +192,8 @@ def main() -> None:
# Free resources
recorder.stop()

save_file(args.output)


if __name__ == "__main__":
main()

"""
This will create video_x.gdp streams. You can mux them using:
gst-launch-1.0 \
mp4mux name=mux ! filesink location=recording.mp4 \
filesrc location=video_0.gdp ! gdpdepay ! h264parse ! queue ! mux. \
filesrc location=video_1.gdp ! gdpdepay ! h264parse ! queue ! mux. \
filesrc location=audio_0.gdp ! gdpdepay ! opusparse ! queue ! mux.
"""

0 comments on commit 90c19b8

Please sign in to comment.