Reputation: 153
I have following code to create chunk of video.
permission_classes=[AllowAny]
video_serializer = serializers.video_serializer
def process_video(self, video_path, video_id):
# Set the path where the processed videos will be saved
output_dir = os.path.join(settings.MEDIA_ROOT, 'processed_videos')
# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Add your ffmpeg_streaming code here to process the video
full_video_path = os.path.join(settings.MEDIA_ROOT, str(video_path))
print(video_path)
video = ffmpeg_streaming.input(full_video_path)
print(video)
# Add your ffmpeg_streaming code here to process the video
_144p = Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024))
_240p = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
_360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
_480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
_720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
dash = video.dash(Formats.h264())
dash.representations(_144p, _240p, _360p, _480p, _720p)
dash.output(os.path.join(output_dir, f'dash-stream-{video_id}.mpd'))
def post(self,request):
try:
video_data = self.video_serializer(data=request.data)
video_data.is_valid(raise_exception=True)
data = video_data.validated_data
video_instance = Video.objects.create(
id = data.get('id'),
saved_location = data.get('video')
)
video_instance.save()
self.process_video(video_instance.saved_location, video_instance.id)
return Response({"success":True})
except Exception as e:
return Response({"success":False,"message":str(e)})
This code is working well as it has created different chunk files as well as a .mpd
file of a video inside media/processed_video
folder.
Then I wrote following code to stream that video using that .mpd
folder.
def get (self,request,video_id):
try:
mpd_path = os.path.join(settings.MEDIA_ROOT, 'processed_videos', f'dash-stream-{video_id}.mpd')
with open(mpd_path, 'rb') as f:
mpd_content = f.read()
response = HttpResponse(mpd_content, content_type='application/dash+xml')
# Set Content-Disposition header to make the response downloadable
response['Content-Disposition'] = f'attachment; filename="dash-stream-{video_id}.mpd"'
# Optionally set Content-Length header to specify the size of the file
response['Content-Length'] = len(mpd_content)
return response
except Exception as e:
return Response({"success":False,"message":str(e)})
When I make get request to the api it returns content of .mpd
as it is (i.e xml). When I used that api in vlc network stream
, vlc couldn't play the video. But when I dragged .mpd
file directly to vlc, the video gets played with 144p only. I don't know where I got wrong in GET
. Please help.
Upvotes: 0
Views: 129