コード例 #1
0
ファイル: server.go プロジェクト: fzakaria/transcoding
func TranscodePost(conversions map[string]FfmpegConversion) echo.HandlerFunc {
	fn := func(c *echo.Context) error {
		//The 0 here is important because it forces the file
		//to be written to disk, causing us to cast it to os.File
		c.Request().ParseMultipartForm(0)
		mf, _, err := c.Request().FormFile("input")
		if err != nil {
			c.String(http.StatusBadRequest, "Error parsing input file.")
			return err
		}
		input := mf.(*os.File)
		defer os.Remove(input.Name())

		output, err := ioutil.TempFile("", "output")
		if err != nil {
			c.String(http.StatusInternalServerError, "Error creating output file.")
			return err
		}
		defer os.Remove(output.Name())

		conversion, exists := conversions[c.Form("type")]
		if !exists {
			return c.String(http.StatusBadRequest, "Not a valid transcoding type.")
		}

		converter := ffmpeg.NewConverter(input.Name(), output.Name(), conversion.Scale,
			conversion.VideoKilobitRate, conversion.AudioKilobitRate)

		if err := converter.Transcode(); err != nil {
			c.String(http.StatusInternalServerError, "Error transcoding the file.")
			return err
		}

		c.Response().Header().Set(echo.ContentType, "video/mp4")
		fi, err := output.Stat()
		if err != nil {
			c.String(http.StatusInternalServerError, "Error retrieving size of file.")
			return err
		}
		c.Response().Header().Set(echo.ContentLength, strconv.FormatInt(fi.Size(), 10))

		if err := c.File(output.Name(), "output.mp4", true); err != nil {
			c.String(http.StatusInternalServerError, "Error sending file.")
			return err
		}

		return nil
	}
	return fn
}
コード例 #2
0
ファイル: server.go プロジェクト: fzakaria/transcoding
func TranscodeJsonPost(awsConfig AwsConfig, conversions map[string]FfmpegConversion) echo.HandlerFunc {
	fn := func(c *echo.Context) error {
		request := &client.TranscodeRequest{}
		if err := c.Bind(request); err != nil {
			return err //return Unsupported Media Type or BadRequest
		}

		svc := s3.New(session.New(&aws.Config{Region: aws.String(awsConfig.Region)}))
		getObjectParams := &s3.GetObjectInput{
			Bucket: aws.String(request.Input.Bucket),
			Key:    aws.String(request.Input.Key),
		}
		resp, err := svc.GetObject(getObjectParams)
		if err != nil {
			log.WithFields(log.Fields{
				"error":   err.Error(),
				"code":    err.(awserr.Error).Code(),
				"message": err.(awserr.Error).Message(),
			}).Warn("Issue occured fetching object.")
			return err
		}
		input, err := ioutil.TempFile("", "s3Input")
		if err != nil {
			log.WithFields(log.Fields{
				"error": err,
			}).Error("Error creating s3 input temporary file.")
			return err
		}
		defer os.Remove(input.Name())
		//Copy over the buffer to the file
		_, errCopy := io.Copy(input, resp.Body)
		if errCopy != nil {
			log.WithFields(log.Fields{
				"error": errCopy,
			}).Error("Error copying object to temporary input file")
			return errCopy
		}
		conversion, ok := conversions[request.Type]
		if !ok {
			return errors.New("This type does not exists")
		}
		output, err := ioutil.TempFile("", "output")
		if err != nil {
			return err
		}
		defer os.Remove(output.Name())
		converter := ffmpeg.NewConverter(input.Name(), output.Name(), conversion.Scale,
			conversion.VideoKilobitRate, conversion.AudioKilobitRate)

		if err := converter.Transcode(); err != nil {
			return err
		}

		fi, _ := output.Stat()
		//Begin Upload
		putObjectParams := &s3.PutObjectInput{
			Bucket:        aws.String(request.Output.Bucket),
			Key:           aws.String(request.Output.Key),
			ContentType:   aws.String("video/mp4"),
			ContentLength: aws.Int64(fi.Size()),
			Body:          output,
		}

		_, err1 := svc.PutObject(putObjectParams)
		if err1 != nil {
			log.WithFields(log.Fields{
				"error": err1,
			}).Error("Error putting s3 output temporary file.")
			return err1
		}
		return nil
	}
	return fn
}